--- 第一步 实体类 package com.student.DAO.util; public class User { private String name; private String password; private String email; public User(String name,String password,String email) { super(); this.name = name; this.password = password; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
--第二部 添加数据的java文件 package com.student.Bean.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Regis { public Connection conn = null; public Statement stm = null; public ResultSet rs = null; public String check; public void getConn() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;database=demodb", "sa", "svse"); // 测试数据库是否连接成功 if(conn!=null){ System.out.println("ok!"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public boolean register(String name, String pwd, String email) { boolean bol = false; this.getConn(); String sql = "insert into t_user values('"+name+"','"+pwd+"','"+email+"')"; String sqlSelect="select name from t_user where name='"+name+"'"; try { stm=conn.createStatement(); rs=stm.executeQuery(sqlSelect); //如果rs有值代表此账号已经存在 则现在需要无法注册 if(rs.next()){ check="亲,此用户名已经注册过了哦,亲你要换一个哦!"; }else{ if(name!=""){ if(pwd!=""){ if(email!=""){ //邮箱不允许重复注册 String sqlSelectEmail="select email from t_user where email='"+email+"'"; stm=conn.createStatement(); rs=stm.executeQuery(sqlSelectEmail); if(rs.next()){ check="亲,此邮箱账号已经被注册过了哦,亲你再换一个邮箱吧!"; }else{ if(email.contains("@") && (email.endsWith(".cn") || email.endsWith(".com") )){ //PreparedStatement pstm=null; stm= conn.createStatement();//创建语句对象 stm.execute(sql); //pstm=conn.prepareStatement(sql); //pstm.execute(); bol = true; }else{ check="亲,请填写正确的邮箱格式!"; } } }else{ check="亲,请填写邮箱噢!"; } }else{ check="亲,请填写密码哦!"; } }else{ check="亲,请填写用户名哦!"; } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return bol; } // public static void main(String[] args){ // Regis r=new Regis(); // r.getConn(); // } }
---- 查看的java文件 package com.student.Bean.util; import java.util.ArrayList; import java.util.List; import com.student.DAO.util.User; public class Select { public List selectMessage(){ Regis re=new Regis(); re.getConn(); List luser=new ArrayList(); String sql="select * from t_user"; try { re.stm=re.conn.createStatement(); re.rs=re.stm.executeQuery(sql); while(re.rs.next()){ String name=re.rs.getString(1); String password=re.rs.getString(2); String email=re.rs.getString(3); User u=new User(name,password,email); luser.add(u); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return luser; } }
--- servlet 文件 --- 注册 package com.student.web.util; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.student.Bean.util.Regis; public class RegisServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); String name = request.getParameter("userName"); String pwd = request.getParameter("userPwd"); String email = request.getParameter("userEmail"); Regis re = new Regis(); name=new String(name.getBytes("ISO-8859-1"),"gbk"); if (re.register(name,pwd,email)==true) { HttpSession sess = request.getSession(); sess.setAttribute("regisName", name); request.getRequestDispatcher("success.jsp").forward(request, response); } else { //out.println("sorry!未能成功添加!"); HttpSession sess = request.getSession(); sess.setAttribute("check", re.check); request.getRequestDispatcher("error.jsp").forward(request, response); } } }
---- 查看 package com.student.web.util; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.student.Bean.util.Select; import com.student.DAO.util.User; public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ response.setContentType("text/html;charset=gbk"); Select ss=new Select(); List array=ss.selectMessage(); HttpSession sess=request.getSession(); sess.setAttribute("selectAll",array); request.getRequestDispatcher("select.jsp").forward(request,response); } }
---- web界面 <servlet> <servlet-name>insert</servlet-name> <servlet-class>com.student.web.util.RegisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>insert</servlet-name> <url-pattern>/insertAdd</url-pattern> </servlet-mapping> <servlet> <servlet-name>message</servlet-name> <servlet-class>com.student.web.util.SelectServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>message</servlet-name> <url-pattern>/selectAllMessage</url-pattern> </servlet-mapping>
---- index 界面 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page errorPage="error.jsp" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="insertAdd" method="post"> 请输入用户名:<input type="text" name="userName" /> <br> 请输入密码:<input type="password" name="userPwd" /> <br> 请输入注册邮箱: <input type="text" name="userEmail" /> <br> <input type="submit" name="submitUser" value="确定" /> </form> </body> </html>
登录失败 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page isErrorPage="true" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> sorry,<%=session.getAttribute("check") %> </body> </html>
登陆成功 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="selectAllMessage" method="post"> 欢迎你:<%=session.getAttribute("regisName") %>,恭喜你,注册成功! <input type="submit" name="submitMessage" value="查看所有注册成员信息" /> </form> </body> </html>
-- 查看所有信息 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="com.student.DAO.util.User" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'select.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <tr> <td>用户名</td> <td>密码</td> <td>邮箱</td> </tr> <% List<User> list=(ArrayList)session.getAttribute("selectAll"); for(int i=0;i<list.size();i++){ User us=list.get(i); %> <tr> <td><%=us.getName() %></td> <td><%=us.getPassword() %></td> <td><%=us.getEmail() %></td> </tr> <% } %> </table> </body> </html>
if exists (select * from sys.objects where name='demodb') drop database demodb go create database demodb go use demodb go create table t_user ( name nvarchar(20) , [password] nvarchar(20), email nvarchar(30) ) go select * from t_user