执行此程序后,数据库未获得更新

时间:2014-04-11 10:00:20

标签: java ms-access jdbc

这是代码

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条记录

但是当我打开数据库时,没有插入记录。代码中有什么问题吗?如果不是可能会出错?

3 个答案:

答案 0 :(得分:0)

任何从程序到数据库的事务都必须提交以反映在JAVA中。 尝试做con.commit();

答案 1 :(得分:0)

也在最后添加。

finally{
    con.commit();
    ps.close();
    con.close();
    }

答案 2 :(得分:0)

如果要连接数据库或执行文件操作,请尝试在代码中使用finally。因为在try块上发生的任何事情始终执行finally块。所以你可以使用finally块来关闭数据库连接并关闭文件并提交或者回滚数据库记录。

相关问题