老兄,
假设你的数据库中有个table 叫user,有两个字段ID,Name.
1,query from table user.
在class 中声明 Connection con;
在
//init
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc
dbc:test");//test为ODBC DSN
dispStmt = con.createStatement();
}//end of try
catch (Exception e) {
throw(new UnavailableException(this,"Sorry!The database did not load!/r The Info is:"+e.toString()));
} //end of catch
}//end of function init
public String getName(String aID)
throws IOException ,UnsupportedEncodingException
{
byte[] nm;
String name="",
sql;//debug use
try{
Statement dispStmt = con.createStatement();
ResultSet rs ;
synchronized(this)
{
sql = "select NAME from user where (ID = /'"+aID+"/')";
rs = dispStmt.executeQuery(sql);
}//end of synchronized
while(rs.next()){
nm = rs.getBytes("CLASSNAME");
ClassName = (new String(nm,"8859_1"));
}//end of while
dispStmt.close();
}//end of try
catch (Exception e) {
out.println("/r ERROR in getClassName:"+e.toString()+"/r");
}//end of catch
// Catolog.setQueryTimeout();
return ClassName;
}//end of function getClassName
2, save Chinese string to database
存盘时将中文string 转换成 ByteArrayInputStream;
public ByteArrayInputStream ChineseToAscii(String str)
throws UnsupportedEncodingException {
return new ByteArrayInputStream(str.getBytes("8859_1"));
}//end of function
然后,用
String userName = "中文";
...
updateStat.setAsciiStream(1,ChineseToAscii(userName),userName.length());
.....
保存.就OK啦。
以上是在Jbuilder3.0+sql7.0下通过的程序。
有问题再联系。
larry