开发者

Can't create SQL Statement - Java

Well writing a login checker for a game, and switching from C# to java. Problem is I am using older code as an exmaple but keep running into this problem with creating a statement from connection. I get "createStatement() symbol not found". Is there a new way to do SQL statements or is there something else wrong?

Here is the source code.

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");
        } cat开发者_JAVA百科ch(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();
    }
}


You should not reuse the name Connection for your own class. When you are declaring objects of type Connection, you aren't using the SQL one, you're using your own.

So, when you declare the variable con, you're declaring a tanksgrid.Connection, not a java.sql.Connection. The same for the cast. And when you call con.createStatement(), the class Connection that you wrote has no such method.

EDIT:

With the source update, there is still a lingering problem with the code: you will need to catch an SQLException possibly thrown by createStatement(). (Also, the method needs to return a boolean value.) Eclipse doesn't see any other problems with the source.


Naming conflict. The compiler thinks con refers to your own Connection class.

Either change the name of your class from Connection to MyConnection or something like that OR reference the con object as java.sql.Connection con; If you do this, also change the type cast from (Connection) to (java.sql.Connection)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜