android中的ClassNotFoundException Class.forName()

时间:2014-07-21 09:03:46

标签: java android database sqljdbc

try {
    String url = "jdbc:sqlserver://Aman\\SQL12;databaseName=P2C_Android";
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn = DriverManager.getConnection(url, "sa", "aman");
    String sql =
        "select * from UserTable " +
        "where username='" + username.getText().toString() +
        "' and password='" + password.getText().toString() + "'";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    if (rs != null && rs.next())
        Toast.makeText(getApplicationContext(), "Redirecting...",
            Toast.LENGTH_SHORT).show();
    else {
        Toast.makeText(getApplicationContext(), "Wrong Credentials",
            Toast.LENGTH_SHORT).show();
        attempts.setBackgroundColor(Color.RED); 
        counter-- ;
        attempts.setText(Integer.toString(counter));
        if(counter == 0)
            login.setEnabled(false);
    }

}
catch (Exception ex) {
    Toast.makeText(getApplicationContext(),
        ex.getMessage(), Toast.LENGTH_SHORT).show();
}

即使我可以看到我添加的sqljdbc.jar中有班级,也说找不到班级。
我试图从Android应用程序访问数据库。

1 个答案:

答案 0 :(得分:0)

确保您只有一个版本的JAR。

对于ex-

您可以在类路径中同时拥有sqljdbc.jarsqljdbc4.jar