unicodeconvertjdbctest.java

来自「工厂版本管理系统,STRUTS2框架,用于管理商品的版本,便于有效的控制版本」· Java 代码 · 共 146 行

JAVA
146
字号
package com.dingl.jdbc;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import junit.framework.TestCase;

/**
 * 测试JDBC PROXY能否正确完成中文转换操作
 * @author Administrator
 *
 */
public class UnicodeConvertJDBCTest extends CommonTestCase{
	
	
	public void tearDown() throws Exception {
	}
	
	/**
	 * 测试能否根据中文字符串正确查询到中文信息
	 * @throws Exception
	 */
	
	public void testFfindByChinseCharacterSet() throws Exception {
		String tt = "猪仔";
		String sql = "  select *   from B_CHINESE_TEST    where trim(name) = ? ";
		PreparedStatement stmt =  conn.prepareStatement(sql);
		stmt.setString(1, tt);
		ResultSet rs = stmt.executeQuery();
		assertTrue(rs.next());
		do {
			System.out.println("id   " + rs.getString(1) + "Address "
					+ rs.getString(2) + "  CITY   " + rs.getString(2));
		} while (rs.next()); 
		rs.close();
		stmt.close();
		conn.close();
	}

	/**
	 * 测试能否正确执行含中文字符的sql
	 * @throws SQLException
	 * @throws UnsupportedEncodingException 
	 */

	public void testExecuteChineseSQlByPrepare() throws SQLException, UnsupportedEncodingException {
		String sql = "  select *   from B_CHINESE_TEST    where trim(name) = '猪仔' ";
//		sql = new String(sql.getBytes(), "iso8859-1");
		System.out.println(sql);
		PreparedStatement stmt =  conn.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		assertTrue(rs.next());
		do {
			System.out.println("id   " + rs.getString(1) + "Address "
					+ rs.getString(2) + "  CITY   " + rs.getString(2));
		} while (rs.next()); 
		rs.close();
		stmt.close();
		conn.close();
	}
	
	/**
	 * 测试能否正确执行含中文字符的sql
	 * @throws SQLException
	 * @throws UnsupportedEncodingException 
	 */

	public void testExecuteChineseSQlByStatement() throws SQLException, UnsupportedEncodingException {
		String sql = "  select *   from B_CHINESE_TEST    where trim(name) = '猪仔' ";
//		sql = new String(sql.getBytes(), "iso8859-1");
		System.out.println(sql);
		Statement stmt =  conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		assertTrue(rs.next());
		do {
			System.out.println("id   " + rs.getString(1) + "Address "
					+ rs.getString(2) + "  CITY   " + rs.getString(2));
		} while (rs.next()); 
		rs.close();
		stmt.close();
		conn.close();
	}

	public void testExecuteChineseSQl() throws SQLException, UnsupportedEncodingException {
		String sql = "  select count(*) from demo_table where name = ? ";
		PreparedStatement stmt =  conn.prepareStatement(sql);
		stmt.setString(1, "顶顶顶");
		ResultSet rs = stmt.executeQuery();
		assertTrue(rs.next());
		do {
			System.out.println("id   " + rs.getString(1));
		} while (rs.next()); 
		rs.close();
		stmt.close();
		conn.close();
	}
	
	
	public void testUpdateChinese() throws SQLException, UnsupportedEncodingException {

		PreparedStatement pstmt =

		conn.prepareStatement("INSERT INTO B_CHINESE_TEST (zid, name) VALUES(?,?)");

		pstmt.setString(1, "0877");
		pstmt.setString(2, "中文之猪");
		pstmt.execute();
		pstmt.close();
		
		
		String sql = "  select *   from B_CHINESE_TEST    where trim(name) = '中文之猪' ";
		Statement stmt =  conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		assertTrue(rs.next());
		do {
			System.out.println("id   " + rs.getString(1) + "Address "
					+ rs.getString(2) + "  CITY   " + rs.getString(2));
		} while (rs.next()); 
		rs.close();
		
		
		stmt.execute("delete from B_CHINESE_TEST where trim(name) = '中文之猪' ");
		stmt.close();
		conn.close();
	}
	

	public void testCreateTestTable() throws SQLException {
		String sql = "  CREATE TABLE B_CHINESE_TEST(zid char(20), name varchar2(100))";
		Statement stmt =  conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		sql = "  INSERT INTO B_CHINESE_TEST (zid, name) VALUES('01','猪仔')";
		rs = stmt.executeQuery(sql);
		sql = "  INSERT INTO B_CHINESE_TEST (zid, name) VALUES('02','猪仔2')";
		rs = stmt.executeQuery(sql);
		rs.close();
		stmt.close();
		conn.close();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?