JDBC进阶版,提取重复代码,包装类
BookDao代码
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDao extends BaseDao {
//查询:
public List<Book> findAll(){
List<Book> list = new ArrayList<Book>();
try {
getConnection();
ps = connection.prepareStatement("select * from book");
set = ps.executeQuery();
while (set.next()) {
int id = set.getInt("id");
String name = set.getString("name");
Date date = set.getDate("time");
Book book = new Book(id, name, date);
list.add(book);
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return list;
}
public Book find(int fid){
Book book = null;
try {
getConnection();
ps = connection.prepareStatement("select * from book where id = ?");
ps.setObject(1,fid);
set = ps.executeQuery();
while (set.next()) {
int id = set.getInt("id");
String name = set.getString("name");
Date date = set.getDate("time");
book = new Book(id, name, date);
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return book;
}
public int insert(Book book){
return update("insert into Book values(?,?,?)",book.getId(),book.getName(),book.getDate());
}
public int update(Book book){
return update("update Book set name = ?,time=? where id = ?",book.getName(),book.getName(),book.getDate());
}
public int delete(int id){
return update("delete from book where id = ?",id);
}
}
BaseDao代码
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class BaseDao {
public Connection connection = null;
public PreparedStatement ps = null;
public ResultSet set = null;
public static String username = null;
public static String password = null;
public static String driverName = null;
public static String url = null;
// public static final String url = "jdbc:mysql://localhost:3306/ceshi?serverTimezone = Asia/Shanghai";
static {
try {
InputStream resourceAsStream = BaseDao.class.getResourceAsStream("/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
username = properties.get("username").toString();
password = properties.get("password").toString();
driverName = properties.get("driverName").toString();
url = properties.get("url").toString();
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getConnection(){
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public int update(String sql,Object... params){
int row = -1;
try {
getConnection();
ps = connection.prepareStatement(sql);
for (int index = 0; index < params.length; index++) {
ps.setObject(index + 1, params[index]);
}
row = ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return row;
}
public void closeAll(){
if(set!=null){
try {
set.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
src中的配置文件properties:
username=root
password=123456
driverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ceshi?serverTimezone=Asia/Shanghai
entity里面的类Book
import java.util.Date;
public class Book {
private int id;
private String name;
private Date date;
public Book() {
}
public Book(int id, String name, Date date) {
this.id = id;
this.name = name;
this.date = date;
}
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;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", date=" + date +
'}';
}
}
test里面的测试类
import java.util.Date;
import java.util.List;
public class test3 {
public static void main(String[] args) {
BookDao bookDao = new BookDao();
// List<Book> list = bookDao.findAll();
// for (Book book:list){
// System.out.println(book);
// }
// Book book = bookDao.find(2);
// System.out.println(book);
//插入
int i = bookDao.insert(new Book(3,"刘",new Date(1999-11-01)));
System.out.println(i);
//更新
// int i = bookDao.update(new Book(2,"zhang",new Date(2000-01-01)));
// System.out.println(i);
//删除
// int i = bookDao.delete(3);
// System.out.println(i);
}
}