JDBC 드라이버와 한글 깨짐 해결

By | 2009년 6월 9일

JSP 프로그램에서 MySQL용 JDBC 드라이버를 이용 폼으로 부터 POST 메소드로 전달된 한글 데이터를 Insert 할 때 한글이 깨진다면 페이지 문자셋을 고려하여 연결 URL을 다음과 같이 지정한다. 그러면 한글이 깨지지 않은 데이터를 테이블에 잘 저장할 수 있다. 물론 request.setCharacterEncoding 메소드의 파라메터도 문자셋에 맞도록 올바르게 지정해야 하고…

 

페이지 문자셋이 “EUC-KR” 인 경우



request.setCharacterEncoding(“euc-kr“);

String jdbc_url = “jdbc:mysql://localhost:3306/jsp_test?useUnicode=true&characterEncoding=euckr“;



페이지 문자셋이 “UTF-8” 인 경우



request.setCharacterEncoding(“utf-8“);


String jdbc_url = “jdbc:mysql://localhost:3306/jsp_test?useUnicode=true&characterEncoding=utf8“;


만약 MySQL 서버가 4.x 까지의 버전 이라면 URL에서 “euckr”과 “utf8” 부분을 각각 “euc-kr”, “utf-8″로 바꿔줘야 한다.

아래 예제 프로그램을 참조하자.

페이지 문자셋이 “EUC-KR”인 경우.

form.html

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=EUC-KR“>
<title>Insert title here</title>
</head>
<body>
<form action=”regdb.jsp” method=”post”>
이름 : <input type=”text” name=”name” />
<input type=”submit” value=”저장” />
</form>
</body>
</html>

regdb.jsp


<%@ page language=”java” contentType=”text/html; charset=EUC-KR
    pageEncoding=”EUC-KR“%>
<%@ page import=”java.sql.*” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=EUC-KR“>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding(“euc-kr“);

Connection conn = null;
Statement stmt = null;

String jdbc_driver = “com.mysql.jdbc.Driver”;
String jdbc_url    = “jdbc:mysql://localhost:3306/jsp_test?useUnicode=true&characterEncoding=euckr“;
String jdbc_user   = “jsp_test”;
String jdbc_pwd    = “jsp_test”;

String sql  = null;

String name = request.getParameter(“name”);

sql = “INSERT INTO member_euckr VALUES (‘” + name + “‘)”;

Class.forName(jdbc_driver);

conn = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_pwd);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
%>
<%= sql %><br>
당신의 이름은
<%= name %>
입니다.
</body>
</html>


페이지 문자셋이 “UTF-8″인 경우.

form.html

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8“>
<title>Insert title here</title>
</head>
<body>
<form action=”regdb.jsp” method=”post”>
이름 : <input type=”text” name=”name” />
<input type=”submit” value=”저장” />
</form>
</body>
</html>

regdb.jsp


<%@ page language=”java” contentType=”text/html; charset=UTF-8
    pageEncoding=”UTF-8“%>
<%@ page import=”java.sql.*” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8“>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding(“utf-8“);

Connection conn = null;
Statement stmt = null;

String jdbc_driver = “com.mysql.jdbc.Driver”;
String jdbc_url    = “jdbc:mysql://localhost:3306/jsp_test?useUnicode=true&characterEncoding=utf8“;
String jdbc_user   = “jsp_test”;
String jdbc_pwd    = “jsp_test”;

String sql  = null;

String name = request.getParameter(“name”);

sql = “INSERT INTO member_utf VALUES (‘” + name + “‘)”;

Class.forName(jdbc_driver);

conn = DriverManager.getConnection(jdbc_url,jdbc_user, jdbc_pwd);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
%>
<%= sql %><br>
당신의 이름은
<%= name %>
입니다.
</body>
</html>


위 예제에서 사용된 두 테이블 member_euckr과 member_utf의 구조는 각각 다음과 같다.


CREATE TABLE `member_euckr` (
  `name` varchar(20) NOT NULL
) DEFAULT CHARSET=euckr;


CREATE TABLE `member_utf` (
  `name` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.