public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if(action.equals("Reset")){
title.setSelectedIndex(0);
forename.setText(null);
surname.setText(null);
age.setText(null);
contactNo.setText(null);
houseNo.setText(null);
streetName.setText(null);
districtName.setText(null);
cityName.setText(null);
postcode.setText(null);
healthPlan.setSelectedIndex(0);
treatment.setSelectedIndex(0);
}
当我单击“提交”按钮时,如何使用文本字段中的数据更新我的sql数据库。我已经编写了这段代码,但是当我点击提交时似乎没有发生任何事情,但重置按钮在点击时工作。如何使用Java更新我的Sql数据库
if(action.equals("Submit")){
Connection con = null;
String db="jdbc:mysql://stusql.dcs.shef.ac.uk/team009?user=team009&password=********";
try {
con = DriverManager.getConnection(db);
Statement bil = con.createStatement();
bil.execute("INSERT INTO Patient (ForeName, SurName, DateOfBirth, PhoneNumber, HouseNumber, PostCode) " +
"VALUES (forename.getText(),surname.getText,age.getText,contactNo.getText(),houseNo.getText(),postcode.getText())");
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch (SQLException ex) {
ex.printStackTrace();
}
finally {
if (con != null)
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
请任何帮助,将不胜感激谢谢。
您当前的“查询”应该抛出一个异常 - 定义*“但似乎没有当我点击提交要发生的事情” * – MadProgrammer 2014-12-02 23:46:37
当我点击提交表单保持不变,数据库不更新。 – theBean 2014-12-02 23:48:45
假设没有任何异常被打印到控制台,并且“Inserted Successfully!”没有被显示,那么'actionCommand'可能是错误的。首先添加一些调试语句来跟踪代码中的流程(打印'actionCommand'开始,并提供一些关于代码中的位置的消息),如果这似乎没有帮助,则添加断点并开始调试代码... – MadProgrammer 2014-12-02 23:51:19