《java学生信息录入和查询(连接mysql数据库).doc》由会员分享,可在线阅读,更多相关《java学生信息录入和查询(连接mysql数据库).doc(25页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datejava学生信息录入和查询(连接mysql数据库)成绩录入1. 成绩录入package studentManage;import java.awt.GridLayout; import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Connection;i
2、mport java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.*;public class StudentManageSystem extends JFrameJTextField jtfdID = new JTextField(8);JTextField jtfdName = new JTextField(128);JTextField jtfdSex = new JTextField(128);J
3、TextField jtfdAge = new JTextField(8);public StudentManageSystem()JButton jbtnEnter= new JButton(录入);JButton jbtnClear= new JButton(清零);JLabel jlblID = new JLabel(学号);JLabel jlblName = new JLabel(姓名);JLabel jlblSex = new JLabel(性别);JLabel jlblAge = new JLabel(年龄);setLayout(new GridLayout(5,2,5,5);ad
4、d(jlblID);add(jtfdID);add(jlblName);add(jtfdName);add(jlblSex);add(jtfdSex);add(jlblAge);add(jtfdAge);add(jbtnEnter);add(jbtnClear);jbtnEnter.addActionListener(new EnterListener();jbtnClear.addActionListener(new ClearListener();class EnterListener implements ActionListenerpublic void actionPerformed
5、(ActionEvent e)Boolean rs = null;tryClass.forName(com.mysql.jdbc.Driver);Connection conn = DriverManager.getConnection(jdbc:mysql:/127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8,root1,root);Statement stmt = conn.createStatement();String sql = insert into student values(+jtfdID.getText()+
6、,+jtfdName.getText()+,+jtfdSex.getText()+,+jtfdAge.getText()+); stmt.execute(sql); jtfdID.setText( );jtfdName.setText( );jtfdSex.setText( );jtfdAge.setText( );catch (ClassNotFoundException e2) System.out.println(Sorry,cant find the Driver!);e2.printStackTrace(); catch (SQLException e3) e3.printStack
7、Trace(); catch (Exception e4) e4.printStackTrace();class ClearListener implements ActionListenerpublic void actionPerformed(ActionEvent e)jtfdID.setText();jtfdName.setText();jtfdSex.setText();jtfdAge.setText();public static void main (String args)StudentManageSystem s = new StudentManageSystem();s.s
8、etTitle(学生个人信息录入);s.setSize(400,300);s.setLocationRelativeTo(null);s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);s.setVisible(true);2. 成绩查询import java.awt.BorderLayout; import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;imp
9、ort java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import com.mysql.jdbc.Prepa
10、redStatement;public class Search extends JFrameJComboBox jcbSelect= new JComboBox();JTextField jtfdInput = new JTextField();JButton jbtnSearch = new JButton(查询);String slt = null;/确定下拉框所选的内容 public Search() jcbSelect.addItem(学号); jcbSelect.addItem(姓名); jcbSelect.addItem(性别); jcbSelect.addItem(年龄); J
11、Panel jpl1=new JPanel(new GridLayout(2,1); /setLayout(new GridLayout(2,1); jpl1.add(jcbSelect); jpl1.add(jtfdInput); setLayout(new GridLayout(2,1); add(jpl1); add(jbtnSearch); jbtnSearch.addActionListener(new SearchListener(); if(jcbSelect.getSelectedItem()=学号) slt=Sno; else if(jcbSelect.getSelected
12、Item()=姓名) slt = Sname; else if(jcbSelect.getSelectedItem()=性别) slt = Ssex; else slt = Sage; class SearchListener implements ActionListener public void actionPerformed(ActionEvent e) ResultSet rs = null; try Class.forName(com.mysql.jdbc.Driver); Connection conn = DriverManager.getConnection(jdbc:mys
13、ql:/127.0.0.1:3306/test,root1,root); /PreparedStatement ps = conn.prepareStatement(select * from student where +slt+like %?%); Statement stmt = conn.createStatement(); String sql= select * from student where +slt+=+ jtfdInput.getText(); rs = stmt.executeQuery(sql); catch (ClassNotFoundException e2)
14、System.out.println(Sorry,cant find the Driver!); e2.printStackTrace(); catch (SQLException e3) e3.printStackTrace(); catch (Exception e4) e4.printStackTrace(); try while(rs.next()System.out.println(rs.getString(1)+t+rs.getString(2)+t+rs.getString(3)+t+rs.getInt(4); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();/while循环输出结果 public static void main (String args)Search s = new Search();s.setTitle(学生个人信息查询);s.setSize(300,200);s.setLocationRelativeTo(null);s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);s.setVisible(true);-