简体   繁体   English

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

[英]Update query for java JDBC +mysql database

I'm trying to run update query on table doctors. 我正在尝试对医生进行更新查询。 The primary key of the table is defined as a composite primary key (deptid, docid). 该表的主键定义为复合主键(deptid,docid)。 What I'm trying to do is to update field designation, qualification and time based on deptid and docid (by another query). 我想做的是根据deptid和docid(通过另一个查询)更新字段名称,资格和时间。 I believe I'm doing something very silly but I'm not able to find it. 我相信我做的事很愚蠢,但我找不到。 Can someone help? 有人可以帮忙吗?

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);

You have 4 question mark but set in wrong order why you don't set like : 您有4个问号,但设置顺序错误,为什么您没有这样设置:

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

Supplying Values for PreparedStatement Parameters 为PreparedStatement参数提供值

You must supply values in place of the question mark placeholders (if there are any) before you can execute a PreparedStatement object. 在执行PreparedStatement对象之前,必须提供值代替问号占位符(如果有)。 Do this by calling one of the setter methods defined in the PreparedStatement class. 通过调用PreparedStatement类中定义的setter方法之一来执行此操作。 The following statements supply the two question mark placeholders in the PreparedStatement named updateSales: 以下语句在PreparedStatement中提供了两个名为updateSales的问号占位符:

updateSales.setInt(1, e.getValue().intValue()); updateSales.setInt(1,e.getValue()。intValue()); updateSales.setString(2, e.getKey()); 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. 在此示例中,setInt指定第一个占位符,setString指定第二个占位符。

One change required in your query is 查询中需要进行的一项更改是

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

I think without editing your code it is good to show you an simple example. 我认为不编辑代码就可以向您展示一个简单的示例。

    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM