JDBC 使用 PreparedStatement。用“?占位符出现的sql语法报错.为什么会出现报错,“?”使用正确吗,该如何解决
问题相关代码,请勿粘贴截图
package com.itheima.jdbc;
import util.JDBCUtils;
import java.sql.*;
import java.util.Scanner;
public class demo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名和密码:");
String UId = sc.nextLine();
String Ups = sc.nextLine();
boolean judge = demo4.load(UId,Ups);
if(judge)
System.out.println("登陆成功");
else
System.out.println("登陆失败");
}
public static boolean load(String UId,String Ups){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from ip where UId = ? and Ups = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,UId);
pstmt.setString(2,Ups);
rs = pstmt.executeQuery(sql);
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
}
报错信息
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and Ups = ?' at line 1
数据库内容,均为varchar类型