【发布时间】:2014-04-11 10:00:19
【问题描述】:
这里是代码
import java.sql.*;
public class Insertdb {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:Dsn1");
PreparedStatement ps= con.prepareStatement("insert into Table1 values (?,?,?)");
ps.setInt(1,1);
ps.setString(2,"Sachin");
ps.setInt(3,25000);
int i=ps.executeUpdate();
if(i>0)
{
System.out.println(i +"records inserted");
}
}
catch(Exception e)
{
System.out.println(e);
}
// TODO code application logic here
}
}
使用的数据库是 MS Access 2013。 上面代码在控制台的输出是:1条记录插入
但是当我打开数据库时,并没有插入记录。代码有什么问题吗?如果不是,会出现什么问题?
【问题讨论】:
-
可能你需要提交事务
-
如何提交事务?
-
关闭所有连接、结果集和语句,然后检查。
-
感谢@xwhyz。实际上我禁用了自动提交并使用了 con.commit()。它就像魅力一样。