这一节就是将之前的user案例的数据用数据库来替代
第一步:新建数据库
create table users(
id int primary key auto_increment,
username varchar(20),
password varchar(50),
nickname varchar(40),
email varchar(50)
);插入数据:
insert into users values(null,'admin','admin','admin','admin@qq.com');
第二步:编写能处理数据库的dao
先编写接口
package com.javaweb.dao;
import com.javaweb.domain.User;
public interface UserDao {
/*
* 根据用户名查找用户
* @param username用户名
* @return根据用户名找到用户信息bean,如果没有找到就返回null
*/
public User findUserByUserName(String username);
public void addUser(User user);
public User findUserByUNandPSW(String username,String password);
}
然后编写继承接口的类
package com.javaweb.dao;
import com.javaweb.util.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.javaweb.domain.User;
public class MySqlUserDao implements UserDao {
public void addUser(User user) {
String sql="insert into users valuses(null,'"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"','"+user.getEmail()+"')";
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try {
conn=JDBCUtils.getConn();
stat=conn.createStatement();
stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
JDBCUtils.close(rs, stat, conn);
}
}
public User findUserByUNandPSW(String username, String password){
String sql = "select * from users where username='"+username+"' and password='"+password+"'";
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try{
conn = JDBCUtils.getConn();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setNickname(rs.getString("nickname"));
user.setEmail(rs.getString("email"));
return user;
}else{
return null;
}
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
JDBCUtils.close(rs, stat, conn);
}
}
public User findUserByUserName(String username) {
String sql = "select * from users where username='"+username+"'";
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try{
conn = JDBCUtils.getConn();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setNickname(rs.getString("nickname"));
user.setEmail(rs.getString("email"));
return user;
}else{
return null;
}
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
JDBCUtils.close(rs, stat, conn);
}
}
}
接着在User类改造一下,添加id的字段
package com.javaweb.domain;
import java.io.Serializable;
import com.javaweb.exception.MsgException;
public class User implements Serializable{
private int id;
private String username;
private String password;
private String password1;
private String nickname;
private 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 getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "username:"+username;
}
public void checkValue() throws MsgException{
if(username==null || "".equals(username)){
throw new MsgException("用户名不能为空!");
}
if(password==null || "".equals(password)){
throw new MsgException("密码不能为空!");
}
if(password1==null || "".equals(password1)){
System.out.println("password:------"+password);
System.out.println("password1:------"+password1);
throw new MsgException("确认密码不能为空!");
}
if(!password.equals(password1)){
throw new MsgException("两次密码不一致!");
}
if(nickname==null || "".equals(nickname)){
throw new MsgException("昵称不能为空!");
}
if(email==null || "".equals(email)){
throw new MsgException("邮箱不能为空!");
}
//这个是正则表达式,可以在String 里面找到它的具体的公式
if(!email.matches("^\\w+@\\w+(\\.\\w+)+$")){
throw new MsgException("邮箱格式不正确!");
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
然后在UserService添加引用新dao的方法
这时候就可以试验了
注册之后,就可以在数据库上看到填写的信息
第三步:如何实现不改动代码就可以切换数据存储的载体?
上面的问题就是需要改动service,这是很不好的,那么我们该如何改变这种状况?
我们可以利用配置文件只需要改动配置文件就可以了
配置文件的内容:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day10
user=root
password=
UserDao=com.java.dao.MySqlUserDao
#UserDao=com.java.dao.XmlUserDao当只改变上面的配置文件,那么就可以改变数据库或用web.xml存储数据,那么用什么改变的?我们可以想到工厂类
里面的代码是这样的:
package com.javaweb.factory;
import com.javaweb.dao.UserDao;
public class DaoFactory {
private static DaoFactory factory=new DaoFactory();
private DaoFactory(){
}
public static DaoFactory getFactory(){
return factory;
}
public UserDao getDao(){
try{
String clazz = prop.getProperty("UserDao");
return (UserDao) Class.forName(clazz).newInstance();
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
接着我们就可以改变service的代码了
这时候就大功告成了。