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>
"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");
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;
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 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>
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>
"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");
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;
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 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>
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;
`name` varchar(20) NOT NULL
) DEFAULT CHARSET=euckr;
CREATE TABLE `member_utf` (
`name` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;
`name` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;
'프로그래밍 > JSP' 카테고리의 다른 글
CVS로 부터 이클립스로 프로젝트 가져오기 (0) | 2009.07.05 |
---|---|
CVS에 이클립스 프로젝트 등록 (0) | 2009.07.05 |
NetBeans에서 JSP 개발하기 (0) | 2009.06.07 |
JDBC 드라이버를 이용한 JSP와 MySQL 연동 (0) | 2009.03.30 |
웹프로그램의 개발 목적 (0) | 2009.03.24 |