android与JDBC。 createStatement()不存在?

时间:2017-05-08 20:15:01

标签: java android android-studio jdbc

我正在尝试使用我的android项目上的JDBC创建与数据库的连接。我正在按照一个教程说明导入jar,然后在活动中创建一个连接。一切都很好,但在连接语句中我收到错误

  

无法解析方法'createStatement()'

如果我尝试进入该方法的参考,它会说

  

无法找到取消去

这个方法来自我导入的jar? (JTDS-1.3.1) 我还缺少什么吗?

 @Override
    protected String doInBackground(String... params)
    {
        Connection con;
        String usernam = params[0];
        String passwordd = params[1];
        if(usernam.trim().equals("")|| passwordd.trim().equals(""))
            z = "Please enter Username and Password";
        else
        {
            try
            {
                con = connectionclass(un, pass, db, ip);        // Connect to database
                if (con == null)
                {
                    z = "Check Your Internet Access!";
                }
                else
                {
                    // Change below query according to your own database.
                    String query = "select * from owner where mail = '" + usernam.toString() + "' and password = '"+ passwordd.toString() +"'  ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        isSuccess=true;
                        con.close();
                    }
                    else
                    {
                        z = "Invalid Credentials!";
                        isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = ex.getMessage();
            }
        }
        return z;
    }
}


@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try
    {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";
        connection = (Connection) DriverManager.getConnection(ConnectionURL);
    }
    catch (SQLException se)
    {
        Log.e("error here 1 : ", se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
        Log.e("error here 2 : ", e.getMessage());
    }
    catch (Exception e)
    {
        Log.e("error here 3 : ", e.getMessage());
    }
    return connection;
}

我在close()方法遇到了同样的问题 Here is a screenshot

1 个答案:

答案 0 :(得分:2)

您要做的就是完全指定班级名称:

java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product","root","");

不需要演员,你报告的错误现在就会消失,因为编译器会在正确的类中寻找方法。

或者只是重命名自己的班级Connection