运行下列代码后,按的按钮"b5",竟无反应!!!!
package package2;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class Select implements ActionListener {
Frame f;
Button b5,b6;
TextArea ta;
Label l3,l4;
TextField tf3,tf4;
Panel p5,p6,p7,p8;
private static String str7;
private static String str1="sun.jdbc.odbc.JdbcOdbcDriver";
private static String str2="jdbc:odbc:two";
private static String str3="select *from table1";
private static String str="you son of a bitch";
Select()
{
f=new Frame();
b5=new Button("Contiute");
b6=new Button("Cancel");
l3=new Label("input ID:");
l4=new Label("input user_name");
tf3=new TextField("",10);
tf4=new TextField("",10);
ta=new TextArea(30,40);
p5=new Panel();
p6=new Panel();
p7=new Panel();
p8=new Panel();
f.setLayout(new GridLayout(4,1));
p5.add(l3);
p5.add(tf3);
p6.add(l4);
p6.add(tf4);
p8.add(b5);
p8.add(b6);
p7.add(ta);
f.add(p5);
f.add(p6);
f.add(p7);
f.add(p8);
b5.addActionListener(this);
b6.addActionListener(this);
f.setSize(400,500);
f.setResizable(true);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void select() throws SQLException
{
String ID;
Connection cc;
Statement st;
DriverManager dr;
ResultSet rs;
try {
Class.forName(str1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cc=DriverManager.getConnection(str2);
st=cc.createStatement();
rs=st.executeQuery(str3);
while(rs.next())
{
str7=rs.getString(2);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b5)
{
ta.setText(str7);
}
}
public static void main(String [] args) throws SQLException
{
new Select();
}
}
数据库是Microsoft office 2007的,如下:
ID user_name
001 huxiaolu
002 wangzheng
ID 和user_name都是String型的,ID是主健
------解决方案--------------------
怀疑没有取到数据,自己debug一下或者打印一些信息看看
while(rs.next())
{
str7=rs.getString(2);
System.out.println("str7=" + str7); //add code here
}
--------
if(e.getSource()==b5)
{
ta.setText(str7);
System.out.println("str7=" + str7); //add code here
}
------解决方案--------------------
还有个问题,LZ好像没有调用过select方法,也就是没有做过数据库查询,str7的值根本就没被改变过
if(e.getSource()==b5)
{
select(); //首先要调用一下select方法检索数据
ta.setText(str7);
}
------解决方案--------------------
看看这个,或许对你有帮助
http://dev.yesky.com/422/2561422.shtml