1
首先在sql 中创建一个数据库 ASD 然后创建一个表 存放 用户名,密码,性别,爱好,出生日期,个性签名,注册时间。
2
,写一个java 类 User,连接数据库,
package cn.com.servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class User {
String username = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//1.获取数据库连接
public Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=ASD";
String user = "sa";
String password = "SQL2014";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//2.执行更新语句
public int executeUpdate(String sql,Object...objects){
int row = 0;
conn = this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i+1, objects[i]);
}
row = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
//3.执行查询语句
public ResultSet executeQuery(String sql,Object...objects){
conn = this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i+1, objects[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//4.关闭资源
public void closeAll(){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
3.
写一个servlet 将注册信息存到数据库中 Usertext
package cn.com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Usertest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//脚本乱麻
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbys = request.getParameterValues("hobby");
String hobby = "";
for (String string : hobbys) {
hobby = hobby + string+" ";
}
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
//获取系统当前时间
String bir = year+"-"+month+"-"+day;
java.sql.Date time = java.sql.Date.valueOf(bir);
DateFormat df = null; //中文显示时间
df = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
String ztime = df.format(new Date());
String sign = request.getParameter("sign");
//3.插入数据库
User conn = new User();
String sql = "insert into u_tabe1 values(?,?,?,?,?,?,?)";
int row = conn.executeUpdate(sql, username,password,sex,hobby,time,sign,ztime);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
if(row != 0){
out.println("注册成功!");
//跳转到登录页面
RequestDispatcher rd = request.getRequestDispatcher("MyHtml.html");
rd.forward(request, response);
}else {
out.println("注册失败!");
response.sendRedirect("zhuceshi.html");
}
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
4.
写一个登录验证的servlet text
package cn.com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class test extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//修改编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//设置响应的内容类型 :html网页
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//获取表单的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//连接数据库 判断能否登录
User dbUtil = new User();
String sql = "select * from u_tabe1 where username = ? and password = ?";
ResultSet rs = dbUtil.executeQuery(sql, username,password);
//给浏览器的响应内容(html源代码)
out.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
try {
if(rs.next()){
out.println("登录成功!");
}else{out.println("登录失败!");
request.getRequestDispatcher("MyHtml.html").forward(request, response);
}
} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{dbUtil.closeAll();}out.println(" ");out.println("");out.flush();out.close();}}
5.
写一个注册的页面 ,zhuce.html
zhuce.html
6.
写一个登录的页面
MyHtml.html