日期:2014-05-20 浏览次数:21173 次
import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class search extends JFrame implements ActionListener{ JLabel log=new JLabel("二级查询"); JTextField log_t=new JTextField(); JLabel id=new JLabel("身份证号:"); JTextField id_t=new JTextField(); JLabel name=new JLabel("姓名:"); JTextField name_t=new JTextField(); JLabel result=new JLabel("结果:"); JTextField result_t=new JTextField(); JLabel bishi=new JLabel("笔试:"); JTextField bishi_t=new JTextField(); JLabel jishi=new JLabel("机试:"); JTextField jishi_t=new JTextField(); JButton bt1=new JButton("查询"); JPanel pe=new JPanel(); search() { super("二级考试查询系统"); setSize(350,230); setLocationRelativeTo(null); setVisible(true); setResizable(false); add(pe); pe.setLayout(null); pe.add(log); log.setBounds(120, 10, 100, 34); log.setFont(new Font("行楷",Font.BOLD,20)); pe.add(id); id.setBounds(10, 50, 80, 24); pe.add(id_t); id_t.setBounds(80, 50, 180, 24); pe.add(bt1); bt1.setBounds(270, 49,70, 24); bt1.setFocusPainted(false); pe.add(name); pe.add(name_t); name.setBounds(10, 105, 40, 24); name_t.setBounds(55, 105, 90, 24); name_t.setEditable(false); pe.add(result); pe.add(result_t); result.setBounds(160, 105, 40, 24); result_t.setBounds(200, 105,90, 24); result_t.setEditable(false); pe.add(bishi); pe.add(bishi_t); bishi.setBounds(10, 160, 40, 24); bishi_t.setBounds(55, 160, 90, 24); bishi_t.setEditable(false); pe.add(jishi); pe.add(jishi_t); jishi.setBounds(160, 160, 40, 24); jishi_t.setBounds(200, 160, 90, 24); jishi_t.setEditable(false); bt1.addActionListener(this); } public static void main(String[] args) { // TODO Auto-generated method stub search smp=new search(); smp.setVisible(true); smp.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);}}); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==bt1) { String name,result,bishi,jishi; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e1){ e1.printStackTrace(); } try { Connection con = DriverManager.getConnection("jdbc:mysql://208.81.166.169:3306/hythzxsql","hythzxSQL","727799796"); //Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/二级查询","root","hythzx"); Statement stmt = con.createStatement(); String sql = "SELECT name,result,bishi,jishi FROM result where id='"+id_t.getText()+"'"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { name=rs.getString("name"); result=rs.getString("result"); bishi=rs.getString("bishi"); jishi=rs.getString("jishi"); name_t.setText(name); result_t.setText(result); bishi_t.setText(bishi); jishi_t.setText(jishi); } rs.close(); stmt.close(); con.close(); } catch(Exception e1){ System.out.println("连接失败!"); e1.printStackTrace(); } } } }