萌新求教,查询获取不到数据

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();
}
}
...全文
349 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HQZYX 2018-07-26
  • 打赏
  • 举报
回复
这个代码有点看不懂,原谅我技术不到家
rs.getString("2");
看到你这个取值,我认为你有必要改下
第一种可能你是按去下标取值rs.getString(2); 那就去掉双引号。
但是这里还要考虑如果你查询出来字段位置被你改动了,
那你代码也要改哈,还有下标边界问题。你还要数这个字段是第几位,还有可能引起下标越界错误
所以我建议用属性名取值 rs.getString("属性名");
RyanIsBest 2018-07-25
  • 打赏
  • 举报
回复
楼上错了,getString()里面的参数应该是你SQL语句的参数
tiaoxixiaoji 2018-07-25
  • 打赏
  • 举报
回复
rs.getString(5)//通过字段下标查询
rs.getString("name")//通过字段名称查询
stacksoverflow 2018-07-23
  • 打赏
  • 举报
回复
你应该是按照下标取得数据,把下标的引号去掉

...
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);
...
小明同学12138 2018-07-22
  • 打赏
  • 举报
回复
你给这个代码也看不出什么问题
1. 先看有没有报错
2. 打个断点调试下,看数据库连接是否获取到
3. 断点看看resultSet是否获取到数据

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧