简体   繁体   中英

Select from table and insert into another table

I tried to select some data from a MYSQL database tab and insert them into another MYSQL database table.

This is my servlet code

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);

    PrintWriter out = response.getWriter();

    String name = request.getParameter("name");

    ArrayList al = null;

    int size = 0;
    size = AcceptDao.getData(name);

    if (size>0) {
        out.println("<script type=\"text/javascript\">");
        out.println("alert('Successfully Employee Added');");
        out.println("</script>");
    } else {
        out.println("<script type=\"text/javascript\">");
        out.println("alert('Try Again');");
        out.println("</script>");
    }
}

and here is my java code to do select and insert data.

 public static int getData(String Uname) {
    ArrayList al = null;
    int status = 0;
    String name = null;
    String role = null;
    String pass = null;
    try {
        Connection con = ConnectionProvider.getCon();
        String query = "SELECT noty_name,noty_user_role,noty_pass FROM notification WHERE noty_name='" + Uname + "'";

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        al = new ArrayList();

        while (rs.next()) {
            name = rs.getString("noty_name");
            role = rs.getString("noty_user_role");
            pass = rs.getString("noty_pass");

        }

            PreparedStatement ps = con.prepareStatement("INSERT INTO employee (name,user_role,pass) values(?,?,?)");
            ps.setString(1, name);
            ps.setString(2, role);
            ps.setString(3, pass);
            status = ps.executeUpdate();
    } catch (Exception e) {
    }
    return status;
}

this isn't work properly. this code doesn't do select and insert. What is the wrong with this code.

Please note that the code part for "insert" is outside the loop. So only the last data in the loop will be inserted, if available. Moreover there is no need of while(rs.next) loop. you can use if(rs.next) condition if you are sure that you be passing a unique name in the request.

while (rs.next()) {
   name = rs.getString("noty_name");
   role = rs.getString("noty_user_role");
   pass = rs.getString("noty_pass");    
}
/* The Below part is wrong */
PreparedStatement ps = con.prepareStatement("INSERT INTO employee (name,user_role,pass) values(?,?,?)");
ps.setString(1, name);
ps.setString(2, role);
ps.setString(3, pass);
status = ps.executeUpdate();

Instead use this below code

public static int getData(String Uname) {
ArrayList al = null;
int status = 0;
String name = null;
String role = null;
String pass = null;
try {
    Connection con = ConnectionProvider.getCon();
    String query = "SELECT noty_name,noty_user_role,noty_pass FROM notification WHERE name='" + Uname + "'";

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);
    al = new ArrayList();

    PreparedStatement ps = con.prepareStatement("INSERT INTO employee (name,user_role,pass) values(?,?,?)");

    while (rs.next()) {
        name = rs.getString("noty_name");
        role = rs.getString("noty_user_role");
        pass = rs.getString("noty_pass");

        ps.setString(1, name);
        ps.setString(2, role);
        ps.setString(3, pass);
        status = ps.executeUpdate();
    }


} catch (Exception e) {
}
return status;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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