I am trying to make a ledger (to keep records of sales). I am using sqlite and netbeans. The problem is that my INSERT
query is not adding data to the database and the console is also not showing any error . The program consists of 2 files ledger.java
and ledgerGUI.java
. ledger.java
contains the function definitions to carry out sql queries. These functions are called in ledgerGUI.java
to add data from JTextField
.
Part of the code of 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){
}
}
Part of the code of 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("");
}
You can use PreparedStatement
, something like this:
// 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){
}
}
Are you sure that Trade Mark
has a spaces separator on the table of database...
INSERT INTO ledger_table (Name, Trade Mark, Item, Quantity, Price)...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.