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();
}
}
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();
}
}
解决方案 »
- 求助,新手问题.
- 关于UDP实现可靠数据传输(Java)问题(急,急,急)
- Java Date类型转换问题
- 请问一下J2EE的SDK安装主要是安装一个应用服务器吗?关于平时用的时候是不是主要只要j2ee.jar这个包就够了啊?
- String s = new String("xyz");创建了三个String Object!!
- 百思不得其解,请过路神仙帮忙
- 一个关于JAVA实现计数器方面的问题!
- 有关NullPointerException的问题
- 有没有人会iplanet application server的?怎么没有人理我?
- 那里可以下载visual cafe
- 大佬求指导
- Java初始化作业,做不通
1. 先看有没有报错
2. 打个断点调试下,看数据库连接是否获取到
3. 断点看看resultSet是否获取到数据
String name=rs.getString("2");
...
String sex=rs.getString(4);
...
String birthday=rs.getString("8");
...
String clas=rs.getString("5");
...
String school=rs.getString("10");
...
改成
...
String name=rs.getString(2);
...
String sex=rs.getString(4);
...
String birthday=rs.getString(8);
...
String clas=rs.getString(5);
...
String school=rs.getString(10);
...
rs.getString("name")//通过字段名称查询
rs.getString("2");
看到你这个取值,我认为你有必要改下
第一种可能你是按去下标取值rs.getString(2); 那就去掉双引号。
但是这里还要考虑如果你查询出来字段位置被你改动了,
那你代码也要改哈,还有下标边界问题。你还要数这个字段是第几位,还有可能引起下标越界错误
所以我建议用属性名取值 rs.getString("属性名");