J2EE 3.10

用户登录 增删改查

main.html

 <frameset rows="20%,*">
 <frame src="top.html" name="top"></frame>
 <frameset cols="20%,*">
  <frame src="left.html" name="left"/>
  <frame src="right.html" name="right"/>
 </frameset>
</frameset>

top.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>     <title>top.html</title>       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">     <meta http-equiv="description" content="this is my page">     <meta http-equiv="content-type" content="text/html; charset=UTF-8">         <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>     <body>     This is top page. <br>   </body> </html>

left.html

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link href="css/amcss.css" rel="stylesheet" type="text/css"> <script language="JavaScript"> function show(i){     if (i.style.display == "none") {       i.style.display = "";      }else{       i.style.display = "none";      }     }      </script>

</head>

<body > <table width="162" border="0" cellpadding="0" cellspacing="1">   <tr>     <td valign=tip> <!--=============--> <table width="100%" border="0" align=center cellpadding="0" cellspacing="1">   <tr>           <td height=27 background="images/title.gif" style="cursor:hand" onClick="show(a1)">             <div align="center"><strong>用户管理</strong>             </div>            </td>              </tr>   <tr>     <td> <DIV id=a1 style="DISPLAY: none">     <div align="left">       <table width="156" border="0" cellspacing="1" cellpadding="1" align="center">           <tr><td height=23 class="amrow"><a href=add-users.html target="right">添加用户</a></td></tr>           <tr><td height=23 class="amrow"><a href=list-users.html target="right">查询用户</a></td></tr>      <tr><td height=23 class="amrow"><a href=modify-users.html target="right">修改用户</a></td></tr>      <tr><td height=23 class="amrow"><a href=delete-users.jsp target="right">删除用户</a></td></tr>      </table>     </div> </Div>   </td>   </tr>

  <tr>           <td height=27 background="images/title.gif" style="cursor:hand" onClick="show(a2)">             <div align="center"><strong>新闻管理</strong>             </div>            </td>              </tr>   <tr>     <td> <DIV id=a2 style="DISPLAY: none">     <div align="left">       <table width="156" border="0" cellspacing="1" cellpadding="1" align="center">           <tr><td height=23 class="amrow"><a href=add-news.html target="right">添加新闻</a></td></tr>           <tr><td height=23 class="amrow"><a href=list-news.jsp target="right">查询新闻</a></td></tr>      <tr><td height=23 class="amrow"><a href=modify-news.jsp target="right">修改新闻</a></td></tr>      <tr><td height=23 class="amrow"><a href=delete-news.jsp target="right">删除新闻</a></td></tr>      </table>     </div> </Div>   </td>   </tr> </table>    

right.html

This is right!

add-users.html

<h1>添加用户信息</h1>

<form action="add-users.jsp" method="get">  用户名:<input type="text" name="uname"><br>  密码:<input type="password" name="upass"><br>  邮箱:<input type="text" name="email"><br>  <input type="submit" value="添加">  

</form>

add-users.jsp

<%@page pageEncoding="gb2312" import="java.sql.*"%>
<%
 //提取
 String u=request.getParameter("uname");
 String p=request.getParameter("upass");
 String e=request.getParameter("email");
 //添加到db/user表中
 //注:1、。jar包放到web-inf/lib文件夹中; 2、导入java.sql.*包
 
 try{
  Class.forName("org.gjt.mm.mysql.Driver");
  String url="jdbc:mysql://localhost:3306/db";//db数据库名
  Connection con=DriverManager.getConnection(url,"root","123456");
  String sql="insert into user1 values(?,?,?)";
  PreparedStatement pstm=con.prepareStatement(sql);
  pstm.setString(1,u);//设置参数
  pstm.setString(2,p);
  pstm.setString(3,e);
  pstm.executeUpdate();//执行语句
  pstm.close();//关闭语句
 
 
 }catch(Exception ex)
 {
 
 }
 
 %>

delete-users-do.jsp

<%@page import="java.sql.*"%>
<%
 String u=request.getParameter("uname");
 
 try{
  Class.forName("org.gjt.mm.mysql.Driver");
  String url="jdbc:mysql://localhost:3306/db";
  Connection con=DriverManager.getConnection(url,"root","123456");
  String sql="delete from user1 where uname=?";
  PreparedStatement pstm=con.prepareStatement(sql);
  pstm.setString(1,u);
  pstm.executeUpdate();
  pstm.close();
  con.close();
 
 }catch(Exception e)
 {
  e.printStackTrace();
 }
 
 %>

delete-users.jsp

<%@page pageEncoding="gb2312" import="java.sql.*"%>
<form action="delete-users-do.jsp" method="get">
 请输入要删除的用户名:<input type="text" name="uname">
 <input type="submit" value="删除">
</form>

查询:

 list-users.html

<form action="list-users.jsp" method="get">  请输入用户名:<input type="text" name="uname">  <input type="submit" value="查询"> </form>

list.users.jsp

<%@page pageEncoding="gb2312" import="java.sql.*"%>

<%  String u=request.getParameter("uname");    try{   Class.forName("org.gjt.mm.mysql.Driver");   String url="jdbc:mysql://localhost:3306/db";   Connection con=DriverManager.getConnection(url,"root","123456");      String sql="select uname,upass,email from user1 where uname like '%"+u+"%'";   PreparedStatement pstm=con.prepareStatement(sql);   //pstm.setString(1,u);   ResultSet rs=pstm.executeQuery();//查询结果集      //out.println("用户名 "+"密码 "+"Email "+"<br>"); %>   <table border=1>    <tr>     <th>用户名</th>     <th>密码</th>     <th>Email</th>    </tr> <%      while(rs.next())   { %>    <tr>     <td><%=rs.getString(1)%></td>     <td><%=rs.getString(2)%></td>     <td><%=rs.getString(3)%></td>             </tr> <%

   //out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+"<br>");   }          }catch(Exception e)  {   e.printStackTrace();  }    %>

modify-users.html


<form action="modify-users.jsp" method="get">
 请输入用户名:<input type="text" name="uname">
 <input type="submit" value="查询">
</form>

modify-users.jsp

<%@page pageEncoding="gb2312" import="java.sql.*"%>

<%  String u=request.getParameter("uname");    try{   Class.forName("org.gjt.mm.mysql.Driver");   String url="jdbc:mysql://localhost:3306/db";   Connection con=DriverManager.getConnection(url,"root","123456");   String sql="select uname,upass,email from user1 where uname=?";   PreparedStatement pstm=con.prepareStatement(sql);   pstm.setString(1,u);   ResultSet rs=pstm.executeQuery();   if(rs.next())   { %>    <form action="modify-users-do.jsp" method="get">     用户名:<input type="text" name="uname" value=<%=rs.getString(1)%>><br>     密码:<input type="text" name="upass" value=<%=rs.getString(2)%>><br>     Email:<input type="text" name="upass" value=<%=rs.getString(2)%>><br>     <input type="submit" value="保存">         </form> <%   }      }catch(Exception e)   {    e.printStackTrace();   }             %>

原文地址:https://www.cnblogs.com/shenzhangwei/p/6565304.html