我正在制作一个基于表单的数据库,问题是我无法弄清楚如何使用我通过表单输入的数据更新数据库的表。这是代码。
public New_Entry() {
initComponents();
}
private void pat_nameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
@SuppressWarnings("UseSpecificCatch")
这是更新按钮的代码字符串......请帮助我做什么......???
private void cmd_updateActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con;
String url ="jdbc:mysql://localhost:3306/testdb";
String user ="root";
String password ="";
con=(javaapplication5.Connection) DriverManager.getConnection(url,user,password);
Statement stmt= con.createStatement();
String Patient_ID=pat_id.getText();
String name=pat_name.getText();
String age=pat_age.getText();
String sex=pat_sex.getText();
String unit=pat_unit.getText();
String Diagonisis=diagonisis.getText();
String DateOfAddmission=DOA.getText();
String TreatmentPlan=treat_plan.getText();
String sql1 = "Insert into patient (Patient_ID,Name,Age,Unit,Sex,Diagonsis,DateOfAddmission,Treatment_Plan) values (?,?,?,?,?,?,?,?)";
stmt=con.prepareStatment(sql1);
stmt.executeUpdate(sql1);
pat_id.setText("");
pat_name.setText("");
pat_age.setText("");
pat_sex.setText("");
pat_unit.setText("");
diagonisis.setText("");
DOA.setText("");
treat_plan.setText("");
JOptionPane.showMessageDialog(null, " Record Updated!");
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void pat_ageActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void pat_unitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void DOAActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void pat_idActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void cmd_exitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);// TODO add your handling code here:
}
private void cmd_backActionPerformed(java.awt.event.ActionEvent evt) {
Start form = new Start();
New_Entry.this.setVisible(false);
form.setVisible(true);
}
private void cmd_newformActionPerformed(java.awt.event.ActionEvent evt) {
New_Entry frame2 = new New_Entry();
New_Entry.this.setVisible(false);
frame2.setVisible(true);
}
private void diagonisisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
最佳答案
创建PreparedStatement对象
下面创建一个带有两个输入参数的PreparedStatement对象:
String updateString =
"update " + dbName + ".COFFEES " +
"set SALES = ? where COF_NAME = ?";
updateSales = con.prepareStatement(updateString);
为PreparedStatement参数提供值
在执行PreparedStatement 对象之前,您必须提供值来代替问号占位符(如果有)。通过调用在PreparedStatement 类中定义的setter 方法之一来执行此操作。以下语句在名为 updateSales 的PreparedStatement 中提供了两个问号占位符:
updateSales.setInt(1, e.getValue().intValue());
updateSales.setString(2, e.getKey());
每个 setter 方法的第一个参数指定问号占位符。在此示例中,setInt 指定第一个占位符,setString 指定第二个占位符。 关联: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
在您的情况下,在使用 executUpdate 方法之前,您必须使用字符串字段提供值,请参阅提供PreparedStatement 参数的值。
关于java - 通过jframe输入数据库保存数据的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864566/