簡體   English   中英

使用Java將數據插入表中

[英]Inserting data into table using java

我想在表格中插入與用戶相關的信息,創建表格時我使用了下拉列表。我的數據庫中有兩個表格(用戶和部門)。我想顯示所有部門的名稱下拉列表中表部門的名稱(以便用戶選擇該部門名稱)並插入表用戶中。 我應該怎么做?

文件名-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>

文件名-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>
<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 

使用ajax調用在ui中顯示部門名稱,並將合適的部門名稱插入到用戶表中

首先改變列的departmentdepartmentidusers表,使外鍵users表,並利用department表作為參考表

然后只需在頁面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 + "')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM