import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ImageDatabase {
	public static Connection makeConnection() {
		String url = "jdbc:mysql://localhost:3306/book_db?characterEncoding=UTF-8&serverTimezone=UTC";
		String id = "root";
		String password = "1234";
		Connection con = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			System.out.println("드라이버 적재 성공");
			con = DriverManager.getConnection(url, id, password);
			System.out.println("데이터베이스 연결 성공");
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버를 찾을 수 없습니다.");
		} catch (SQLException e) {
			System.out.println("연결에 실패하였습니다.");
		}
		return con;
	}

   public static void main(String args[]) throws Exception {

      Connection con = makeConnection();
      Statement stmt = con.createStatement();
      //stmt.execute("DROP TABLE Animals");

      
      String createTable = "CREATE TABLE Animals( "
         + "Name VARCHAR(255), "
         + "Type VARCHAR(50), "
         + "Image BLOB)";
      stmt.execute(createTable);

      String query = "INSERT INTO Animals(Name, Type, Image) VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Dog");
      pstmt.setString(2, "WHITE DOG");
      FileInputStream fin = new FileInputStream("dog.png");
      pstmt.setBinaryStream(3, fin);
      pstmt.execute();

      pstmt.setString(1, "Cat");
      pstmt.setString(2, "GRAY CAT");
      fin = new FileInputStream("cat.png");
      pstmt.setBinaryStream(3, fin);
      pstmt.execute();
      System.out.println("데이터가 추가되었습니다.");
      ResultSet rs = stmt.executeQuery("Select *from Animals");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Type: "+rs.getString("Type")+", ");
         System.out.print("Image: "+rs.getBlob("Image"));
         System.out.println();
      }
      con.close();
   }
}