本章来实现一下上传文件列表展示,同时优化了一下第一章中的代码。
废话少说,上代码
mysql创建附件表
DROP TABLE tbl_accessory; CREATE TABLE tbl_accessory ( id INT AUTO_INCREMENT PRIMARY KEY, file_name VARCHAR(500), file_size DOUBLE(10,2), file_ext_name VARCHAR(100), file_path VARCHAR(2000) ) ; SELECT * FROM tbl_accessory; DELETE FROM tbl_accessory;
创建附件实体类
package entity.upload; /** * 附件实体类 * * @author xusucheng * @create 2017-12-29 **/ public class EntityAccessory { private int id; private String fileName; private double fileSize; private String file_ext_name; private String filePath; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public double getFileSize() { return fileSize; } public void setFileSize(double fileSize) { this.fileSize = fileSize; } public String getFile_ext_name() { return file_ext_name; } public void setFile_ext_name(String file_ext_name) { this.file_ext_name = file_ext_name; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } }
创建DBUtil工具类
package util; import java.sql.*; import java.io.InputStream; import java.util.Properties; /** * 数据库工具类 * * @author xusucheng * @create 2017-11-18 **/ public class DBUtil { //定义链接所需要的变量 private static Connection con = null; private static PreparedStatement ps = null; private static ResultSet rs = null; //定义链接数据库所需要的参数 private static String url = ""; private static String username = ""; private static String driver=""; private static String password=""; //定义读取配置文件所需要的变量 private static Properties pp = null; private static InputStream fis = null; /** * 加载驱动 */ static { try { //从dbinfo.properties配置文件中读取配置信息 pp = new Properties(); fis = DBUtil.class.getClassLoader().getResourceAsStream("db.properties"); pp.load(fis); url = pp.getProperty("url"); username = pp.getProperty("username"); driver=pp.getProperty("driver"); password=pp.getProperty("password"); //加载驱动 Class.forName(driver); } catch (Exception e) { System.out.println("驱动加载失败!"); e.printStackTrace(); } finally { try { fis.close(); } catch (Exception e) { e.printStackTrace(); } fis = null; //垃圾回收自动处理 } } /** * 得到Connection链接 * @return Connection */ public static Connection getConnection() { try { //建立连接 con = DriverManager.getConnection(url, username, password); } catch (Exception e) { System.out.println("数据库链接失败!"); e.printStackTrace(); } return con; } /*public DBUtil(String sql){ try { ps = getConnection().prepareStatement(sql);//准备执行语句 } catch (Exception e) { e.printStackTrace(); } } public void close() { try { con.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } }*/ /** * 统一的资源关闭函数 * @param rs * @param ps * @param con */ public static void close(ResultSet rs,Statement ps, Connection con){ if(rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if(ps != null) { try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } if(con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
创建附件实体DAO类
package dao.upload; import entity.upload.EntityAccessory; import util.DBUtil; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * 附件上传DAO * * @author xusucheng * @create 2017-12-29 **/ public class AccessoryDao { public static void add(EntityAccessory entity) { Connection conn = DBUtil.getConnection(); String sql = "insert into tbl_accessory(file_name,file_size,file_ext_name,file_path) values(?,?,?,?)"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, entity.getFileName()); ps.setDouble(2, entity.getFileSize()); ps.setString(3, entity.getFile_ext_name()); ps.setString(4, entity.getFilePath()); ps.execute(); //conn.commit(); DBUtil.close(null, ps, conn); } catch (SQLException e) { e.printStackTrace(); } } public static List<EntityAccessory> list() { Connection conn = DBUtil.getConnection(); String sql = "select id,file_name,file_size,file_ext_name,file_path from tbl_accessory"; List<EntityAccessory> accessoryList = new ArrayList<>(); try { PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { EntityAccessory entity = new EntityAccessory(); entity.setId(rs.getInt("id")); entity.setFileName(rs.getString("file_name")); entity.setFileSize(new BigDecimal(rs.getDouble("file_size") / 1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()); entity.setFile_ext_name(rs.getString("file_ext_name")); entity.setFilePath(rs.getString("file_path")); accessoryList.add(entity); } DBUtil.close(rs, ps, conn); } catch (SQLException e) { e.printStackTrace(); } return accessoryList; } public static void remove(int id) { Connection conn = DBUtil.getConnection(); String sql = "delete from tbl_accessory where id=?"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,id); ps.execute(); //conn.commit(); mysql默认开启了autocommit DBUtil.close(null,ps,conn); } catch (SQLException e) { e.printStackTrace(); } } }
创建list.jsp列表页面