一、封装数据库操作
创建DBUtil类,创建一个单例类辅助创建连接,其中URL为数据库连接字符串,用户名,密码都是固定的。
private static final String URL = "jdbc:mysql://127.0.0.1:3306/image_server?
characterEncoding=utf8&useSSL=true"
private static final String USERNAME = "root";
private static final String PASSWORD = "1002";
这个类主要包含三个方法
// 这是一个获取单例的方法
public static DataSource getDataSource() { }
// 获取链接
public static Connection getConnection() { }
// 关闭链接
public static void close(Connection conn, PreparedStatement statement,
ResultSet resultSet) {}
类的实现代码
public class DBUtil {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/image_server?characterEncoding=utf8&useSSL=true";
private static final String USERNAME = "root";
private static final String PASSWORD = "1002";
private static volatile DataSource dataSource = null;
public static DataSource getDataSource() {
//通过这个方法创建DataSource的实例
if (dataSource == null) {
synchronized (DBUtil.class) {
if(dataSource == null){
dataSource = new MysqlDataSource();
MysqlDataSource tmpDataSource = (MysqlDataSource) dataSource;
tmpDataSource.setURL(URL);
tmpDataSource.setUser(USERNAME);
tmpDataSource.setPassword(PASSWORD);
}
}
}
return dataSource;
}
public static Connection getConnection() {
try {
return getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet){
try {
if(resultSet != null){
resultSet.close();
}
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
创建Image类
public class Image {
private int imageId;
private String imageName;
private int size;
private String uploadTime;
private String contentType;
private String path;
private String md5;
}
创建ImageDao类
public class ImageDao {
public boolean insert(Image image) {
return false;
}
public Image[] selectAll() {
return null;
}
public Image selectOne(int imageId) {
return null;
}
public boolean delete(int imageId) {
return false;
}
}
实现ImageDao.insert方法
public void insert(Image image){
//1.获取数据库连接
Connection connection = DBUtil.getConnection();
//2.创建并拼装 sql 语句
String sql = "insert into image_table values(null,?,?,?,?,?,?)";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
statement.setString(1,image.getImageName());
statement.setInt(2,image.getImageId());
statement.setString(3,image.getuuploadTime());
statement.setString(4,image.getContentType());
statement.setString(5,image.getPath());
statement.setString(6,image.getMd5());
//3.执行 sql 语句
int ret = statement.executeUpdate();
if(ret != 1){
//程序出现问题,抛出一个异常
throw new JavaImageServerException("插入数据库出错!");
}
} catch (SQLException | JavaImageServerException e) {
e.printStackTrace();
}finally {
//4.关闭连接和 statement
DBUtil.close(connection,statement,null);
}
}
实现ImageDao.selectAll
public List selectAll(){
List images = new ArrayList<>();
//1.获取数据库连接
Connection connection = DBUtil.getConnection();
//2.构造 sql 语句
String sql = "select * from image_table";
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//3.执行 sql 语句
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
//4.处理结果集\
while (resultSet.next()){
Image image = new Image();
image.setImageId(resultSet.getInt("imageId"));
image.setImageName(resultSet.getString("imageName"));
image.setSize(resultSet.getInt("size"));
image.setuploadTime(resultSet.getString("uploadTime"));
image.setContentType(resultSet.getString("contentType"));
image.setPath(resultSet.getString("path"));
image.setMd5(resultSet.getString("md5"));
images.add(image);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//5.关闭连接
DBUtil.close(connection,statement,resultSet);
}
return images;
}
实现ImageDao.selectOne方法
public Image selectOne(int imageId){
//1.获取数据库连接
Connection connection = DBUtil.getConnection();
//2.构造sql语句
String sql = "select *from image_table where imageId = ?";
PreparedStatement statement = null;
ResultSet resultSet = null;
//3.执行sql语句
try {
statement = connection.prepareStatement(sql);
statement.setInt(1,imageId);
resultSet = statement.executeQuery();
//4.处理结果集
if (resultSet.next()){
Image image = new Image();
image.setImageId(resultSet.getInt("imageId"));
image.setImageName(resultSet.getString("imageName"));
image.setSize(resultSet.getInt("size"));
image.setuploadTime(resultSet.getString("uploadTime"));
image.setContentType(resultSet.getString("contentType"));
image.setPath(resultSet.getString("path"));
image.setMd5(resultSet.getString("md5"));
return image;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//5.关闭连接
DBUtil.close(connection,statement,resultSet);
}
return null;
}
实现ImageDao.delete方法
public void delete(int imageId) {
//1.获取数据库连接
Connection connection = DBUtil.getConnection();
//2.拼装 sql 语句
String sql = "delete from image_table where imageId = ?";
PreparedStatement statement = null;
//3.执行 sql 语句
try {
statement = connection.prepareStatement(sql);
statement.setInt(1,imageId);
int ret = statement.executeUpdate();
if(ret != 1){
throw new JavaImageServerException("删除数据库出错!");
}
} catch (SQLException |JavaImageServerException e) {
e.printStackTrace();
} finally {
DBUtil.close(connection,statement,null);
}
}