Java i cirilica u mysql bazi

Započeo maxogm, 27.12.2010, 00:09

prethodna tema - sledeća tema

maxogm

27.12.2010, 00:09 Poslednja Izmena: 27.12.2010, 00:12 od maxogm
Pri kreiranju baze postaviti default character set i collation

CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;


contex.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/xxxx?useUnicode=true&amp;characterEncoding=utf8&amp;charactetrResultSets=utf8"
username="xxxx" password="xxxx" maxActive="20" maxIdle="10"
maxWait="-1" />
</Context>


Ukoliko koristite neki vas DB manager potrebano je na database url dopisati:

jdbc:mysql://localhost:3306/cirilica?useUnicode=true&characterEncoding=utf8&charactetrResultSets=utf8


Na jsp strani podesiti:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


Ukoliko koristite servlet kao kontroler potreno je da parametre primate na sledeci nacin:

String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");


index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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="Controler">
<input type="text" name="name"/>
<input type="submit" value="Submit" />
</form>
${msg}
</body>
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>TestUTF</display-name>
<servlet>
<description>
</description>
<display-name>Controler</display-name>
<servlet-name>Controler</servlet-name>
<servlet-class>rs.in.jmax.cirilica.servlet.Controler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controler</servlet-name>
<url-pattern>/Controler</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


Controler.java

package rs.in.jmax.cirilica.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.in.jmax.cirilica.db.UserDAO;

/**
* Servlet implementation class Controler
*/
public class Controler extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
UserDAO dao = new UserDAO();
request.setAttribute("msg", dao.insertName(name));
request.getRequestDispatcher("index.jsp").forward(request, response);
}

}


UserDAO.java

/**
*
*/
package rs.in.jmax.cirilica.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
* @author kowalski
*
*/
public class UserDAO {

private DataSource ds = null;

public UserDAO() {
try {
InitialContext cxt = new InitialContext();
if (cxt == null) {

}
ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/mysql");
if (ds == null) {

}
} catch (NamingException e) {
e.printStackTrace();
}
}

public String insertName(String name) {
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
String retVal = null;

try {
// con = DriverManager
// .getConnection(
// "jdbc:mysql://localhost:3306/cirilica?useUnicode=true&characterEncoding=utf8&charactetrResultSets=utf8",
// "root", "toor");
con = ds.getConnection();
pstm = con.prepareStatement("INSERT INTO user (name) VALUES (?)");
pstm.setString(1, name);
pstm.execute();

pstm.clearParameters();
pstm = con.prepareStatement("SELECT * FROM user ORDER BY id DESC LIMIT 1");
rs = pstm.executeQuery();

while (rs.next()) {
retVal = rs.getString("name");
}

} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return retVal;
}

}