萌新求教,查询获取不到数据
IT小喳喳 2018-07-22 11:16:43 package org.mhr.StudentManager.student.management;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.mhr.StudentManager.main.db.DBConnector;
import java.sql.*;
class GetStudent extends JFrame
implements ActionListener
{
private static final long serialVersionUID =1L;
JLabel JL =new JLabel("查 询 基 本 信 息",JLabel.CENTER);
JLabel JLNumber=new JLabel("请输入学号:");
JTextField JTNumber=new JTextField();
JLabel JLName =new JLabel("姓名:");
JTextField JTName =new JTextField();
JLabel JLClass =new JLabel("班级:");
JTextField JTClass =new JTextField();
JLabel JLsex =new JLabel("性别:");
ButtonGroup BG =new ButtonGroup();
JRadioButton JRB1 =new JRadioButton("男");
JRadioButton JRB2 =new JRadioButton("女");
JLabel JL1 =new JLabel("学院:");
JTextField JT1 =new JTextField();
JLabel JL2 =new JLabel("生日:");
JTextField JT2 =new JTextField();
JButton JBSet =new JButton("查询");
JButton JBNext =new JButton("重置");
JButton JBExit =new JButton("退出");
String sql="";
public GetStudent()
{
this.setTitle("查询学生信息");
this.setLayout(null);
JL.setFont(new Font("TimesRoman",Font.BOLD,24));
JL.setForeground(Color.red);
JL.setFont(new java.awt.Font("宋体",Font.PLAIN,19));
JL.setBounds(100,30,200,40);
this.add(JL);
JLNumber.setBounds(100,80,100,20);
this.add(JLNumber);
JTNumber.setBounds(200,80,80,20);
this.add(JTNumber);
JLName.setBounds(100,160,60,20);
this.add(JLName);
JTName.setBounds(200,160,80,20);
this.add(JTName);
JLsex.setBounds(100,200,100,20);
this.add(JLsex);
JRB1.setBounds(200,200,40,20);
JRB2.setBounds(300,200,40,20);
this.add(JRB1);
this.add(JRB2);
BG.add(JRB1);
BG.add(JRB2);
JL2.setBounds(100,240,80,20);
this.add(JL2);
JT2.setBounds(200,240,80,20);
this.add(JT2);
JLClass.setBounds(100,280,60,20);
this.add(JLClass);
JTClass.setBounds(200,280,80,20);
this.add(JTClass);
JL1.setBounds(100,320,60,20);
this.add(JL1);
JT1.setBounds(200,320,80,20);
this.add(JT1);
JBSet.setBounds(80,120,90,20);
this.add(JBSet);
JBSet.addActionListener(this);
JBNext.setBounds(190,120,90,20);
this.add(JBNext);
JBNext.addActionListener(this);
JBExit.setBounds(300,120,90,20);
this.add(JBExit);
JBExit.addActionListener(this);
this.setBounds(10,10,500,400);
this.setVisible(true);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==JBSet)
{
int id = Integer.parseInt(JTNumber.getText());
sql="select * from students where Id="+id;
try
{
Statement stm = DBConnector.getConn().createStatement();
ResultSet rs=stm.executeQuery(sql);
if(rs.next())
{
String name=rs.getString("2");
JTName.setText(name);
String sex=rs.getString(4);
if(sex.equals("男")) {
JRB1.setSelected(true);
}else {
JRB2.setSelected(true);
}
String birthday=rs.getString("8");
JT2.setText(birthday);
String clas=rs.getString("5");
JTClass.setText(clas);
String school=rs.getString("10");
JT1.setText(school);
}
else
{
JOptionPane.showMessageDialog(null,"此用户不存在!");
}
}catch(Exception ee)
{
}
}
if(e.getSource()==JBNext)
{
JTNumber.setText(null);
JTName.setText(null);
JTClass.setText(null);
JT1.setText(null);
JT2.setText(null);
}
if(e.getSource()==JBExit)
{
setVisible(false);
}
}
public static void main(String args[])
{
new GetStudent();
}
}