《java学生信息录入和查询.docx》由会员分享,可在线阅读,更多相关《java学生信息录入和查询.docx(6页珍藏版)》请在三一办公上搜索。
1、java学生信息录入和查询1.成绩录入 package studentManage; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.
2、swing.*; public class StudentManageSystem extends JFrame JTextField jtfdID = new JTextField(8); JTextField jtfdName = new JTextField(128); JTextField jtfdSex = new JTextField(128); JTextField jtfdAge = new JTextField(8); public StudentManageSystem JButton jbtnEnter= new JButton(录入); JButton jbtnClea
3、r= 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); add(jlblID); add(jtfdID); add(jlblName); add(jtfdName); add(jlblSex); add(jtfdSex); add(jlblAge); add(jtfdAge); a
4、dd(jbtnEnter); add(jbtnClear); jbtnEnter.addActionListener(new EnterListener); jbtnClear.addActionListener(new ClearListener); class EnterListener implements ActionListener public void actionPerformed(ActionEvent e) Boolean rs = null; try Class.forName(com.mysql.jdbc.Driver); Connection conn = Drive
5、rManager.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+,+jtfdName.getText+,+jtfdSex.getText+,+jtfdAge.getText+); class ClearListener implements ActionLis
6、tener public void actionPerformed(ActionEvent e) jtfdID.setText(); jtfdName.setText(); jtfdSex.setText(); jtfdAge.setText(); stmt.execute(sql); catch (ClassNotFoundException e2) System.out.println(Sorry,cant find the Driver!); e2.printStackTrace; e3.printStackTrace; e4.printStackTrace; jtfdID.setTex
7、t( ); jtfdName.setText( ); jtfdSex.setText( ); jtfdAge.setText( ); catch (SQLException e3) catch (Exception e4) public static void main (String args) StudentManageSystem s = new StudentManageSystem; s.setTitle(学生个人信息录入); s.setSize(400,300); s.setLocationRelativeTo(null); s.setDefaultCloseOperation(J
8、Frame.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; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import
9、 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.PreparedStatement; public class Search extends JFrame JComboBox jcbSelect= new JComb
10、oBox; JTextField jtfdInput = new JTextField; JButton jbtnSearch = new JButton(查询); String slt = null;/确定下拉框所选的内容 jcbSelect.addItem(学号); jcbSelect.addItem(姓名); public Search jcbSelect.addItem(性别); jcbSelect.addItem(年龄); JPanel jpl1=new JPanel(new GridLayout(2,1); /setLayout(new GridLayout(2,1); jpl1.
11、add(jcbSelect); jpl1.add(jtfdInput); setLayout(new GridLayout(2,1); add(jpl1); add(jbtnSearch); jbtnSearch.addActionListener(new SearchListener); if(jcbSelect.getSelectedItem=学号) else if(jcbSelect.getSelectedItem=姓名) else if(jcbSelect.getSelectedItem=性别) else slt = Sage; slt = Ssex; slt = Sname; slt
12、=Sno; class SearchListener implements ActionListener ; /PreparedStatement ps = conn.prepareStatement(select * from Statement stmt = conn.createStatement; String sql= select * from student where +slt+=+ student where +slt+like %?%); public void actionPerformed(ActionEvent e) ResultSet rs = null; try
13、Class.forName(com.mysql.jdbc.Driver); Connection conn = DriverManager.getConnection(jdbc:mysql:/127.0.0.1:3306/test,root1,root)jtfdInput.getText; rs = stmt.executeQuery(sql); catch (ClassNotFoundException e2) System.out.println(Sorry,cant find the Driver!); e2.printStackTrace; e3.printStackTrace; e4
14、.printStackTrace; catch (SQLException e3) catch (Exception e4) try while(rs.next) System.out.println(rs.getString(1)+t+rs.getString(2)+t+rs.getString( / TODO Auto-generated catch block e1.printStackTrace; 3)+t+rs.getInt(4); catch (SQLException e1) /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);