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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法