有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于更新的java my sql语句不起作用

我无法执行update语句。我的更新声明似乎有些问题

我现在正在做的是,我正在尝试更新重复的电子邮件地址,并用非重复的电子邮件替换(更新)。因此,为了更新,我的SQL语句将根据输入检查我的数据库,如果((firstname && lastname && dateofbirth) || (phoneNo && address)与用户输入匹配,它将更新数据库;也就是说:用不重复的电子邮件更新重复的电子邮件地址,然后在执行更新后删除所有重复的电子邮件

然而,我无法找出问题所在。以下是我的最新声明:

 try {
         System.out.println("it came here where filepart!=null");
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
            // connects to the database
            conn = getConnection();

            // constructs SQL statement
          stmt = conn.createStatement();

           //sqll should be update


          String sql1 ="UPDATE registration SET emailAddress = ? where ((firstName = ? && lastName= ? && dateOfBirth= ?) || (phoneNo= ? && address= ?))" ;

            //Using a PreparedStatement to save the file
            PreparedStatement statement1 = conn.prepareStatement(sql1);
            System.out.println(firstName+"firstname");
            statement1.setString(1, firstName);
            statement1.setString(2, lastName);
          statement1.setString(3, dateOfBirth); 
            statement1.setString(4, phoneNo);
          statement1.setString(5, address);
       statement1.executeUpdate();
       statement1.close();
     String sql2="delete registration from registration inner join (select min(userId) as lastId, emailAddress from registration where emailAddress in ( select emailAddress  from registration group by emailAddress having count(*) > 1) group by emailAddress) duplic on duplic.emailAddress = registration.emailAddress where registration.userId > duplic.lastId";

   stmt.executeUpdate(sql2);



            //sends the statement to the database server

           // if (row > 0) {

             // getServletContext().getRequestDispatcher("/Message.jsp").include(request, response);
              //  message = "You have successfully registered.";
            //}


        } catch (SQLException ex) {
        //  message = "You have failed to registered. Please try again.";
         //   ex.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    // ex.printStackTrace();
                    //silent
                    //message="You have failed to log in";
                     getServletContext().getRequestDispatcher("/FailedMsg.jsp").include(request, response);
                }
            }      
          rs.close();
            ps.close();
            conn.close();
        }}
 }

共 (0) 个答案