我曾尝试连接到Access数据库,但无论我尝试什么,我都会遇到同样的错误:
UCAExc ::: 0.0给定文件不存在:
我一直在使用Android Studio v2.3的UCanAccess。我尝试过YouTube,但我到处搜索过,但没有代码片段正在运行。
这是否与我使用Windows 10有关,我必须将数据库放在一个特殊的文件夹中?
我在代码末尾包含了错误日志
package com.example.david.dvd2;
// * Created by David on 2017-04-14.
import java.io.StringWriter;
import java.sql.*;
import java.util.Enumeration;
import android.util.Log;
import org.apache.commons.lang.exception.ExceptionUtils;
public class TestConnection {
public static void CreateDBConection() {
Log.i("TESTConnection","Inside TestConnection [CreateDBConnection]");
try {
Statement st;
ResultSet re;
/*
Layout of the Access Database, a simple database with DVD-Titles
tblDVD Id, m,d,fldTitle, fldSweTitle, fldTime, fldComment, fktyp,
fkyear,
tblType Id, fldType
tblYear Id, fldYear
*/
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String dbFileName = "C:/db/dvd.mdb";
String database = "jdbc:ucanaccess://" + dbFileName + "";
Connection cn = DriverManager.getConnection(database, "", "");
st = cn.createStatement();
re = st.executeQuery("SELECT Id, fldYear FROM tblYear WHERE Id=35");
while (re.next()) {
String idoo = re.getString("Id");
String fldyearoo = re.getString("fldYear");
Log.i("Result from SQL query", "A selected Year with ID=35: " + fldyearoo);
}
}
catch(ClassNotFoundException | SQLException e)
{
Log.i("ERROR","Info:\n" + e.getMessage());
String s = ExceptionUtils.getStackTrace(e);
Log.i("ERROR","More detailed info\n" + s);
e.printStackTrace(System.err);
}
}
}
这是Err.log:
04-21 16:19:04.466 1693-1693/com.example.david.dvd2 I/TESTConnection: Inside TestConnection [CreateDBConnection]
04-21 16:19:04.554 1693-1700/com.example.david.dvd2 W/art: Suspending all threads took: 9.814ms
04-21 16:19:04.648 1693-1709/com.example.david.dvd2 I/art: Background sticky concurrent mark sweep GC freed 8515(456KB) AllocSpace objects, 9(288KB) LOS objects, 16% free, 2MB/3MB, paused 6.486ms total 34.758ms
04-21 16:19:04.680 1693-1709/com.example.david.dvd2 W/art: Suspending all threads took: 32.195ms
04-21 16:19:04.785 1693-1709/com.example.david.dvd2 W/art: Suspending all threads took: 22.748ms
04-21 16:19:04.897 1693-1693/com.example.david.dvd2 I/ERROR: Info:
UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
04-21 16:19:04.933 1693-1693/com.example.david.dvd2 I/BERROR: More detailed info
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:264)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26)
at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20)
at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.io.FileNotFoundException: given file does not exist: /C:/db/dvd.mdb
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:372)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:252)
at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:35)
at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:161)
at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:51)
at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:99)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26)
at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20)
at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err: at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:264)
04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:179)
04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:213)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.os.Looper.loop(Looper.java:135)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: Caused by: java.io.FileNotFoundException: given file does not exist: /C:/db/dvd.mdb
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:372)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:252)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:35)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:161)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:51)
04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:99)
答案 0 :(得分:0)
在Android Studio中的Android模拟器下运行的代码显然无法直接访问主机的文件系统。在这种情况下,Windows路径C:/db/dvd.mdb
被解释为Android路径/C:/db/dvd.mdb
因此我们需要将数据库文件导入到Android&#34;设备&#34;上的文件系统中。正如this answer中所述。