import java.io.File;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class TextDatabase {
	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();
		// String dropTable = "drop table Articles;";
		// stmt.execute(dropTable);
		String createTable = "create table Articles(Name VARCHAR(255), Article LONGTEXT);";
		stmt.execute(createTable);

		String query = "insert into Articles " + "values (?,?)";
		PreparedStatement pstmt = con.prepareStatement(query);

		try {
			File file = new File("test.txt");
			FileReader fileReader = new FileReader(file,  StandardCharsets.UTF_8);

			pstmt.setInt(1, 2);
			pstmt.setCharacterStream(2, fileReader, (int) file.length());

			pstmt.executeUpdate();
			System.out.println("파일 저장 성공");

			pstmt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}