简体   繁体   中英

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:

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.

You might be able to use the UCanAccess JDBC driver (setup details here ). 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.

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. Instead, you would have to use the Jackcess API, which is very powerful but it is also very different from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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