- 论坛徽章:
- 1
|
Java 中文问题的解决 mysql, oracle, servlet, jsp (2002-11-14)
以下是我的测试代码,No problem now.
客户端直接连的,用jsp,我这边现在没环境。不过没什么区别了。
基本上和楼主说的方式一样,但是getBytes不能带编码参数,否则还是有问题。
- package testpostgres;
- import java.util.*;
- import java.io.*;
- import java.sql.*;
- public class TestMain
- {
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- String driver = "org.postgresql.Driver";
- String url = "jdbc:postgresql://dbserver:5432/testdb";
- String user = "postgres";
- String passwd = null;
- String update = "";
- String select = "select * from area";
- try
- {
- Class.forName(driver);
- conn = DriverManager.getConnection(url, user, passwd);
- stmt = conn.createStatement();
- String str = "上海";
- PreparedStatement pStmt = conn.prepareStatement("insert into area values (21, ?)");
- pStmt.setString(1,new String(str.getBytes(),"gb2312"));
- pStmt.executeUpdate();
- rs = stmt.executeQuery(select);
- while(rs.next())
- {
- int areaId = rs.getInt("area_id");
- String areaName = rs.getString("area_name");
- System.out.println( areaId + ": " + areaName + "<br>;");
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- // Always close properly
- if(rs != null)
- {
- rs.close();
- }
- if(stmt != null)
- {
- stmt.close();
- }
- if(conn != null)
- {
- conn.close();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
复制代码 |
|