다음은 vsms라는 데이터베이스 내에 test라는 테이블에 한글 데이터를 넣는 예제 프로그램이다.

import java.sql.*;

public class MySQLKoreanInsertDemo {
 public static void main(String[] args) {
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  } catch (ClassNotFoundException e) {
   System.err.println(e);
  } catch (InstantiationException e) {
   System.err.println(e);
  } catch (IllegalAccessException e) {
   System.err.println(e);
  }

  String server = "localhost";
  String dbname = "vsms";
  String username = "user1";
  String password = "passwd1";

  String url = "jdbc:mysql://" + server + "/" + dbname
    + "?user=" + username
    + "&password=" + password
    + "&useUnicode=true&characterEncoding=euc_kr";

  try {
   Connection conn = DriverManager.getConnection(url);
   Statement stmt = conn.createStatement();

   String query = "INSERT INTO test (data) VALUES ('임정묵')";

   stmt.executeUpdate(query);

   stmt.close();
   conn.close();
  } catch (SQLException e) {
   System.err.println(e);
  }
 }
}


위에서 "&useUnicode=true&characterEncoding=euc_kr"가 필요함에 유의한다.

Posted by 알 수 없는 사용자
,