【发布时间】:2010-05-27 12:36:52
【问题描述】:
我有一个 Java 应用程序,我想使用 SQL 数据库。我有一个连接类:
public class SQLConnection{
private static String url = "jdbc:postgresql://localhost:5432/table";
private static String user = "postgres";
private static String passwd = "toto";
private static Connection connect;
public static Connection getInstance(){
if(connect == null){
try {
connect = DriverManager.getConnection(url, user, passwd);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Connection Error", JOptionPane.ERROR_MESSAGE);
}
}
return connect;
}
}
现在,在另一个类中,我成功打印了我的值,但是当我尝试插入一个值时,什么都没有发生......
这是我的代码:
try {
Statement state = SQLConnection.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
Statement state2 = SQLConnection.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
state2.executeUpdate("INSERT INTO table(field1) VALUES (\"Value\")"); // Here's my problem
ResultSet res = state.executeQuery("SELECT * FROM table");
【问题讨论】:
-
我想知道您为什么以及如何将其标记为
prepared-statement而您根本没有在代码中使用它(您应该但是这样做)。
标签: java sql jdbc prepared-statement