import java.sql.*;
import java.awt.*;
import java.awt.event.*;public class JDBC
{
public static void main(String args[])throws Exception
{ new MyFrame("我的JDBC连接"); }
}
class MyFrame extends Frame implements ActionListener
{
Button btadd;
Button btn2;
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label lab2;
TextField input1;
Label lab3;
TextField input2;
Label lab4;
TextField input3;
Toolkit tool = getToolkit();
Dimension dim = tool.getScreenSize(); MyFrame(String s)
{
super(s);
Label outputL=new Label(" 学生信息:");
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label output=new Label("请输入入学生信息进行添加");
Label lab2=new Label("学号:");
TextField input1=new TextField(10);
Label lab3=new Label("姓名:");
TextField input2=new TextField(6);
Label lab4=new Label("性别");
TextField input3=new TextField(4);
Button btadd=new Button("添加");
Label query=new Label("请输入学号进行查询:");
Label query1=new Label("学号");
TextField query2=new TextField(10);
Button btn2=new Button("查询");
Label result=new Label("查询结果: ");
add(outputL);
add(outputLbl1);
add(outputLbl2);
add(outputLbl3);
add(output);
add(lab2);
add(input1);
add(lab3);
add(input2);
add(lab4);
add(input3);
add(btadd);
add(query);
add(query1);
add(query2);
add(btn2);
add(result);
btadd.addActionListener(this);
btn2.addActionListener(this);
setLayout(new FlowLayout());
setBounds(0,0,dim.width/3,dim.height/3);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){ outputLbl1.setText("\t 学号:"+rs.getString(1));
outputLbl2.setText("\t 姓名:"+rs.getString(2));
outputLbl3.setText("\t 性别:"+rs.getString(3)+"\n");
stmt.close();
rs.close();
}
}catch(Exception e){ e.printStackTrace(); }
pack();
show(); }
public void actionPerformed(ActionEvent e){
System.out.println("hao"); if(e.getSource() == btadd)
{
Connection con;
Statement stmt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student1";
con=DriverManager.getConnection(url);
stmt = con.createStatement();
String update = "insert into student values(input1.getText(),input2.getText(),input3.getText())";
int result = stmt.executeUpdate(update);
if ( result > 0 )
{ System.out.println("update success!");
} else{
System.out.println("update fail!");
}
try{
if(!con.isClosed())
stmt.close();
con.close(); //关闭数据库连接
}catch(SQLException err){
System.err.println(err.toString());
}
}catch(Exception ae){ ae.printStackTrace(); }
}
if(e.getSource() == btn2)
{
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1=DriverManager.getConnection("jdbc:odbc:student1","","");
//加载JDBC-ODBC bridge驱动程序
PreparedStatement pstmt=con1.prepareStatement("select * from student where 学号 = query2.getText()");
ResultSet rs1=pstmt.executeQuery();
rs1.close();
pstmt.close();
con1.close();
}catch(Exception e1) { }
}
}
}
import java.awt.*;
import java.awt.event.*;public class JDBC
{
public static void main(String args[])throws Exception
{ new MyFrame("我的JDBC连接"); }
}
class MyFrame extends Frame implements ActionListener
{
Button btadd;
Button btn2;
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label lab2;
TextField input1;
Label lab3;
TextField input2;
Label lab4;
TextField input3;
Toolkit tool = getToolkit();
Dimension dim = tool.getScreenSize(); MyFrame(String s)
{
super(s);
Label outputL=new Label(" 学生信息:");
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label output=new Label("请输入入学生信息进行添加");
Label lab2=new Label("学号:");
TextField input1=new TextField(10);
Label lab3=new Label("姓名:");
TextField input2=new TextField(6);
Label lab4=new Label("性别");
TextField input3=new TextField(4);
Button btadd=new Button("添加");
Label query=new Label("请输入学号进行查询:");
Label query1=new Label("学号");
TextField query2=new TextField(10);
Button btn2=new Button("查询");
Label result=new Label("查询结果: ");
add(outputL);
add(outputLbl1);
add(outputLbl2);
add(outputLbl3);
add(output);
add(lab2);
add(input1);
add(lab3);
add(input2);
add(lab4);
add(input3);
add(btadd);
add(query);
add(query1);
add(query2);
add(btn2);
add(result);
btadd.addActionListener(this);
btn2.addActionListener(this);
setLayout(new FlowLayout());
setBounds(0,0,dim.width/3,dim.height/3);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){ outputLbl1.setText("\t 学号:"+rs.getString(1));
outputLbl2.setText("\t 姓名:"+rs.getString(2));
outputLbl3.setText("\t 性别:"+rs.getString(3)+"\n");
stmt.close();
rs.close();
}
}catch(Exception e){ e.printStackTrace(); }
pack();
show(); }
public void actionPerformed(ActionEvent e){
System.out.println("hao"); if(e.getSource() == btadd)
{
Connection con;
Statement stmt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student1";
con=DriverManager.getConnection(url);
stmt = con.createStatement();
String update = "insert into student values(input1.getText(),input2.getText(),input3.getText())";
int result = stmt.executeUpdate(update);
if ( result > 0 )
{ System.out.println("update success!");
} else{
System.out.println("update fail!");
}
try{
if(!con.isClosed())
stmt.close();
con.close(); //关闭数据库连接
}catch(SQLException err){
System.err.println(err.toString());
}
}catch(Exception ae){ ae.printStackTrace(); }
}
if(e.getSource() == btn2)
{
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1=DriverManager.getConnection("jdbc:odbc:student1","","");
//加载JDBC-ODBC bridge驱动程序
PreparedStatement pstmt=con1.prepareStatement("select * from student where 学号 = query2.getText()");
ResultSet rs1=pstmt.executeQuery();
rs1.close();
pstmt.close();
con1.close();
}catch(Exception e1) { }
}
}
}
Button btadd=new Button("添加");
这两个前面的Buttion去了.然后加上
btn2.add.....(this);
btadd.add.....(this);
返回此按钮激发的动作事件的命令名称。如果命令名称为 null(默认),则此方法返回按钮的标签。
不断学习,不断进步!
看个P,胡说八道,忽悠新手呢?!
什么Button没有添加事件?! 看清了再回!原因是:Button btn2 = ...;这样你并没有对之前定义的全部变量 btn2 进行初始化,而是有重新定义了一个新的与全局变量同名的局部变量,根据Java的优先规则,你的操作是对局部变量进行的。监听是已经触发了的,只是getSource()获取的是局部变量 btn2 的引用,你在判断的时候 getSource() == btn2,这里的btn2是定义的布局变量,所以你的判断是始终都不成立,并不是监听没有响应修改方案:
1、去掉 Button btn2 = ,使其为全部变量初始化,这样你的getSource就没有问题了。2、使用getActionCommand,但是最好还是在初始化的时候btn2.setActionCommand(唯一的命令字符串),避免使用Text做为Command。因为在界面比较复杂的时候,难免会有组件的Text相同。
说话放干净点.我只是没有看到他的添加事件的地方而已.你说的btn2问题我没有说吗?
Button btn2=new Button("查询");
Button btadd=new Button("添加");
这两个前面的Buttion去了. 然后加上
btn2.add.....(this);
btadd.add.....(this);