我正在使用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;
}
}