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

Update query for java JDBC +mysql database

我正在尝试 运行 更新对 table 医生的查询。 table 的主键定义为复合主键(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);
}

您的查询需要进行的一项更改是

"where doctorname='dname';)" ==>> "where doctorname='"+dname+"';)"

 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.

我认为在不编辑您的代码的情况下向您展示一个简单的示例会很好。

    PrintWriter out = response.getWriter();
    String Title = request.getParameter("Title");
    String Artist = request.getParameter("Artist");
    String Country = request.getParameter("Country");
    String price = request.getParameter("price");
    String Year = request.getParameter("Year");

    try {
        //loading driver 

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

        //creating connection with the database 
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");

        PreparedStatement ps = con.prepareStatement("update COMPACT_DISK set TITLE=?,ARTIST=?,COUNTRY=?,PRICE=?,YEARS=? where TITLE=?");

        ps.setString(1, Title);
        ps.setString(2, Artist);
        ps.setString(3, Country);
        ps.setString(4, price);
        ps.setString(5, Year);
        ps.setString(6, Title);
        int i = ps.executeUpdate();
        if (i > 0) {
            out.println("Compact disk successfully inserted");
        }
    } catch (Exception se) {
        out.println("Error Occured : \n" + se.getLocalizedMessage());
        se.printStackTrace();
    }