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;方法不用何必要为难自己呢

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料