2016-04-14 330 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

相關問題