无法从java.sql.Statement转换为DataBaseConnection.Statement

时间:2014-01-11 14:49:36

标签: java mysql database database-connection

我有这个问题eclipse的quickfix建议我为这一行添加一个演员阵容:

state=conex.createStatement();

以及此行的另一个演员:

result= state.executeQuery("SELECT * FROM itshop.products");

在输出中我没有收到任何东西,当我进行演员时错误消失,但我没有任何输出。你能检查我是否正确连接到我的mysql数据库?

代码:

import java.sql.*;

public class Statement {

      public static void main(String[] args) {
          Connection conex=null;
          Statement state=null;
          ResultSet result=null;
          try
          {
              Class.forName("com.mysql.jdbc.Driver");
              conex=DriverManager.getConnection("jdbc:mysql://localhost:3306/itshop","user","pass");
          }
          catch(Exception e){
              e.printStackTrace();
          }
          try
          {
              System.out.println();
              state=conex.createStatement();
              result= state.executeQuery("SELECT * FROM itshop.products");
              result.next();
              int RowCount=result.getRow();
              System.out.println("Rows Number Are:"+RowCount);
          }
          catch(Exception e){
              e.printStackTrace();
          }

      }
}

并且当我没有为上面的行进行演员时显示错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Type mismatch: cannot convert from java.sql.Statement to      DataBaseConnection.Statement
    The method executeQuery(String) is undefined for the type Statement

    at DataBaseConnection.Statement.main(Statement.java:22)

2 个答案:

答案 0 :(得分:2)

问题是您班级的名称。有两个Statement类:

  1. java.sql.Statement
  2. DataBaseConnection.Statement
  3. 尝试将状态变量声明为java.sql.Statement的类型:

    public class Statement {
    
          public static void main(String[] args) {
              Connection conex=null;
              java.sql.Statement state=null;
              ResultSet result=null;
              ....
              ....
          }
    }    
    

    否则将您的类重命名为Statement

    以外的其他内容

答案 1 :(得分:0)

您的班级名称不应为声明。您必须更改类名,或者可以将语句对象声明为java.sql.Statement state = null;