【问题标题】:Code that could save data by entering through jframe to database通过jframe进入数据库可以保存数据的代码
【发布时间】:2015-08-06 19:50:46
【问题描述】:

我正在制作一个基于表单的数据库,问题是我无法弄清楚数据库的表将如何使用我通过表单输入的数据进行更新。这是代码。

   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:
}                                          

【问题讨论】:

    标签: java swing jdbc


    【解决方案1】:

    创建 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 参数的值。

    【讨论】:

    • 我还是不知道该怎么办..!! :(
    • 在更新按钮的代码中,您要为“?”插入字符串参数人物。上面的为 PreparedStatement 参数提供值部分为您提供了如何执行此操作的示例。如果您需要答案如何完成整个过程(按钮单击事件处理、创建连接等)请查看@Amit Bhati 的答案
    【解决方案2】:

    1) 将 JFrame 上的所有值绑定到 Java Pojo 或类。 查看答案:Binding of JText fields value to Info Class

    2) 现在给保存按钮绑定一个action方法,这里可以使用步骤1的类来获取通过form输入的值。现在您所要做的就是创建与数据库的连接并插入数据。

    阅读 swing 中的变量绑定以获得更好的想法。

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2014-02-17
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多