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