今天就要交java大作业了,但我的程序还没完全成功,有个问题,望知道的大侠帮忙解决
在主类里实现了与数据库的连接
在另一个类里,还要再对此数据库进行操作,于是在构造函数内,我使用主类的connection con 作为参数,再在此类中生成statement 但是执行sql语句后却得到了空的resultset,sql语句是应该没有写错的,不知为什么
第二个类的代码如下,只看最后的两个函数就行,
class ReadPane {
String readSql = new String();
double leftM;
Connection con;
Statement st;
ResultSet rt;
JDialog readDialog = new JDialog();
JTabbedPane rd = new JTabbedPane();
JPanel shuaka = new JPanel();
JPanel chong = new JPanel();
JTextField rt1 = new JTextField();
JTextField rt2 = new JTextField();
JTextField rt3 = new JTextField();
JTextField rt11 = new JTextField();
JTextField rt22 = new JTextField();
JLabel rl1 ;
JLabel rl2 ;
JLabel rl3 ;
JLabel rl11 ;
JLabel rl22 ;
JButton ok2 ;
JButton ok1 ;
JPanel shuaUp = new JPanel();
JPanel chongUp = new JPanel();
ReadPane(){
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException e) {JOptionPane.showMessageDialog(null, "数据库错误");}
try{
con = DriverManager.getConnection("jdbc:odbc:shitang", "a", "a");
st=con.createStatement();}
catch(SQLException xx){System.out.println("youcuole");
}
rl1 = new JLabel("输入卡号:");
rl2 = new JLabel("输入密码:");
rl3 = new JLabel("输入金额:");
rl11 = new JLabel("输入卡号:");
rl22 = new JLabel("输入金额:");
ok2 = new JButton("确定");
ok1 = new JButton("确定");
readDialog.setSize( new Dimension(360, 260) );
readDialog.setTitle( "读卡" );
rd.addTab( "刷卡", shuaka );
rd.addTab( "充值",chong );
shuaka.setLayout( new BorderLayout() );
shuaka.add( shuaUp, BorderLayout.CENTER );
shuaUp.setLayout( new GridLayout(3,2) );
shuaUp.add(rl1);
shuaUp.add(rt1);
shuaUp.add(rl2);
shuaUp.add(rt2);
shuaUp.add(rl3);
shuaUp.add(rt3);
shuaka.add(ok1,BorderLayout.SOUTH);
chong.setLayout( new BorderLayout() );
chongUp.setLayout( new GridLayout(2,2) );
chongUp.add(rl11);
chongUp.add(rt11);
chongUp.add(rl22);
chongUp.add(rt22);
chong.add( chongUp, BorderLayout.CENTER );
chong.add( ok2,BorderLayout.SOUTH );
readDialog.getContentPane().add( rd );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
readDialog.setLocation( (int)(screenSize.width-420)/2,
(int)(screenSize.height-360)/2
);
readDialog.setResizable(false);
ok1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){ readOkButton_actionPerformed(); }
}
);
ok2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){ readOk2Button_actionPerformed(); }
}
);
readDialog.show();
}
private void readOkButton_actionPerformed(){
readSql = "select * from student where 卡号=" + rl1.getText() ;
try{
rt= st.executeQuery( readSql );
if(rt1.getText()!=rt.getString(2)) {
JOptionPane.showMessageDialog(null,"密码错误!");
return;
}
leftM = Double.valueOf(rt.getString(6)).doubleValue() - Double.valueOf(rl2.getText()).doubleValue();
readSql = "update student where 卡号=" + rt1.getText() + "set 余额=" + Double.toString(leftM);
st.executeUpdate( readSql );
}
catch( SQLException e ){System.out.println("dd");
}
readDialog.setVisible(false);
};
void readOk2Button_actionPerformed() {
readSql="select * from student where 卡号=" + rt11.getText();
try{
rt=st.executeQuery( readSql );
System.out.println(rt.getString(2));
leftM=Double.valueOf(rt.getString("余额")).doubleValue()+Double.valueOf(rl22.getText()).doubleValue();
readSql="update student where 卡号=" + rt11.getText() + "set 余额=" + Double.toString(leftM);
st.executeUpdate( readSql );
}
catch(SQLException e ){
System.out.println( "dddd" );
}
readDialog.setVisible( false );
};
}