JDBC在Android模拟器中工作但在真实设备上没有?

时间:2018-05-09 21:55:12

标签: java android jdbc

我正在使用Android Studio开发一个使用JDBC驱动程序连接到Oracle数据库的Android应用程序。

问题:

在Android Emulator(API 26)上编译和运行时工作正常。但是当我尝试在真实设备(API 19)上运行它时,它会崩溃。

当调用Class.forName("oracle.jdbc.driver.OracleDriver")导致ClassNotFoundException时,它会崩溃。

代码:

public class LiveDB {
String driver = "oracle.jdbc.driver.OracleDriver";
String serverName = "192.168.x.x";
String portNumber = "1521";
String db = "orcl";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + db;
String user = "someuser";
String pwd = "somepass";
Connection con = null;

private void openConnection(){
    if(con != null) return;
    try {
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url, user, pwd);
    } catch (Exception e) {
        e.printStackTrace();
        con = null;
    }
}

private void closeConnection(){
    if(con == null) return;
    try{
        con.close();
    }
    catch (Exception e){
        e.printStackTrace();
        con = null;
    }
}

public ArrayList<Student> searchStudents(String input){
    ArrayList<Student> students = new ArrayList<>();
    openConnection();
    try {
        String query = Student.getQuery(input);
        Statement statement = con.createStatement();
        ResultSet set = statement.executeQuery(query);
        while (set.next()) {
            //Getting data from set and show it.
        }
    }
    catch (Exception e){
        e.printStackTrace();
        con = null;
    }
    closeConnection();
    return students;
}
}

0 个答案:

没有答案