为什么我无法使用JDBC java将数据输入sqlite数据库?

时间:2016-07-05 15:59:02

标签: java database sqlite jdbc

我正在尝试制作分类帐(以保留销售记录)。我正在使用sqlite和netbeans。 问题是我的INSERT查询没有向数据库添加数据,而控制台也没有显示任何错误。 该计划包含2个文件ledger.javaledgerGUI.javaledger.java包含执行sql查询的函数定义。这些函数在ledgerGUI.java中调用,以便从JTextField添加数据。

ledger.java的部分代码

  package ledger;
  import java.sql.*;

  public class Ledger  {
  public Connection c = null;
  public Statement stmt = null;

  private String name_db = "";
  private String tm_db = "";
  private String item_db = "";
  private int quantity_db = 0;
  private int price_db = 0;   

// Connecting to the database.
public void connectDb(){        
    try{
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:Ledger.db");

    } 
    catch ( ClassNotFoundException | SQLException e ){        
    }    
}

// Query
public void insertToDb(String name_entry, String tmark_entry, String item_entry, int quantity_entry, int price_entry){
    name_db = name_entry;
    tm_db = tmark_entry;
    item_db = item_entry;
    quantity_db = quantity_entry;
    price_db = price_entry;

    try{
        connectDb();
        stmt = c.createStatement();
        String query = "INSERT INTO ledger_table (Name, Trade Mark, Item, Quantity, Price) VALUES (name_db, tm_db, item_db, quantity_db, price_db)";
        stmt.executeUpdate(query);            
        stmt.close();
        c.commit();
        c.close();           

    }
    catch(Exception e){            
    }             
}

ledgerGui.java的部分代码

 private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
   Ledger lg = new Ledger();
   String name = tf_name.getText();
   String tmark = tf_tmark.getText();
   String item = tf_item.getText();
   int quantity = Integer.parseInt(tf_quantity.getText());
   int price = Integer.parseInt(tf_price.getText());
   try{
       lg.insertToDb(name, tmark.toUpperCase(), item, quantity, price);
   }
         catch(Exception e){      
    }
   tf_item.setText("");
   tf_quantity.setText("");
   tf_price.setText("");
}            

1 个答案:

答案 0 :(得分:0)

您可以使用PreparedStatement,如下所示:

// Query
public void insertToDb(String name_entry, String tmark_entry, String item_entry, int quantity_entry, int price_entry){
    name_db = name_entry;
    tm_db = tmark_entry;
    item_db = item_entry;
    quantity_db = quantity_entry;
    price_db = price_entry;
try{
    connectDb();
    String query =
        "INSERT INTO ledger_table (Name, Trade Mark, Item, Quantity, Price) VALUES (?, ?, ?, ?, ?)";        
    PreparedStatement statement = c.prepareStatement(query);
    statement.setString(1, name_db);
    statement.setString(2, tm_db);
    statement.setString(3, item_db);
    statement.setInt(4, quantity_db);
    statement.setInt(5, price_db);
    statement.executeUpdate();            
    c.commit();
    c.close();           

}
catch(Exception e){            
}             
}

您确定Trade Mark在数据库表上有空格分隔符吗?

INSERT INTO ledger_table (Name, Trade Mark, Item, Quantity, Price)...