从java向ms access插入数据

从java向ms access插入数据,java,sql,ms-access,jdbc,jdbc-odbc,Java,Sql,Ms Access,Jdbc,Jdbc Odbc,我是JDBC新手。我想从Java向Access中插入数据,但无法获取。它显示以下错误: Connection Established Successfully java.sql.SQLException: General error Could Not Connect to Database at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.

我是JDBC新手。我想从Java向Access中插入数据,但无法获取。它显示以下错误:

Connection Established Successfully  
java.sql.SQLException: General error  
Could Not Connect to Database  
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)  
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)  
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)  
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)  
    at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)  
    at DBConnect.<init>(DBConnect.java:22)  
    at DBConnect.main(DBConnect.java:32)  

您尚未正确安装MSAccess的驱动程序

例如,试着这样做

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // set this to a MS Access DB you have on your machine
   String filename = "d:/DB.accdb";
   String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
   database+= filename.trim() + ";DriverID=22;}"; // add on to the end 
   // now we can get the connection from the DriverManager
   Connection con = DriverManager.getConnection( database ,"",""); 
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into product(productID,description,quantity,price,discount) values(?,?,?,?,?)");
           pstmt.setString(1, productId);
           pstmt.setString(1, desc);
           //same for all statement
           pstmt.executeUpdate();
           pstmt.close();
还要确保在路径中有ODBC驱动程序的导入jar文件

更新:

像这样插入数据

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // set this to a MS Access DB you have on your machine
   String filename = "d:/DB.accdb";
   String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
   database+= filename.trim() + ";DriverID=22;}"; // add on to the end 
   // now we can get the connection from the DriverManager
   Connection con = DriverManager.getConnection( database ,"",""); 
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into product(productID,description,quantity,price,discount) values(?,?,?,?,?)");
           pstmt.setString(1, productId);
           pstmt.setString(1, desc);
           //same for all statement
           pstmt.executeUpdate();
           pstmt.close();

您尚未正确安装MSAccess的驱动程序

例如,试着这样做

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // set this to a MS Access DB you have on your machine
   String filename = "d:/DB.accdb";
   String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
   database+= filename.trim() + ";DriverID=22;}"; // add on to the end 
   // now we can get the connection from the DriverManager
   Connection con = DriverManager.getConnection( database ,"",""); 
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into product(productID,description,quantity,price,discount) values(?,?,?,?,?)");
           pstmt.setString(1, productId);
           pstmt.setString(1, desc);
           //same for all statement
           pstmt.executeUpdate();
           pstmt.close();
还要确保在路径中有ODBC驱动程序的导入jar文件

更新:

像这样插入数据

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  // set this to a MS Access DB you have on your machine
   String filename = "d:/DB.accdb";
   String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
   database+= filename.trim() + ";DriverID=22;}"; // add on to the end 
   // now we can get the connection from the DriverManager
   Connection con = DriverManager.getConnection( database ,"",""); 
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into product(productID,description,quantity,price,discount) values(?,?,?,?,?)");
           pstmt.setString(1, productId);
           pstmt.setString(1, desc);
           //same for all statement
           pstmt.executeUpdate();
           pstmt.close();

{Microsoft Access驱动程序(*.mdb,)}
不正确。它必须是
{Microsoft Access Driver(*.mdb)}
{Microsoft Access Driver(*.mdb,*.accdb)}
@GordThompson..抱歉,忘了写..,
{Microsoft Access Driver(*.mdb,*.accdb)}
也不正确。空格位于
.accdb
之前,而不是之后。另外,当练习的目标是在数据库上进行插入时,您认为
READONLY=true
是个好主意吗?@GordThompson。。当然不是好主意,但我正在尝试解决连接错误,不担心插入或选择?仍然没有解决问题。。它在线路连接con.上显示错误。。java.sql.SQLException:sun.jdbc.odbc.JdbcOdbc.createSQLException(未知源)在sun.jdbc.odbc.JdbcOdbc.standardError(未知源)在sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(未知源)在sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(未知源)在DBcon。位于DBcon.main(DBcon.java:34)的(DBcon.java:24)无法连接到数据库
{Microsoft Access Driver(*.mdb,)}
不正确。它必须是
{Microsoft Access Driver(*.mdb)}
{Microsoft Access Driver(*.mdb,*.accdb)}
@GordThompson..抱歉忘了写..,
{Microsoft Access Driver(*.mdb,*.accdb)}
也不正确。空格出现在
.accdb
之前,而不是之后。另外,当练习的目标是在数据库上进行插入时,您认为
READONLY=true
是个好主意吗?@GordThompson..当然不是个好主意,但我正在尝试解决连接错误,不关心插入或选择吗?仍然l未解决问题..它在线路连接con..java.sql.SQLException:在sun.jdbc.odbc.JdbcOdbc.createSQLException(未知源)处出现常规错误,在sun.jdbc.odbc.JdbcOdbc.standardError(未知源)处出现错误,在sun.jdbc.odbc.JdbcOdbc.JdbcOdbcStatement.execute(未知源)在DBcon.main(DBcon.java:34)的DBcon.(DBcon.java:24)的sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(未知源)无法连接到数据库