public class InfoDao { Connection con=null; PreparedStatement ps = null; DBConnection dbc = new DBConnection(); public InfoDao(){ con = dbc.getCon(); } public boolean addImages(Info info){ boolean f = false; String sql ="insert into info values(?)"; try { ps = con.prepareStatement(sql); ps.setString(1, info.getImages()); int i = ps.executeUpdate(); if(i>0){ f = true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return f; } public List showImages(){ List list = new ArrayList(); ResultSet rs = null; Info info = null; String sql = "select * from info"; try { ps = con.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ info = new Info(); info.setImages(rs.getString("images")); list.add(info); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public static void main(String[] args) { /* Info info = new Info(); // i.setImages("a"); // System.out.println(new InfoDao().addImages(i)); List list = new InfoDao().showImages(); System.out.println(list.size()); for(int i = 0;i<list.size();i++){ info = (Info)list.get(i); System.out.println(info.getImages()); }*/ } }
Action处理:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException { UpForm upForm = (UpForm) form; FormFile formFile = upForm.getWenjian(); PrintWriter out = null; response.setCharacterEncoding("GBK"); out = response.getWriter(); String path = this.getServlet().getServletContext().getRealPath("/uploads"); InputStream in = formFile.getInputStream(); String filePath =path + "//"+ formFile.getFileName(); FileOutputStream fout = new FileOutputStream(filePath); byte buffer[] = new byte[8192]; int count = 0; InfoDao id = new InfoDao(); Info info = new Info(); info.setImages(filePath); boolean flag = id.addImages(info);