简体   繁体   English

从表中选择并插入另一个表

[英]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. 我试图从MYSQL数据库选项卡中选择一些数据,然后将它们插入另一个MYSQL数据库表中。

This is my servlet code 这是我的servlet代码

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. 这是我的Java代码来选择和插入数据。

 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. 而且,不需要while(rs.next)循环。 you can use if(rs.next) condition if you are sure that you be passing a unique name in the request. 如果您确定要在请求中传递唯一名称,则可以使用if(rs.next)条件。

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;

} }

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

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