ecliopse、tomcat 7.0、navicat for mysql
用到的架包:
mysql-connector-java-5.1.25-bin.jar
三.开发的代码:
3.1 User类:
package com.demo.bean;
public class User {
private Integer id;
private String name;
private String password;
private int role;
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
}
3.2 JdbcUtils代码:
package com.demo.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class JdbcUtils {
//注册驱动的工具类
private static String url = null;
private static String user = null;
private static String password = null;
private static String dv = null;
static{
Properties prop = new Properties();
InputStream in = JdbcUtils.class.getResourceAsStream("/a.properties");
try {
prop.load(in);
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("123456");
dv = prop.getProperty("driver");
/*System.out.println("url:"+url);*/
//注册驱动
try {
Class.forName(dv);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接对象
public static Connection getCon() throws SQLException{
Connection conn = null;
conn = (Connection) DriverManager.getConnection(url, user, password);
return conn;
}
//关闭的方法
public static void close(Statement statement,Connection conn){
if(statement !=null){
try {
statement.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 static void close(PreparedStatement preparedStatement,Connection conn,ResultSet resultSet){
if(preparedStatement !=null){
try {
preparedStatement.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();
}
}
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.3 Login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
登录界面
3.4 LoginServlet代码:
package com.demo.service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.demo.bean.User;
import com.demo.dao.UserDAO;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDAO userDAO=new UserDAO();
User user=userDAO.login(username, password);
if(user!=null){
request.getRequestDispatcher("success.jsp").forward(request, response);;
}else{
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
}
3.5 UserDAO代码:
package com.demo.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.demo.bean.User;
import com.demo.utils.JdbcUtils;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class UserDAO {
//数据库连接对象
public User login(String username,String password) {
User u=null;
Connection connection =null;
PreparedStatement pstmt=null;
ResultSet resultSet=null;
//赋值
try {
connection=JdbcUtils.getCon();
//静态sql语句
String sql = "select * from user where name=? and password=?";
pstmt = (PreparedStatement) connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
resultSet = pstmt.executeQuery();
if(resultSet.next()){
u=new User();
u.setName(resultSet.getString("name"));
u.setPassword(resultSet.getString("password"));
System.out.println("登录成功!");
}else{
System.out.println("用户名或者密码错误!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(pstmt, connection);
}
return u;
}
public void addUser(User user) {
Connection connection = null;
PreparedStatement psmt = null;
try {
connection = JdbcUtils.getCon();
String sql ="insert into user(id,name,password,role)values(?,?,?,?);";
psmt = (PreparedStatement) connection.prepareStatement(sql);
//运用实体对象进行参数赋值
psmt.setInt(1, user.getId());
psmt.setString(2, user.getName());
psmt.setString(3,user.getPassword());
psmt.setInt(4, user.getRole());
psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtils.close(psmt, connection);
}
}
}
3.6 Register.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
注册界面
3.7 RegisterServlet代码:
package com.demo.service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.demo.bean.User;
import com.demo.dao.UserDAO;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPut(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
int id=Integer.valueOf(request.getParameter("id"));
String name=request.getParameter("name");
String password=request.getParameter("password");
int role=Integer.valueOf(request.getParameter("role"));
User user=new User();
user.setId(id);
user.setName(name);
user.setPassword(password);
user.setRole(role);
UserDAO userDAO=new UserDAO();
userDAO.addUser(user);
System.out.println("注册成功");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
4.1登录界面:
4.2 注册页面: