扫一扫
关注微信公众号

Servlet汉字问题解决办法
2007-09-26   网络

Servlet汉字问题解决办法。
//要在Linux上编译,JDK1.2,JSWDK1.0.1,
//时区 US-EAST

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
= (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> ");

out.println(TITLE);

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

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

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

out.println("conn successfully");

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

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> ");
}
rset.close();

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

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

out.println("SQL Execute Successfully<BR> ");
}
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> ");

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
}

热词搜索:

上一篇:redhat6.1+apache+tomcat安装指南
下一篇:tomcat 3.1在RedHat下的安装

分享到: 收藏