2016-04-14 324 views
0
package jdbcDemo; 
import java.sql.*; 

public class PreparedStatement { 

     public static void main(String[] args){ 
      // using SQL to insert,edit,delete information from a database 
      Connection myConn =null; 
      PreparedStatement myStmt=null; 
      ResultSet myRs=null; 

     try{ 
      // 1.make a connection to database 
      myConn= DriverManager.getConnection("jdbc:mysql://localhost:3306/shopping_cart","root","1578"); 

      //2.create a statement 
      myStmt= myConn.prepareStatement("SELECT * FROM shopping_cart.customers where total_cost>? and items_number>?"); 

      //3.set parameters 
      myStmt.setDouble(1,2); 
      myStmt.setDouble(2,40);    
      myRs=myStmt.executeQuery(); 
     } 
     catch(Exception exc){ 
      exc.printStackTrace(); 
     } 

     } 

在步骤2,我想创建一个事先准备好的声明,但有一个“类型不匹配:不能从java.sql.PreparedStatement中转换到jdbcDemo。PreparedStatement“。我GOOGLE了它,大部分答案都说我应该导入java.sql。*,然后我仍然得到了答案。类型不匹配:不能从java.sql.PreparedStatement中转换为jdbcDemo.PreparedStatement

+1

重命名你的类'PreparedStatement',它屏蔽了该''java.sql.PreparedStatement' import'。或者使用完全限定的类名'java.sql.PreparedStatement myStmt = null;'。 –

回答

2

你定义了你自己的PreparedStatement,这让编译器感到困惑。

更改类的名称或更换

PreparedStatement myStmt=null; 

java.sql.PreparedStatement myStmt=null; 
+0

谢谢!有用! – Doris

相关问题