- 论坛徽章:
- 0
|
本帖最后由 hsyll 于 2011-12-16 09:34 编辑
以前没有用java写过东西,这一次真晕了,环境:tomcat6+sqlserver2005 tomcat 装在linux(centerOS)上
$java --version
java version "1.4.2"
gij (GNU libgcj) version 4.1.2 20080704 (Red Hat 4.1.2-46)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
--------------------------------------------------------------------------------------------------------------
Server Information
Tomcat Version JVM Version JVM Vendor OS Name OS Version OS Architecture
Apache Tomcat/6.0.33 1.7.0_01-b08 Oracle Corporation Linux 2.6.18-164.el5 i386
----------------------------------------------------------------------------------------------------------------------------
linux的中文环境是如下设置:
,
cat /etc/sysconfig/i18n
#LANG="en_US.UTF-8"
#SYSFONT="latarcyrheb-sun16"
LANG="zh_CN.GBK"
LANGUAGE="zh_CN.GBK:zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT="lat0-sun16"
在/etc/profile 最后有一句:
export LC_ALL=zh_CN.GBK
数据库用的是sqlserver 2005,装在windows2003机器上
在redhat 的机器上classpath里有sqljdbc4.jar,这个jdbc驱动没问题
两个文件如下:
ConnDB.java
-----------------------------------------------
import java.sql.*;
public class ConnDB {
private String url;
private String user;
private String password;
private Connection conn;
ConnDB()
{
url="jdbc:sqlserver://192.168.1.4:1433;databaseName=db_test";
user="test";
password="123456";
conn=null;
}
public Connection getConn()
{
try {
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(url,user,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
---------------------------------------------------------------------------
ShowInfo.java
----------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class ShowQuxianInfo extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
Statement sm=null;
ResultSet rs=null;
Connection ct=null;
PrintWriter pw=res.getWriter();
try{
res.setContentType("text/html;charset=gbk");
ConnDB conndb=new ConnDB();
ct=conndb.getConn();
sm=ct.createStatement();
rs=sm.executeQuery("select * from tb_info");
printinfo(pw,rs);
}
catch(Exception ex)
{
ex.printStackTrace();
}finally
{
try{
if (rs!=null){rs.close();}
if (sm!=null){sm.close();}
if(ct!=null){ct.close();}
if (pw!=null){pw.close();}
}
catch(Exception ex){ ex.printStackTrace();}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
this.doGet(req,res);
}
private void printinfo(PrintWriter pw,ResultSet rs) throws ServletException,IOException, SQLException
{
pw.println("</html><head>");
pw.println("<titile>信息</title>");
pw.println("</head><body>");
//调试代码开始
//------------------------------------------------------------------------------------
String testString=new String("实验中文");
pw.println(testString);
pw.println(new String(testString.getBytes("ISO-8859-1"),"gb2312"));
pw.println(new String(testString.getBytes("UTF8"),"gb2312"));
pw.println(new String(testString.getBytes("GB2312"),"gb2312"));
pw.println(new String(testString.getBytes("GBK"),"gb2312"));
pw.println(new String(testString.getBytes("BIG5"),"gb2312"));
//调试代码结束
//-------------------------------------------------------------------------------------
pw.println();
pw.println("<table border=1><caption>信息</cation>");
pw.println("<tr><th>编号</th><th>名称</th></tr>");
while(rs.next())
{
pw.println("<tr>");
pw.println("<td>"+rs.getString("id")+"</td>");
pw.println("<td>"+rs.getString("name")+"</td>");
pw.println("</tr>");
}
pw.println("</table></body></html>");
}
}
在linux编译参数如下:
javac -encoding gbk *java
编译没有问题,生成两个class文件
---------------------------------------------------------------------------
输出结果如下:
???? ???? ???? ?????? ???? ???? ????
???? ???? ????
01 ???
02 ???
03 ???
04 ???
05 ???
06 ???
07 ???
08 ???
09 ???
10 ??
11 ???? |
|