package demo.dao.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import demo.dao.IComputerDAO;
import demo.jdbc.MyConnection;
import demo.model.Computer;
public class ComputerDAO implements IComputerDAO{
private Connection connection = MyConnection.getConnection();
private PreparedStatement pstmt=null;
private ResultSet rs;
private File upload;
private String uploadFileName;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public List<Computer> find() {
// TODO Auto-generated method stub
String sql = "select * from info_computer";
List<Computer>computerData = new ArrayList<Computer>();
try{
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
Computer computer = new Computer();
computer.setId(rs.getInt(1));
computer.setBrand(rs.getString(2));
computer.setVersion(rs.getString(3));
computer.setPrice(rs.getString(4));
computer.setCtype(rs.getString(5));
computer.setScreensize(rs.getString(6));
computer.setCpu(rs.getString(7));
computer.setGraphics(rs.getString(8));
computer.setMemory(rs.getString(9));
computer.setHardDisk(rs.getString(10));
computer.setColor(rs.getString(11));
//computer.setImageFileName(rs.getString(12));
computerData.add(computer);
}
rs.close();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return computerData;
}
@Override
public void insertOK(Computer computer)throws Exception {
// TODO Auto-generated method stub
InputStream is = new FileInputStream(computer.getTpbin());
String path = ServletActionContext.getServletContext().getRealPath("/img/");
System.out.println(computer.getTpbinFileName());
System.out.println(computer.getTpbinContentType());
String est = computer.getTpbinFileName().substring(computer.getTpbinFileName().lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String saveFileName = uuid + est;
System.out.println(path + saveFileName);
OutputStream os = new FileOutputStream(path + saveFileName);
byte buffer[] = new byte[1024];
int count = 0;
while((count=is.read(buffer))>0){
os.write(buffer,0,count);
}
is.close();
os.close();
String sql = "insert into info_computer(brand,version,price,ctype,screensize,cpu,graphics,memory,hardDisk,color,tp) "
+ "values(?,?,?,?,?,?,?,?,?,?,?)";
try{
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, computer.getBrand());
pstmt.setString(2, computer.getVersion());
pstmt.setString(3, computer.getPrice());
pstmt.setString(4, computer.getCtype());
pstmt.setString(5, computer.getScreensize());
pstmt.setString(6, computer.getCpu());
pstmt.setString(7, computer.getGraphics());
pstmt.setString(8, computer.getMemory());
pstmt.setString(9, computer.getHardDisk());
pstmt.setString(10, computer.getColor());
pstmt.setString(11,saveFileName);
pstmt.executeUpdate();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void updateOK(Computer computer) {
// TODO Auto-generated method stub
String sql = "update info_computer set brand=?,version=?,price=?,ctype=?,"
+ "screensize=?,cpu=?,graphics=?,memory=?,hardDisk=?,color=?,tp=? where id=?";
try{
pstmt=connection.prepareStatement(sql);
pstmt.setString(1, computer.getBrand());
pstmt.setString(2, computer.getVersion());
pstmt.setString(3, computer.getPrice());
pstmt.setString(4, computer.getCtype());
pstmt.setString(5, computer.getScreensize());
pstmt.setString(6, computer.getCpu());
pstmt.setString(7, computer.getGraphics());
pstmt.setString(8, computer.getMemory());
pstmt.setString(9, computer.getHardDisk());
pstmt.setString(10, computer.getColor());
pstmt.setString(11, computer.getTp());
pstmt.setInt(12, computer.getId());
pstmt.executeUpdate();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
String sql = "delete from info_computer where id=?";
try{
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public List<Computer> selectID(Integer id) {
// TODO Auto-generated method stub
String sql = "select * from info_computer where id=" + id;
List<Computer>computerData = new ArrayList<Computer>();
try{
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
Computer computer = new Computer();
computer.setId(rs.getInt(1));
computer.setBrand(rs.getString(2));
computer.setVersion(rs.getString(3));
computer.setPrice(rs.getString(4));
computer.setCtype(rs.getString(5));
computer.setScreensize(rs.getString(6));
computer.setCpu(rs.getString(7));
computer.setGraphics(rs.getString(8));
computer.setMemory(rs.getString(9));
computer.setHardDisk(rs.getString(10));
computer.setColor(rs.getString(11));
computer.setTp(rs.getString(12));
computerData.add(computer);
}
rs.close();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return computerData;
}
@Override
public List<Computer> selectKeywords(String keywords) {
// TODO Auto-generated method stub
String sql = "select * from info_computer where brand like '%"+keywords+"%'";
List<Computer>computerData = new ArrayList<Computer>();
try{
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
Computer computer = new Computer();
computer.setId(rs.getInt(1));
computer.setBrand(rs.getString(2));
computer.setVersion(rs.getString(3));
computer.setPrice(rs.getString(4));
computer.setCtype(rs.getString(5));
computer.setScreensize(rs.getString(6));
computer.setCpu(rs.getString(7));
computer.setGraphics(rs.getString(8));
computer.setMemory(rs.getString(9));
computer.setHardDisk(rs.getString(10));
computer.setColor(rs.getString(11));
computer.setTp(rs.getString(12));
computerData.add(computer);
}
rs.close();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return computerData;
}
}