如何使用Java(NetBeans)编辑MS Access数据库

时间:2022-10-30 14:49:41

I'm trying to edit an MS Access database using some Java code (running NetBeans 7.2.1). I set up the data source and linked it to my database ProjectDatabase using the ODBC tool and named the data source DB, then i run the following code:

我正在尝试使用一些Java代码(运行NetBeans 7.2.1)编辑MS Access数据库。我设置数据源并使用ODBC工具将其链接到我的数据库ProjectDatabase并命名为数据源DB,然后运行以下代码:

import java.sql.*; public class NewMain {

import java.sql。*;公共课NewMain {

public static void main(String[] args) {
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con = DriverManager.getConnection("jdbc:odbc:DB");
       Statement st=con.createStatement();
       String name="roseindia";
       String address="delhi";
       int i=st.executeUpdate("insert into user(name,address)      values('"+name+"','"+address+"')");
       System.out.println("Row is added");
       }
    catch(Exception e){
        System.out.println(e);
    }
}
} 

The code runs without and error and returns the "Row is added" message. The problem is that when I go back to view the database the changes have not taken effect. I have tried this with a code for deleting the data, also to no effect. Has anybody had this problem and knows how to solve it?

代码运行时没有和错误,并返回“添加行”消息。问题是,当我返回查看数据库时,更改尚未生效。我试过这个用于删除数据的代码,也没有效果。有没有人有这个问题,知道如何解决它?

I'm running Windows 7 64-bit, Microsoft Office 64-bit with all the 64-bit drivers and I have been unable to find any mention of this problem through web searches.

我正在使用所有64位驱动程序运行Windows 7 64位,Microsoft Office 64位,我无法通过网络搜索找到任何关于此问题的提及。

Thanks in advance for any help =)

在此先感谢任何帮助=)

1 个解决方案

#1


2  

First of all you are not closing the connection, so that is one problem. Also change your code to:

首先,你没有关闭连接,所以这是一个问题。还要将代码更改为:

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("jdbc:odbc:DB");
   Statement st=con.createStatement();
   con.setAutoCommit(false); //Notice change here
   String name="roseindia";
   String address="delhi";
   int i=st.executeUpdate("insert into user(name,address)      values('"+name+"','"+address+"')");
   con.commit(); //Notice change here
   System.out.println("Row is added");
   con.close(); //Notice change here

This will commit the changes to access database, so now you should be able to see data in MS Access.

这将提交对访问数据库的更改,因此现在您应该能够在MS Access中查看数据。

Read here to know more about best practices for Closing and Releasing JDBC resources

阅读此处以了解有关关闭和释放JDBC资源的最佳实践的更多信息

#1


2  

First of all you are not closing the connection, so that is one problem. Also change your code to:

首先,你没有关闭连接,所以这是一个问题。还要将代码更改为:

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("jdbc:odbc:DB");
   Statement st=con.createStatement();
   con.setAutoCommit(false); //Notice change here
   String name="roseindia";
   String address="delhi";
   int i=st.executeUpdate("insert into user(name,address)      values('"+name+"','"+address+"')");
   con.commit(); //Notice change here
   System.out.println("Row is added");
   con.close(); //Notice change here

This will commit the changes to access database, so now you should be able to see data in MS Access.

这将提交对访问数据库的更改,因此现在您应该能够在MS Access中查看数据。

Read here to know more about best practices for Closing and Releasing JDBC resources

阅读此处以了解有关关闭和释放JDBC资源的最佳实践的更多信息