代码:
一.前端界面的优化
zhuce.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册页面
会员注册
@charset "utf-8";
/* CSS Document */
@charset "utf-8";
/* CSS Document */
body {
text-align: center;
font-size: 12px;
font-family: "宋体",Tahoma, Arial, Helvetica, sans-serif;
color: #333;
}
#container{
margin:0px auto;
width:1500px;
height:1130px;
text-align:left;
background:#FFF;
border: 10px solid #0F0;
}
#top{
height:30px;
width:100%;
line-height: 30px;
border-bottom: 1px solid #cacaca;
clear: both;
padding: 25px 0 0;
}
#tit_name
{
display: block;
float: left;
padding: 0 10px;
line-height: 30px;
border-bottom: 1px solid #31e02c;
font-size: 14px;
font-weight: bold;
}
#left{
height:1000px;
width:900px;
float: left;
display: inline;
}
#loginbox
{
float: left;
margin-top:100px;
width: 400px;
padding: 50px;
border: 1px solid #069;
}
#username{
height: 32px;
line-height: 32px;
text-indent: 2px;
border: 1px solid #666;
background: #fff;
}
二.后端功能的实现
DButil.java
package Servlet;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class DButil
{
private static String driver;
private static String url;
private static String username;
private static String password;
static {
driver="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/test";
username="root";
password="root";
}
/*
* 打开数据库
*/
public static Connection open()
{
try {
System.out.println("数据库连接成功!");
Class.forName(driver);
return (Connection) DriverManager.getConnection(url,username,password);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败!");
e.printStackTrace();
}
return null;
}
/*
* 关闭数据库
*/
public static void close(Connection conn)
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package Servlet;
public class Customer {
String username;
String password;
String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
}
package Servlet;
import java.util.List;
public interface CustomerDAO {
public void add(Customer c);//添加用户
public void update(Customer c);//修改用户
public List query();//查询返回所有用户
}
package Servlet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class CustomerDAOer implements CustomerDAO {
public void add(Customer c) {
// TODO Auto-generated method stub
String sql="insert into user(username,password,email) values(?,?,?)";
Connection conn=DButil.open();
try {
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1,c.getUsername());
pst.setString(2,c.getPassword());
pst.setString(3,c.getEmail());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DButil.close(conn);
}
}
@Override
public void update(Customer c) {
// TODO Auto-generated method stub
String sql="update user set password=? where username=?";
Connection conn=DButil.open();
try {
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(2,c.getPassword());
pst.setString(3,c.getEmail());
pst.setString(1,c.getUsername());
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DButil.close(conn);
}
}
@Override
public List query() {
String sql="select * from user";
Connection conn=DButil.open();
try {
Statement stmt=(Statement) conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
List list=new ArrayList();
while(rs.next())
{
String username=rs.getString(1);
String password=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setUsername(username);
c.setPassword(password);
c.setEmail(email);
list.add(c);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DButil.close(conn);
}
return null;
}
}
package Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register extends HttpServlet {
private static final long serialVersionUID = 1L;
public Register() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String username=request.getParameter("username");
String passwordone=request.getParameter("passwordone");
String passwordtwo=request.getParameter("passwordtwo");
String email=request.getParameter("email");
PrintWriter out=response.getWriter();
if(username!=""&&passwordone.equals(passwordtwo)&&email!="")
{
String password=passwordone;
out.println("恭喜您!注册成功");
CustomerDAO dao=new CustomerDAOer();//调用接口读取方法来实例化
Customer c=new Customer();
c.setUsername(username);
c.setPassword(password);
c.setEmail(email);
dao.add(c);
}
else
{
out.println("您输入的信息有误,请重新输入!");
}
}
}
看看后台的数据库(最后一个即是刚注册的用户),说明用户注册成功了!