Moncel 2022-08-10 17:21 采纳率: 86.7%
浏览 58
已结题

关于JDBC 使用 PreparedStatement。用“?占位符出现的sql语法报错的问题,如何解决?

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类型

img

  • 写回答

1条回答 默认 最新

  • Tomshidi 2022-08-10 17:35
    关注

    所以说,你的报错是啥?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月19日
  • 已采纳回答 8月11日
  • 修改了问题 8月10日
  • 创建了问题 8月10日