您好我正在尝试做一个Android应用程序,我需要连接到外部数据库但是当我加载jdbc驱动程序时,系统给我一个错误,这是代码,错误是在Class.forName( " com.mysql.jdbc.Driver&#34); 我查看了jdbc驱动程序的路径,但它是正确的,任何想法?
private void connDB(String username, String password, Intent intent){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://ip/dbname?", "username", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM id");
rs = stmt.getResultSet();
if (stmt.execute("SELECT * FROM id")) {
rs = stmt.getResultSet();
}
while (rs.next()) {
//get id
id1 = rs.getInt("id");
id.add(id1);
//get name
buff = rs.getString("id");
user_name.add(buff);
//get password
buff = rs.getString("id");
user_password.add(buff);
}
imax = id.size();
for(i=0; i<imax; i++){
if(MainClass.username.equals(user_name.get(i)) && MainClass.password.equals(user_password.get(i))){
startActivity(intent);
idUser = id.get(i);
break;
}
else{
System.out.println("error");
}
}
}
catch (Exception ex){
System.out.println("SQLException: " + ex.getMessage());
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:309)
at java.lang.Class.forName(Class.java:273)
所有都是系统错误
答案 0 :(得分:-3)
试试这个:
Class.forName("com.mysql.jdbc.Driver").newInstance();