我试图插入新的记录,使用jdbc。一切看起来都没问题,我没有任何异常,但是新记录没有插入到表格中。 Select语句正确。JDBC插入语句不起作用
public Connection getConnection(){
Connection conn=null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url);
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void insertDish(String name, float mass, float price, String description,int pizzeria_id) {
String insertStr = "insert into \"Dish\"(name,mass,price,description,pizzeria_id) values("+"'"+name+"'"+", "+mass+", "+price+", "+"'"+description+"'"+", "+pizzeria_id+")";
Connection conn = getConnection();
try {
Statement sql = conn.createStatement();
sql.executeUpdate(insertStr);
sql.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
什么可能是错的?
如果你把你的SQL并尝试直接在数据库中运行,会发生什么? – hvgotcodes 2010-10-21 18:53:42
你确定没有例外吗? – 2010-10-21 18:53:54
Sql语句直接在db中添加新记录。没关系。它不适用于jdbc。 – sunny 2010-10-21 18:55:17