简体   繁体   English

无法连接到MS Access数据库?

[英]Cannot connect to MS Access Database?

I am building an android app in eclipse and I cant connect to my MS Access database.I am using a code I found online, it was using JTBS but I didn't want my database to be on a server so I changed it to ODBC.(I am a beginner so keep in mind my mistake might be very basic or stupid) here is my code: 我正在eclipse中构建一个android应用程序,但无法连接到MS Access数据库。我使用的是在网上找到的代码,它使用的是JTBS,但是我不希望数据库位于服务器上,所以我将其更改为ODBC ((我是一个初学者,所以请记住我的错误可能是非常基本的或愚蠢的)这是我的代码:

import java.sql.*;

public final class Database 
{

public Connection connectionObj;
private Statement statement;
public static Database dataBase;
ResultSet res ;

Database() 
{
    String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Users/barw1_000/Desktop/Projects/SwinApp;";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String userName = "";
    String password = "";

    try 
    {

        Class.forName(driver).newInstance();
        this.connectionObj = (Connection)DriverManager.getConnection(url,userName,password);

    }
    catch (Exception sqle) 
    {
        sqle.printStackTrace();
    }
}
/**
 *
 * @return MysqlConnect Database connection object
 */
public static synchronized Database getDbCon() 
{
    if ( dataBase == null ) 
    {
        dataBase = new Database();
    }
    return dataBase;

}
/**
 *
 * @param query String The query to be executed
 * @return a ResultSet object containing the results or null if not available
 * @throws SQLException
 */
public ResultSet query(String query) throws SQLException
{
    statement = dataBase.connectionObj.createStatement();
     res = statement.executeQuery(query);
    return res;
}
/**
 * @desc Method to insert data to a table
 * @param insertQuery String The Insert query
 * @return boolean
 * @throws SQLException
 */
public int insert(String insertQuery) throws SQLException 
{
    statement = dataBase.connectionObj.createStatement();
    int result = statement.executeUpdate(insertQuery);
    return result;

}

} }

The error shown is: 显示的错误是:

Invalid layout of java.lang.String at value
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (javaClasses.cpp:126), pid=7136, tid=4712
#  fatal error: Invalid layout of preloaded class
#
# JRE version:  (8.0_25-b18) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode windows-amd64 compressed oops)
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\barw1_000\Desktop\Projects\SwinApp\SwimApp\hs_err_pid7136.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#

I also don't know how to use this database with the app so if you can give me any information about it, that would be really great. 我也不知道如何在应用程序中使用该数据库,因此,如果您可以向我提供有关它的任何信息,那将是非常不错的。

Your error says JRE version: (8.0_25-b18) indicating that you are using Java 8. The JDBC-ODBC Bridge was removed from Java 8 so you cannot use ODBC. 您的错误显示JRE version: (8.0_25-b18)表示您正在使用JRE version: (8.0_25-b18)已从Java 8中删除,因此您无法使用ODBC。

You might be able to use the UCanAccess JDBC driver (setup details here ). 您也许可以使用UCanAccess JDBC驱动程序( 此处的设置详细信息)。 UCanAccess uses HSQLDB as a backing database, keeping a duplicate copy of the Access database either in storage or in memory, so that may be a bit resource-intensive for an Android application. UCanAccess使用HSQLDB作为后备数据库,在存储或内存中保留Access数据库的重复副本,因此对于Android应用程序可能会占用一些资源。

A more lightweight solution would be to use Jackcess which updates the Access database file directly, but you would not be able to use SQL. 一个更轻量级的解决方案是使用Jackcess ,它可以直接更新Access数据库文件,但是您将无法使用SQL。 Instead, you would have to use the Jackcess API, which is very powerful but it is also very different from SQL. 相反,您将不得不使用Jackcess API,该API非常强大,但与SQL也有很大不同。

Finally, as mentioned in comments to your other (identical) questions, you might want to reconsider using an Access database file and use SQLite instead. 最后,如对其他(相同)问题的评论中所述,您可能想重新考虑使用Access数据库文件,而改用SQLite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM