java - 带有 JDBC 的 Android。 createStatement() 不存在?

标签 java android android-studio jdbc

我正在尝试在我的 android 项目上使用 JDBC 创建与数据库的连接。我正在遵循一个教程,该教程说导入一个 jar,然后在 Activity 中创建一个连接。一切正常,但在连接语句中我收到错误

Cannot resolve method 'createStatement()'

如果我尝试进入该方法的引用,它会显示

cannot find decleration to go to

这个方法来 self 导入的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

最佳答案

您所要做的就是完整地指定类名:

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

不需要强制转换,您报告的错误现在将消失,因为编译器将在正确的类中寻找方法。

或者只是重命名您自己的类Connection

关于java - 带有 JDBC 的 Android。 createStatement() 不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43856566/

相关文章:

java - 如何在java中使用ArrayList制作XY折线图

android - "Your content must have a ListView whose id attribute is ' android.R.id.list '"当从ListActivity变为 View 时

android - 更新到 Android Studio Bumblebee 2021.1.1 后,Android Studio 中的 Gradle 项目同步出错

java - 错误: int cannot be dereferenced in java

java - JPanel、JFrame、BoxLayout 问题

android - 如何使用 Appcompat v7 21、Toolbar 和 DrawerLayout 将 Burger 动画化为箭头

android - 为android编译C库

java - 位置需要许可

java - 如何将行中的结构字段转换为 Spark Java 中的 avro 记录

android - Nexus 6 的正确屏幕尺寸和密度配置是多少?