Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » Other DB  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 postgresql + jdbc + servlet 中文问题已经解决
hzxl





发贴: 169
积分: 40
于 2003-08-31 17:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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);
}
}
}
}




话题树型展开
人气 标题 作者 字数 发贴时间
12274 postgresql + jdbc + servlet 中文问题已经解决 hzxl 1917 2003-08-31 17:38

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923