用 面向对象的方式 连接数据库------------------重点+难度
1. 创建新的module , 在 pom.xml 增加 依赖
2. entity 层
数据库表 dept , 实体 Dept类
3. 编写 工具类 DBUtils
定义四个数据库连接属性
定义了 获得连接的方法 及关闭的方法
4. 创建接口与实现类--------- 业务层 service
IDeptService 及 DeptServiceImpl
5. 按照 编号查询 部门名称
参见项目 jdbcOOP
6.
查询全部
查询一个
增加 ,删除 ,修改, 就是 sql 语句不同, 调用数据库连接都相同,因此
可以写 成 带参方法 来实现
package entity;
public class Dept {
private int id;
private String name;
public Dept() {
}
public Dept(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package service;
import java.sql.SQLException;
public interface IDeptService {
/**
* 根据部门编号获得部门名称
* @param id 部门编号
* @return 部门名称
*/
String getNameById(int id) throws SQLException;
/**
* 增加部门
* @param name 部门名称
* @return 受影响的行数
*/
int save(String name);
}
package service;
import java.sql.SQLException;
public interface IDeptService {
/**
* 根据部门编号获得部门名称
* @param id 部门编号
* @return 部门名称
*/
String getNameById(int id) throws SQLException;
/**
* 增加部门
* @param name 部门名称
* @return 受影响的行数
*/
int save(String name);
}
package util;
import java.sql.*;
/**
* 数据库连接的工具类
*/
public class DBUtils {
// 数据库连接的4个属性
public static final String USERNAME= "root"; //用户名
public static final String PASSWORD = "用户名";// 密码
public static final String URL="jdbc:mysql://localhost:3306/数据库";// 数据库的连接地址
public static final String DRIVER = "com.mysql.cj.jdbc.Driver"; // 注意 8-5.5 写法不同
/**
* 获得数据库的连接对象Connection
* @return Connection
*/
public Connection getConn(){
Connection connection = null;
try{
if(connection==null || connection.isClosed()){
Class.forName(DRIVER); // 加载驱动
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
}catch (Exception e){
System.out.println("-----数据库连接失败----"+e.getMessage());
}
return connection;
}
/**
* 关闭所有的 连接对象
* @param rs
* @param st
* @param conn
*/
public void closeAll(ResultSet rs, Statement st,Connection conn){
try{
// 关闭连接
if(rs !=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn !=null){
conn.close(); //关闭连接
}
}catch (Exception e){
System.out.println("--------关闭失败----"+e.getMessage());
}
}
// insert delete update 均返回 受影响的行数
/**
* 增加, 删除, 修改 均需要调用本方法,
* @param sql 执行 sql
* @param params sql 需要拼接的参数
* @return 受影响的行数, 如果 行数>=1 表示 sql执行成功, 否则 就是执行 失败
*/
public int execute(String sql,Object[] params){
//获得数据库的连接
Connection conn = this.getConn();
PreparedStatement st = null;
ResultSet rs = null;
try{
st = conn.prepareStatement(sql);
//组装sql
for(int i =0;i< params.length;i++){
st.setObject(i+1,params[i]);
}
//返回受影响的行数
return st.executeUpdate();
}catch (Exception e){
System.out.println("-----执行失败-----"+e.getMessage());
}finally {
this.closeAll(rs,st,conn);
}
return 0;
}
}
import org.junit.Test;
import service.IDeptService;
import service.impl.DeptServiceImpl;
import java.sql.SQLException;
public class TestDept {
@Test
public void testGetNameById() throws SQLException {
IDeptService service = new DeptServiceImpl();
String name = service.getNameById(3);
System.out.println("查询结果为----"+name);
}
@Test
public void testSave(){
IDeptService service = new DeptServiceImpl();
int count = service.save("规划部");
//根据 count的取值进行 判断
if(count>=1){
System.out.println("添加 部门 成功");
}else{
System.out.println("添加 部门 失败啊");
}
}
}