请问有没有jdbc存取中文的例了?

xiaoyoung 2001-08-28 11:23:29
多谢!
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamfancy 2001-08-29
  • 打赏
  • 举报
回复
语言环境设置为:zh_CN,
数据库编码方式:EUC_CN
搞定
给分
snmmc 2001-08-28
  • 打赏
  • 举报
回复
你看看这个例子吧

import java.lang.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

public class IsItWorking extends HttpServlet {

public static final String TITLE = "我们来测试 okokokok!!!!";
private static Connection conn = null;

String native2unicode(String s)
{
if(s==null || s.length()==0)
{
return null;
}

byte [] buffer = new byte[s.length()];

for(int i=0;i<s.length();i++)
{
buffer[i] = (byte)s.charAt(i);
}

return new String(buffer);
}

String unicode2native(String s)
{
if(s==null || s.length()==0)
{
return null;
}

char [] buffer = new char[s.length() * 2];

char c;
int j=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)>=0x100)
{
c = s.charAt(i);
byte [] buf = (""+c).getBytes();
buffer[j++] = (char)buf[0];
buffer[j++] = (char)buf[1];
}
else
{
buffer[j++] = s.charAt(i);
}
}

return new String(buffer,0,j);
}

public void service (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
}
catch ( Exception e )
{
out.println("Could not establish connection.");
}

out.println("Success Oracle Driver JJJJJJJJJJ<BR>\n");

out.println(TITLE);

byte [] by = TITLE.getBytes("GB2312");
String unicode = new String(by,"8859_1");

out.println(unicode+"<BR>\n");

try
{
try
{
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.6:1521:ORCL", "expert","expert");
}
catch(SQLException e)
{
out.println("Connect Error<BR>\n");
}

out.println("conn successfully");

Statement stmt = null;
try
{
stmt = conn.createStatement ();
}
catch(SQLException e)
{
out.println("STMT Error<BR>\n");
}

ResultSet rset;
try
{
//rset = stmt.executeQuery ("select modulename from modmethodinter");
/*
//SQL 语句嵌入汉字处理方法。
String szSQL = "insert into test values('测试')";

String szTemp;// = unicode2native(szSQL);

byte [] b = szSQL.getBytes("8859_1");

szTemp = new String(b,"GB2312");
*/

String szSQL = "select name from test";
rset = stmt.executeQuery (szSQL);

String szOut;
String szTemp;

while(rset.next())
{
szTemp = rset.getString("name");
byte [] b = szTemp.getBytes("GB2312");
szOut = new String(b,"8859_1");
out.println(szOut+"<BR>\n");
}
rset.close();

}
catch(SQLException e)
{
out.println("SQL Execute Error<BR>\n");
stmt.close();
conn.close();
return;
}

stmt.close();
conn.close();

out.println("SQL Execute Successfully<BR>\n");
}
catch(Exception e)
{
}

/*
// set content type and other response header fields first
response.setContentType("text/html");

ServletOutputStream out_put = response.getOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(out_put,"8859_1");

try
{
// byte [] b = TITLE.getBytes("GB2312");
// String unicode = new String(b,"8859_1");
//NT out.println(unicode);
//Linux
ow.write(TITLE);
ow.close();
}
catch( UnsupportedEncodingException e)
{
out_put.println("Error!");
}
// write the data
//stockDb db = new stockDb();
*/
}
public void doGet ( HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();

out.println("Hello World");

try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
}
catch ( Exception e )
{
out.println("Could not establish connection.");
}

out.println("Success Oracle Driver<BR>\n");

try
{
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.6:1521:ORCL", "expert","expert");

out.println("conn successfully");

Statement stmt = conn.createStatement ();

// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("insert into test values('测试')");

/*
while(rset.next())
{
out.println("TEST");//(rset.getBinaryStream(1));
}
*/
rset.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
}

}//doGet
}
xiaoyoung 2001-08-28
  • 打赏
  • 举报
回复
存取到postgres数据库里
snmmc 2001-08-28
  • 打赏
  • 举报
回复
是存取到什么地方?
xiaoyoung 2001-08-28
  • 打赏
  • 举报
回复
你的方法我已经试过了,不行呀
:(

62,610

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧