java - java JDBC +mysql数据库更新查询

标签 java mysql sql jdbc

我正在尝试对 table doctors 运行更新查询。表的主键定义为复合主键(deptid、docid)。我要做的是根据 deptid 和 docid(通过另一个查询)更新字段名称、资格和时间。 我相信我正在做一些非常愚蠢的事情,但我找不到它。有人可以帮忙吗?

String did= request.getParameter("text1");
String dname = request.getParameter("text2");
String desig = request.getParameter("text3");
String qualification = request.getParameter("text4");
String time = request.getParameter("text5");

String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.13";
String user = "root";
String password = "";

PreparedStatement ps;
ResultSet rs;

try {
    Class.forName(className);                
    Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/webhospital","root","");
    //        PreparedStatement prepStmt = (PreparedStatement) conn.prepareStatement("Select * from tbl_userinfo");
    ps = (com.mysql.jdbc.PreparedStatement) con.prepareStatement("update doctors set Designation=?,Qualification=?,Time= ? where deptid =? and docid IN(select docid from doctors where doctorname='dname';)");
    ps.setString(1, did);
    ps.setString(3,desig);
    ps.setString(4,qualification);
    ps.setString(5,time);
    ps.executeUpdate();
}  catch (ClassNotFoundException cx) {
    out.println(cx);
} catch (SQLException ex) {
    Logger.getLogger(MysqlInsertServlet.class.getName()).log(Level.SEVERE, null, ex);
}

最佳答案

 ps = (com.mysql.jdbc.PreparedStatement) con.prepareStatement("update doctors set Designation=?,Qualification=?,Time= ? where deptid =? and docid IN(select docid from doctors where doctorname='dname';)");
            ps.setString(1, did);
            ps.setString(3,desig);
            ps.setString(4,qualification);
            ps.setString(5,time);

你有 4 个问号,但设置的顺序错误,为什么你不这样设置:

ps.setString(1, desig);
                ps.setString(2,qualification);
                ps.setString(3,time);
                ps.setString(4,deptId);

Supplying Values for PreparedStatement Parameters

You must supply values in place of the question mark placeholders (if there are any) before you can execute a PreparedStatement object. Do this by calling one of the setter methods defined in the PreparedStatement class. The following statements supply the two question mark placeholders in the PreparedStatement named updateSales:

updateSales.setInt(1, e.getValue().intValue()); updateSales.setString(2, e.getKey());

The first argument for each of these setter methods specifies the question mark placeholder. In this example, setInt specifies the first placeholder and setString specifies the second placeholder.

关于java - java JDBC +mysql数据库更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314927/

相关文章:

Java 程序混淆了字符串变量

触发事件时的 Java 同步

php - 使用php/SQL从数据库查询信息

mysql - mysql 5.6安装文件夹中没有my.ini文件

mysql 编译器。使用 with 并插入它会提示语法错误

mysql - ServiceNow SNC - 查找基础报表 SQL

SQL查询最大日期和其他表中的一些字段

java - 使用 java play 框架的 Integration Express Checkout 问题

mysql - 未找到列 : 1054 Unknown column '' 1'' in 'field list'

java - 如何让 servlet 中的 System.out.println 显示在控制台上?