I'm writing a Java app which has to add a record to a database. Everything works fine until I want to add a local variable to the database (I think I'm placing my parentheses wrong or something). Anyway, I'm tired of looking for the problem and would appreciate some help.
My code:
public void newUser(int userID, String userName, String credentials) {
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager
.getConnection("jdbc:sqlite:c:/temp/alarmsystem.db");
Statement statement = conn.createStatement();
statement.execute("insert into Users values(" + 1 + "," + userName
+ "," + "'Helloskit'" + ") ");
core.printToConsole("created");
ResultSet rs = statement.executeQuery("select * from Users");
while (rs.next()) {
String s = rs.getString("Username");
core.printToConsole("name = " + s);
}
} catch (Exception e) {
}
}
The error:
java.sql.SQLException: no such column: Tombellens
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NestedDB.prepare(NestedDB.java:115)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.execute(Stmt.java:82)
at me.server.DBCommunications.DBConnection.newUser(DBConnection.java:59)
at me.server.Core.Core.newUser(Core.java:61)
at me.server.LocalUser.Console.main(Console.java:72)
Thanks, Tom
The problem is the in query. The userName
variable is not enclosed in quotes
Use below code:
statement.execute("insert into Users values(" + 1 + ",'" + userName + "'," + "'Helloskit'" +") ");
In your SQL statement, the variable userName should be in quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With