createdb -E unicode idg
create table area (area_id int, area_name varchar(32))
html encoding: iso8859_1
test: mozilla, links(set encoding to iso8859_1)
enviroment: rh72 english / resin-3 / postgresql-7.3.2 / j2sdk1.4.1 / pg73jdbc3.jar
/////////
package web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.*;
public class Sender extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html; charset=iso8859_1");
req.setCharacterEncoding("iso8859_1");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String area = "上海";
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/idg";
String user = "idg";
String passwd = "idg";
String update = "insert into area values (21, ?)";
String select = "select * from area";
PrintWriter pw = resp.getWriter();
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
stmt = conn.prepareStatement(update);
// stmt.setInt(1, 21);
// stmt.setString(1, new String(area.getBytes(), "GBK"));
stmt.setString(1, area);
stmt.executeUpdate();
rs = stmt.executeQuery(select);
while(rs.next())
{
int areaId = rs.getInt("area_id");
String areaName = rs.getString("area_name");
pw.println( areaId + " " + areaName + "<br>");
}
}
catch (Exception e)
{
e.printStackTrace(pw);
}
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(pw);
}
}
}
}