gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:50:48 30 4
gpt4 key购买 nike

我正在尝试在我的 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com