那么为游戏编写登录检查器,并从C#切换到java.问题是我使用旧代码作为例子,但是通过从连接创建语句来继续解决这个问题.我得到"找不到"createStatement()符号".是否有新的方法来执行SQL语句或是否有其他错误?
这是源代码.
import java.sql.*;
public class SQLCheck {
public Connection con;
public void connect(){
String host = "somewhereoutthere";
String username = "stuff";
String pass = "haclmolg";
String db = "users";
try{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://"+host+"/"+db+"?";
con = (Connection) DriverManager.getConnection(connectionUrl,username,pass);
System.out.println("Server: Connected to MYSQL");
} catch(SQLException e){
System.out.println("Server: SQL EXECEPTION: " + e.toString());
} catch (ClassNotFoundException cE){
System.out.println("Server: Class not found: " + cE.toString());
}
}
boolean checkLogin(String userName, String password){
boolean correct = false;
Statement s = con.createStatement();
}
}
Run Code Online (Sandbox Code Playgroud)
您不应该Connection为自己的类重用该名称.当您声明类型的对象时Connection,您没有使用SQL,您正在使用自己的.
因此,当您声明变量时con,您声明的是a tanksgrid.Connection,而不是a java.sql.Connection.演员也一样.当你打电话时con.createStatement(),Connection你写的课没有这样的方法.
编辑:
随着源代码的更新,代码仍然存在一个挥之不去的问题:你需要捕获一个SQLException可能被抛出的东西createStatement().(此外,该方法需要返回一个布尔值.)Eclipse没有看到源的任何其他问题.
| 归档时间: |
|
| 查看次数: |
1902 次 |
| 最近记录: |