前言碎语
下雨的NB市格外的迷人可爱,气温舒适
今天,企业讲师结合他们自己的开发经验,以及目前企业的标准习惯,为我们重新讲解了数据库建立连接的七个过程 ,意识到自己在学校里所学还是和实际应用有区别的,学校中我们只是对基本概念有所了解,而在实战中,讲究的是框架结构,条理顺序,代码框架必须要有,这样在项目的实施过程中才能有条不紊的进行下去
基本框架结构
controller 层
作为控制层,主要用来接收用户的请求信息,不会对信息进行其他过多的操作,如果需要对信息进行处理、操作,则需要移交给Service层
Service 层
在service层中,会对一些业务逻辑进行处理,但都不会涉及数据库的操作,因为在分层设计的时候,对数据库的操作都会在Dao中来
Dao 层
数据访问层,与数据库进行直接数据交换的
数据库的连接实现:
/**
* 1. 实现数据库连接的方法
*/
public Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
System.out.println("连接数据库成功.");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
数据库的释放:
/**
* 2. 释放数据库连接
*/
public void closeConn(ResultSet rs, PreparedStatement pstm, Connection conn) throws Exception {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
}
在dao的接口层中写增删查改的方法声明
public interface IUser {
void insertUser(User newUser) throws Exception; // 添加用户的方法
void deleteUser(int id) throws Exception; // 删除用户的方法
void updateUser(int id,User modUser) throws Exception; // 更新用户的方法
void selectByID(int id) throws Exception; // 根据id查询用户
void selectAllUser() throws Exception;// 查询全部用户**
}
增删查改具体操作:
public class UserDao implements IUser {
DBUtil db = new DBUtil();
private Connection conn;
@Override
public void insertUser(User newUser) throws Exception {
// 将用户信息添加到后台数据库表中
Connection conn = db.getConn();
PreparedStatement pstm = null;
String sql_insert = "insert into tb_user(username,password,type) values(?,?,?)";
pstm = conn.prepareStatement(sql_insert);
pstm.setString(1, newUser.getUsername());
pstm.setString(2, newUser.getPassword());
pstm.setInt(3, newUser.getType());
int row = pstm.executeUpdate();
System.out.println("新增用户成功" + row + "行受到影响");
db.closeConn(null, pstm, conn);
}
@Override
public void deleteUser(int id) throws Exception {
// 从数据表中删除用户信息
Connection conn = db.getConn();
PreparedStatement pstm = null;
String sql_delete = "delete from tb_user where id=?";
pstm = conn.prepareStatement(sql_delete);
pstm.setInt(1,id);
int row = pstm.executeUpdate();
System.out.println("删除用户成功"+row+"行受到影响");
db.closeConn(null, pstm, conn);
}
@Override
public void updateUser(int id, User modUser) throws Exception {
// 从数据库中修改用户信息
Connection conn = db.getConn();
PreparedStatement pstm = null;
String sql_update = "update tb_user set username=?password=?type=? where id=?";
pstm = conn.prepareStatement(sql_update);
pstm.setString(1,modUser.getUsername());
pstm.setString(2,modUser.getPassword());
pstm.setInt(3,modUser.getType());
int row = pstm.executeUpdate();
System.out.println("修改用户成功"+row+"行受到影响");
db.closeConn(null, pstm, conn);
}
@Override
public void selectByID(int id) throws Exception {
// 根据id单查用户信息
Connection conn = db.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
String sql_selectById = "select * from tb_user where id=?";
pstm = conn.prepareStatement(sql_selectById);
pstm.setInt(1,id);
rs = pstm.executeQuery();
System.out.println("查询成功,信息如下:");
System.out.println("用户编号\t\t用户名称\t\t用户密码\t\t用户类型\t\t");
while(rs.next()){
System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("username")+"\t\t"+rs.getString("password")+"\t\t"+(rs.getInt("type")==0?"管理员":"普通用户"));
}
db.closeConn(rs, pstm, conn);
}
@Override
public void selectAllUser() throws Exception {
// 查询全部用户信息
Connection conn = db.getConn();
PreparedStatement pstm = null;
ResultSet rs = null;
String sql_selectByAll = "select * from tb_user";
pstm = conn.prepareStatement(sql_selectByAll);
rs = pstm.executeQuery();
System.out.println("查询成功,信息如下:");
System.out.println("用户编号\t\t用户名称\t\t用户密码\t\t用户类型\t\t");
while(rs.next()){
System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("username")+"\t\t"+rs.getString("password")+"\t\t"+(rs.getInt("type")==0?"管理员":"普通用户"));
}
db.closeConn(rs, pstm, conn);
}
// 判断是否存在该用户,存在返回值为true,不存在返回值为false
public boolean getHaveUser(User user) throws Exception {
Connection conn = db.getConn();
PreparedStatement pstm = null;
String sql_selectHave = "select count(*) from tb_user where username=? and password=? and type=?";
pstm = conn.prepareStatement(sql_selectHave);
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
pstm.setInt(3,user.getType());
ResultSet rs = pstm.executeQuery();
while(rs.next()){
return true;
}
return false;
}
}