1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| String sql = "SELECT id, name, email, birth, photo From customer WHERE id = ?"; conn = getConnection(); ps = conn.prepareStatement(sql); ps setInt(1,8); rs = ps.executeQuery(); if(rs.next()){ Integer id = rs.getInt(1); String name = rs.getString(2); String email = rs.getString(3); Date birth = rs.getDate(4); Customer cust = new Customer(id, name, email, birth); System.out.println(cust); Blob photo = rs.getBlob(5); InputStream is = photo.getBinaryStream(); OutputStream os = new FileOutputStream("c.jpg"); byte [] buffer = new bytr[1024]; int len = 0; while((len = is.read(buffer)) != -1){ os.write(buffer,0,len); } JDBCUtils.closeResource(conn,ps,rs); if(is != null){ is.close(); } if(os != null){ os.close(); } }
|