将java文本框获取的数据插入数据库中

最好写的详细点

第1个回答  2010-07-02
import java.awt.*;
import java.awt.event.*;
public class Test1 extends WindowAdapter implements ActionListener{
TextField t1,t2;
Button b1,b2;
Frame f;
Label l1,l2;
void init(){
f=new Frame("注册窗口");
l1=new Label("用户名",Label.CENTER);
l2=new Label("密码",Label.CENTER);
t1=new TextField(11);
t2=new TextField(11);
b1=new Button("注册");
b2=new Button("取消");
f.setLayout(new FlowLayout());//流式布局管理
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b1);
f.add(b2);
f.setVisible(true);
f.pack();
t2.setEchoChar('*');

t1.addActionListener(this);
t2.addActionListener(this);
f.addWindowListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
f.setLocation(300,300);
f.setResizable(false);
f.setBackground(Color.BLUE);
l1.setBackground(Color.YELLOW);
l1.setForeground(Color.RED);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
String u1=t1.getText();
String p1=t2.getText();
myDb mydb=new myDb();
mydb.connection("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:myDb");
String sql="insert into user(username,password) values ('"+u1+"','"+p1+"');";
mydb.update(sql);
}
else if(e.getSource()==b2)
{
t1.setText("");
t2.setText("");
}

}
public static void main(String[] args) {
new Test1().init();
}

}本回答被提问者采纳
相似回答