使用java将数据插入表中

时间:2016-06-02 07:36:02

标签: java mysql jsp

我想将用户相关信息插入表(用户),当我创建表单时,我使用了下拉列表。我的数据库中有两个表(用户和部门)。我想显示名称下拉列表中的表部门的所有部门(以便用户选择该部门名称)并插入表用户。我该怎么做?

文件名-c_user.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>create</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <h1>Create User</h1>
    <br>
    <h2><a href='logout.jsp'>Log out</a></h2>
    <br>
    <h3><a href='success.jsp'>Go Back</a></h3>
    <form method="post" action="registration.jsp">
        <center>
        <table border="1" width="30%" cellpadding="3">
            <thead>
                <tr>
                    <th colspan="2">Login Here</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>User Name :</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>User Id :</td>
                    <td><input type="text" name="uid" value="" /></td>
                </tr>
                <tr>
                    <td>Department :</td>
                    <td><select name="departments">
                            <option></option>
                            <option></option>
                        </select></td>
                </tr>
                <tr>
                    <td>Email Id :</td>
                    <td><input type="text" name="email" value="" /></td>
                </tr>
                <tr>
                    <td>Mobile No. :</td>
                    <td><input type="text" name="mobile" value="" /></td>
                </tr>
                <tr>
                    <td>Password :</td>
                    <td><input type="password" name="pass" value="" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login" /></td>
                    <td><input type="reset" value="Reset" /></td>
                </tr>

            </tbody>
        </table>
        </center>
    </form>
 </body>
</html>

file name-registration.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%@ page import ="java.sql.*" %>
<%
String user = request.getParameter("uname");  
String id = request.getParameter("uid");
String email = request.getParameter("email");
String mobile = request.getParameter("mobile");    
String pwd = request.getParameter("pass");   
String department="";


Class.forName("com.mysql.jdbc.Driver");
Connection con =    DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
        "root", "root");
Statement st = con.createStatement();
//ResultSet rs;
int i = st.executeUpdate("insert into users(uname, id, department, email,   mobile, pass) values ('" + user + "','" + id + "','" + department + "','" +   email + "','" + mobile + "','" + pwd + "')");
if (i > 0) {
    //session.setAttribute("userid", user);
    response.sendRedirect("welcome.jsp");
   // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to   Login</a>");
} else {
    response.sendRedirect("c_user.jsp");
}
%>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

<select>
<%
         ResultSet rs1=state.executeQuery(" select department from your table");
                             while(rs.next()){

                            %>

          <option value="<%rs.getString(1)"%>><%rs.getString(1)"%></option>


    <%}%>
</select>
try this code 

答案 1 :(得分:0)

使用ajax调用在ui中显示部门名称并将其插入到用户表

答案 2 :(得分:0)

首先将department中的列departmentid更改为users,并在users表中创建外键并使用department表作为参考表< / p> 然后呢 只需在您的网页c_user.jsp

上执行此代码即可
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>create</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <h1>Create User</h1>
        <br>
        <h2><a href='logout.jsp'>Log out</a></h2>
        <br>
        <h3><a href='success.jsp'>Go Back</a></h3>
        <form method="post" action="registration.jsp">
            <center>
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name :</td>
                        <td><input type="text" name="uname" value="" /></td>
                    </tr>
                    <tr>
                        <td>User Id :</td>
                        <td><input type="text" name="uid" value="" /></td>
                    </tr>
                    <tr>
                        <td>Department :</td>
                        <td><select name="departments">
    <%try{
    String sql="select * from department";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
            "root", "root");
    Statement st = con.createStatement();
    ResultSet rs=st.executeQuery(sql);
    while(rs.next()){
    %>                          
      <option value="<%=rs.getInt("departmentid")%>"><%=rs.getString("departmentname")%></option>
<%}
    rs.close();
    st.close();
    con.close();
    }catch(Exception e){
    e.printStackTrace();
    }%>
                              </select></td>
                    </tr>
                    <tr>
                        <td>Email Id :</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>Mobile No. :</td>
                        <td><input type="text" name="mobile" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password :</td>
                        <td><input type="password" name="pass" value="" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Login" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>

                </tbody>
            </table>
            </center>
        </form>
     </body>
    </html>

然后提交后这个departmentid将存储在users表中,不要错过更改插件的查询  在registration.jsp

中更改插入查询
insert into users(uname, id, departmentid, email,   mobile, pass) values ('" + user + "','" + id + "','" + departmentid + "','" +   email + "','" + mobile + "','" + pwd + "')