sinat_33676226 2017-08-30 11:19 采纳率: 0%
浏览 3247
已结题

java的preparedstatemen的setString(),setObject()匹配问题

关键代码如下:
String id =request.getParameter("id");
String password = request.getParameter("password");
Connection connection=null; PreparedStatement pStatement=null;

ResultSet rs = null;

try {

Class.forName("com.mysql.jdbc.Driver");
System.out.println("load Driver successfully");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","58546494"); String url="select * from users where id=? and passwd=?"; // jdbc:mysql://localhost:端口号/数据库名称","用户名","密码" pStatement=connection.prepareStatement(url); pStatement.setObject(1, id); pStatement.setString(2, password); rs=pStatement.executeQuery(); if(rs.next()){ request.getRequestDispatcher("/MainFrame").forward(request, response); System.out.println(id);数据库中的ID为整型,我传入其他类型服务器端不报错,重要的是ID只要前边的匹配了密码正确,就可以登陆成功,比如ID=1,密码=123,在界面输入ID 1SAFHj 这也可以登陆成功
图片说明
图片说明

  • 写回答

6条回答

  • 我也不知道该叫啥 2017-08-30 13:11
    关注

    /**

    The JDBC specification specifies a standard mapping from
    * Java Object types to SQL types. The given argument
    * will be converted to the corresponding SQL type before being
    * sent to the database.
    */
    这是javadoc文档中对于void setObject(int parameterIndex, Object x) throws SQLException;的部分注释

    JDBC规格指定了一个从Java Object类型到SQL类型的标准映射。指定的协议在发送给数据库之前可能被更改去响应SQL中的类型。
    所以你的用户名最后是传到的不一定就是你写的值。至于其他的更详细的内容你可以把setObject方法的注释都看完,我在这只说重要的部分

    放着void setInt(int parameterIndex, int x) throws SQLException;方法不用何必要为难自己呢

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)