我正在尝试使用java awt创建一个表单应用程序。然后我要添加从表单中获取的数据到我的数据库中。 我在按钮btnadd中使用了ActionListener,该按钮应该连接到数据库并存储在数据库中
这是用于插入数据的awt表格
import java.awt.*;
import java.awt.event.*;
public class InsertFrom extends Frame{
Panel panelInsert,panelbtn;
Label lfName,lmName,llName,lage,lssn,lcity,lstate,lcountry;
TextField tfName,tmName,tlName,tage,tssn,tcity,tstate,tcountry;
Button btnadd;
String firstName,middleName,lastName,city,state,country;
int age,ssn;
InsertFrom(){
lfName = new Label("First Name");
lmName = new Label("Middle Name");
llName = new Label("Last Name");
lage = new Label("Age");
lssn = new Label("SSN");
lcity = new Label("City");
lstate = new Label("State");
lcountry = new Label("Country");
tfName = new TextField(10);
tmName = new TextField(10);
tlName = new TextField(10);
tage = new TextField(10);
tssn = new TextField(10);
tcity = new TextField(10);
tstate = new TextField(10);
tcountry = new TextField(10);
panelInsert = new Panel(new GridLayout(8,2));
panelInsert.add(lfName);
panelInsert.add(tfName);
panelInsert.add(lmName);
panelInsert.add(tmName);
panelInsert.add(llName);
panelInsert.add(tlName);
panelInsert.add(lage);
panelInsert.add(tage);
panelInsert.add(lssn);
panelInsert.add(tssn);
panelInsert.add(lcity);
panelInsert.add(tcity);
panelInsert.add(lstate);
panelInsert.add(tstate);
panelInsert.add(lcountry);
panelInsert.add(tcountry);
btnadd = new Button("Add to Database");
panelbtn = new Panel(new FlowLayout());
panelbtn.add(btnadd);
btnadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstName=tfName.getText();
middleName=tmName.getText();
lastName=tlName.getText();
age=Integer.parseInt(tage.getText());
ssn=Integer.parseInt(tssn.getText());
city=tcity.getText();
state=tstate.getText();
country=tcountry.getText();
InsertingData();
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
add(panelInsert);
add(panelbtn);
setTitle("Insert Form");
setLayout(new GridLayout(2,1));
setVisible(true);
setSize(500,500);
setLocationRelativeTo(null);
}
public static void main(String[] args){
new InsertFrom();
}
}
这是用于连接数据库并在数据库中插入数据
import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;
public class InsertingData {
public static void main(String args[]) throws Exception{
String sql="INSERT INTO customers VALUES ('"+firstName+"','"
+middleName+"','"+lastName+"',"+age+","+ssn+",'"
+city+"',"+state+",'"+country+"')";
insertCustomer(sql);
}
private static void insertCustomer(String sql) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb";
Connection con =DriverManager.getConnection(url,"root","");
if(con!=null){
Statement stmt =con.createStatement();
int result= stmt.executeUpdate(sql);
if(result!=-1){
System.out.println("Inserted"+result+"Record(s) successfully");
}else{
System.out.println("Unable to insert record. Please check your
SQL syntax");
}
stmt.close();
con.close();
}else{
System.out.println("Unable to get the connection");
}
}
}
答案 0 :(得分:0)
在InsertingData
中更改您的main
方法名称。如果您不想从那时开始启动应用,请尝试避免使用main
关键字。
您可以这样做:
public static void insertData(String firstName, String middleName,
String lastName, int age, int ssn, String city
String state, String country) throws Exception{
String sql="INSERT INTO customers VALUES ('"+firstName+"','"
+middleName+"','"+lastName+"',"+age+","+ssn+",'"
+city+"',"+state+",'"+country+"')";
insertCustomer(sql);
}
然后从ActionListener
在InsertFrom
中调用它,如下所示:
InsertingData.insertData(firstName, middleName, lastName, age, ssn, city, state, country);