DAO层的实现:
package com.qyl.dao;
public class Dept {
private Integer id;
private String name;
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;
}
@Override
public String toString() {
return "Dept [id=" + id + ", name=" + name + "]";
}
}
package com.qyl.dao;
import java.util.List;
public interface IDeptDao {
/**
* 保存
* @param dept
*/
public void save(Dept dept);
/**
* 删除
* @param id
*/
public void delete(Integer id);
/**
* 修改
* @param dept
*/
public void update(Dept dept);
/**
* 获取单个
* @param dept
*/
public Dept getOne(Integer id);
/**
* 获取所有
* @param dept
*/
public List<Dept> getAll();
}
package com.qyl.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.qyl.dao.Dept;
import com.qyl.dao.IDeptDao;
import com.qyl.util.JDBCUtils;
public class DeptDaoImpl implements IDeptDao {
@Override
public void save(Dept dept) {
Connection conn = null;
Statement st = null;
try {
conn = JDBCUtils.getInstance().co();
Statement st1 = conn.createStatement();
// sql
String sql = "insert into dept (name) value ('"+dept.getName()+"')";
System.out.println(sql);
// 执行语句
st1.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
JDBCUtils.getInstance().close(null, st, conn);
}
}
@Override
public void delete(Integer id) {
}
@Override
public void update(Dept dept) {
// TODO Auto-generated method stub
}
@Override
public Dept getOne(Integer id) {
return null;
}
@Override
public List<Dept> getAll() {
List<Dept> list = new ArrayList<>();
Connection conn = null;
Statement st = null;
ResultSet result = null;
// 加载驱动
try {
conn = JDBCUtils.getInstance().co();
//获取语句对象
Statement st1 = conn.createStatement();
//sql
String sql = "select * from dept";
//执行语句
result = st1.executeQuery(sql);
while (result.next()) {
Dept dept = new Dept();
dept.setId(result.getInt("id"));
dept.setName(result.getString("name"));
list.add(dept);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.getInstance().close(result, st, conn);
}
return list;
}
}
package com.qyl.dao;
import java.util.List;
import org.junit.Test;
import com.qyl.dao.impl.DeptDaoImpl;
public class TestDaoImpl {
private IDeptDao deptDao = new DeptDaoImpl();
@Test
public void testSave() throws Exception {
Dept dept = new Dept();
dept.setName("化学部");
deptDao.save(dept);
}
@Test
public void testGetAll() throws Exception {
List<Dept> all = deptDao.getAll();
System.out.println(all);
}
JDBCUtils:
package com.qyl.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.qyl.dao.Dept;
import com.qyl.dao.IDeptDao;
import com.qyl.util.JDBCUtils;
public class DeptDaoImpl implements IDeptDao {
@Override
public void save(Dept dept) {
Connection conn = null;
Statement st = null;
try {
conn = JDBCUtils.getInstance().co();
Statement st1 = conn.createStatement();
// sql
String sql = "insert into dept (name) value ('"+dept.getName()+"')";
System.out.println(sql);
// 执行语句
st1.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
JDBCUtils.getInstance().close(null, st, conn);
}
}
@Override
public void delete(Integer id) {
}
@Override
public void update(Dept dept) {
// TODO Auto-generated method stub
}
@Override
public Dept getOne(Integer id) {
return null;
}
@Override
public List<Dept> getAll() {
List<Dept> list = new ArrayList<>();
Connection conn = null;
Statement st = null;
ResultSet result = null;
// 加载驱动
try {
conn = JDBCUtils.getInstance().co();
//获取语句对象
Statement st1 = conn.createStatement();
//sql
String sql = "select * from dept";
//执行语句
result = st1.executeQuery(sql);
while (result.next()) {
Dept dept = new Dept();
dept.setId(result.getInt("id"));
dept.setName(result.getString("name"));
list.add(dept);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.getInstance().close(result, st, conn);
}
return list;
}
}
package com.qyl.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
//私有化构造器
private JDBCUtils (){}
//静态代码块
private static Properties prop = null;
private static JDBCUtils instance = null;
static{
try {
prop = new Properties();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
Class.forName(prop.getProperty("driver"));
instance = new JDBCUtils();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取实例化
public static JDBCUtils getInstance(){
return instance;
}
//获取链接
public Connection co() throws Exception {
return DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("user"),prop.getProperty("password"));
}
public void close(ResultSet result,Statement st,Connection conn) {
try {
try {
if(result!=null)result.close();
} catch (Exception e2) {
e2.printStackTrace();
}finally {
try {
if(st!=null)st.close();
} catch (Exception e3) {
e3.printStackTrace();
}finally {
try {
if(conn!=null)conn.close();
} catch (Exception e4) {
e4.printStackTrace();
}
}
}
} catch (Exception e) {
}
}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root