我正在编写一个Java应用程序,它必须将记录添加到数据库中.一切正常,直到我想向数据库添加一个局部变量(我想我的括号错了或者其他东西).无论如何,我已经厌倦了寻找问题,并希望得到一些帮助.
我的代码:
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
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)
Run Code Online (Sandbox Code Playgroud)
谢谢,汤姆
hav*_*exz 17
问题是在查询中.该userName变量未包含在引号中
使用以下代码:
statement.execute("insert into Users values(" + 1 + ",'" + userName + "'," + "'Helloskit'" +") ");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16305 次 |
| 最近记录: |