简体   繁体   English

我需要调用数据库的步骤是什么

[英]what is the step do i need to call database

i am new in developing android application. 我是开发Android应用程序的新手。 Now, i have a database file which is "database.accdb" in assets folder. 现在,我在资产文件夹中有一个数据库文件“ database.accdb”。 what should i do inside the java code in order to access the database 我应该在Java代码中做什么才能访问数据库

UPDATE I just realized that you're writing an Android application. 更新我刚刚意识到您正在编写一个Android应用程序。 ODBC or Microsoft Access database will NOT work in Android. ODBC或Microsoft Access数据库将无法在Android中使用。 SQLite is used in Android, you have to subclass SQLiteOpenHelper , and override onCreate() , and onUpgrade() . SQLite在Android中使用,您必须SQLiteOpenHelper子类,并重写onCreate()onUpgrade() Below is the direction of how to access Microsoft Access database on a regular Java program. 下面是如何在常规Java程序上访问Microsoft Access数据库的方向。


Looks like you're trying to access a Microsoft Access database. 似乎您正在尝试访问Microsoft Access数据库。 In this case, you need to instantiate the JDBC-ODBC driver by 在这种情况下,您需要通过以下方式实例化JDBC-ODBC驱动程序:

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String connectionUri = "jdbc:odbc:" + /*PATH TO YOUR FILENAME*/;
    connection = DriverManager.getConnection(connectionUri, username, password);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Then you can use your connection like you use any other JDBC database connection. 然后,您可以像使用其他任何JDBC数据库连接一样使用连接。 For example, you can do: 例如,您可以执行以下操作:

Connection connection = .... /* Get my connection */;
try {
    PreparedStatement ps = connection.prepareStatement(
            "SELECT id, password FROM users WHERE email LIKE ?");
    ps.setString(1, email);
    ResultSet result = ps.executeQuery();
    while (result.next()) {
        /* do whatever you want with the result */
    }
} catch (SQLException ex) {
    ex.printStackTrace();
} finally {
    /* Close the connection */
}

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

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