package com.riskiq.grid.dao.basic; import java.sql.*; import java.util.Date; import java.util.*; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; /** * checks to make sure the basic dao implementation actually works * * @author ck * @version $Revision: 422 $ $Date: 2008-02-13 18:54:05 -0800 (Wed, 13 Feb 2008) $ */ public class UnicodeTest extends AbstractDependencyInjectionSpringContextTests { @Autowired @Qualifier(value = "/riskiq/DataSource") private DataSource dataSource; /** ensure we autowire by name */ public UnicodeTest() { setAutowireMode(AUTOWIRE_BY_TYPE); } /** {@inheritDoc} */ protected String[] getConfigLocations() { return new String[]{ "classpath:/spring/configuration.xml", "classpath:/spring/dao.xml", "classpath:/spring/test/dao-test.xml", }; } public void testUnicode2() throws Exception { String name = "不一样的星期五"; Connection conn = null; PreparedStatement pstmt = null; Statement stmt = null; ResultSet rs = null; try { conn = dataSource.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("show session variables like '%char%'"); System.out.println("----------------------"); while (rs.next()) { System.out.println(rs.getString(1) + ": " + rs.getString(2)); } System.out.println("----------------------"); stmt.executeUpdate("drop table if exists test_model"); stmt.executeUpdate("create table test_model ( " + "test_id integer not null auto_increment, " + "name varchar(32), " + "primary key (test_id)" + " ) charset=utf8"); pstmt = conn.prepareStatement("insert into test_model (test_id, name) values (?, ?)"); pstmt.setInt(1, 100); pstmt.setString(2, name); pstmt.executeUpdate(); conn.commit(); rs = stmt.executeQuery("select name from test_model where test_id = 100"); rs.next(); String s = rs.getString(1); System.out.println("----- name: " + s); // works fine assertEquals("name", name, s); } finally { try { if (rs != null) rs.close(); } catch (SQLException e) { /* na */ } try { if (stmt != null) stmt.close(); } catch (SQLException e) { /* na */ } try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { /* na */ } try { if (conn != null) conn.close(); } catch (SQLException e) { /* na */ } } } }