|
发表于 2003-10-28 09:06:53
|
显示全部楼层
嗬嗬,我加一个连接可以access(中文查询)的
[code:1]
例:
g_drvSQL = (Driver)Class.forName(MM_testjdbc_DRIVER).newInstance();;
g_connSQL = DriverManager.getConnection(MM_testjdbc_STRING,MM_testjdbc_USERNAME,MM_testjdbc_PASSWORD);
String aszParam[] = {"游客"};
MyOpenResultset("SELECT * FROM 注册用户列表 WHERE realname LIKE ?",aszParam);
返回的ResultSet为:
g_rstSQL
[/code:1]
[code:1]
<%!
// resultset
Driver g_drvSQL = null;
Connection g_connSQL = null;
PreparedStatement g_stmSQL = null;
ResultSet g_rstSQL = null;
public String MyGetColumn(String str) throws Exception{
Object objData;
objData = (((objData = g_rstSQL.getObject(str))==null || g_rstSQL.wasNull())?"":objData);
return MyUnicode2Chinese(objData.toString());
}
public String[] MyFormatsql(ServletRequest request, String columns, String fields, int ntype) throws Exception{
String aszReturn[] = {"",""};
String[] MM_fields = null, MM_columns = null;
try{
//get from form first
java.util.StringTokenizer tokens = new java.util.StringTokenizer(fields,"|");
MM_fields = new String[tokens.countTokens()];
for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken();
tokens = new java.util.StringTokenizer(columns,"|");
MM_columns = new String[tokens.countTokens()];
for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken();
// set the form values
for (int i=0; i+1 < MM_fields.length; i+=2) {
MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):"");
}
if (0==ntype){
StringBuffer buffer = new StringBuffer();
//update
// create the update sql statement
// MM_editQuery = new StringBuffer("update ").append(MM_editTable).append(" set ");
for (int i=0; i+1 < MM_fields.length; i+=2) {
String formVal = MM_fields[i+1];
String elem;
tokens = new java.util.StringTokenizer(MM_columns[i+1],",");
String delim = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
String altVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
if (formVal.length() == 0) {
formVal = emptyVal;
} else {
if (altVal.length() != 0) {
formVal = altVal;
} else if (delim.compareTo("'") == 0) { // escape quotes
StringBuffer escQuotes = new StringBuffer(formVal);
for (int j=0; j < escQuotes.length(); j++)
if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
formVal = "'" + escQuotes + "'";
} else {
formVal = delim + formVal + delim;
}
}
buffer.append((i!=0)?",":"").append(MM_columns[i]).append(" = ").append(formVal);
}
aszReturn[0] = buffer.toString();
}else if(1==ntype){
//insert
StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer();
for (int i=0; i+1 < MM_fields.length; i+=2) {
String formVal = MM_fields[i+1];
String elem;
tokens = new java.util.StringTokenizer(MM_columns[i+1],",");
String delim = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
String altVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
if (formVal.length() == 0) {
formVal = emptyVal;
} else {
if (altVal.length() != 0) {
formVal = altVal;
} else if (delim.compareTo("'") == 0) { // escape quotes
StringBuffer escQuotes = new StringBuffer(formVal);
for (int j=0; j < escQuotes.length(); j++)
if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
formVal = "'" + escQuotes + "'";
} else {
formVal = delim + formVal + delim;
}
}
MM_tableValues.append((i!=0)?",":"").append(MM_columns[i]);
MM_dbValues.append((i!=0)?",":"").append(formVal);
}
aszReturn[0] = MM_tableValues.toString();
aszReturn[1] = MM_dbValues.toString();
}
return aszReturn;
}catch(Exception e){
return null;
}
}
public void MyUpdateResultset(StringBuffer str) throws Exception{
try{
MyUpdateResultset(str.toString());
}catch(Exception e){
}
}
public void MyUpdateResultset(String str) throws Exception{
try{
MyCloseResultset();
// g_stmSQL = g_connSQL.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
g_stmSQL = g_connSQL.prepareStatement(MyChinese2Unicode(str.trim()), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
g_stmSQL.executeUpdate();
MyCloseStatement();
}catch(Exception e){
}
}
public String[] MyString2Array(String src, String szsplit) throws Exception{
String asz[] = null;
try{
java.util.StringTokenizer tokens = new java.util.StringTokenizer(src,szsplit);
asz = new String[tokens.countTokens()];
for (int i=0; tokens.hasMoreTokens(); i++) asz[i] = tokens.nextToken();
}catch(Exception e){
}
return asz;
}
public void MyOpenResultset(StringBuffer str) throws Exception{
try{
MyOpenResultset(str.toString(), null);
}catch(Exception e){
}
}
public void MyOpenResultset(String str) throws Exception{
try{
MyOpenResultset(str, null);
}catch(Exception e){
}
}
public void MyOpenResultset(StringBuffer str, String param[]) throws Exception{
try{
MyOpenResultset(str.toString(), param);
}catch(Exception e){
}
}
public void MyOpenResultset(String str, String param[]) throws Exception{
try{
MyCloseResultset();
g_stmSQL = g_connSQL.prepareStatement(MyChinese2Unicode(str.trim()), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if (param!=null){
for (int i=0; i<param.length; i++) g_stmSQL.setObject(i+1, MyChinese2Unicode(param[i]));
}
g_rstSQL=g_stmSQL.executeQuery();
}catch(Exception e){
}
}
public void MyCloseConnection() throws Exception{
try{
MyCloseResultset();
g_connSQL.close();
g_connSQL=null;
}catch(Exception e){
}
}
public void MyCloseStatement() throws Exception{
try{
g_stmSQL.close();
}catch(Exception e){
}
g_stmSQL=null;
}
public void MyCloseResultset() throws Exception{
try{
MyCloseStatement();
g_rstSQL.close();
}catch(Exception e){
}
g_rstSQL=null;
}
public int MyString2int(String str) throws Exception{
try{
return Integer.parseInt(str);
}catch(Exception e){
return 0;
}
}
public String MyUnicode2Chinese(String s) throws Exception{
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859-1"),"gb2312");
return newstring;
}catch(Exception e){
return s;
}
}
public static String MyChinese2Unicode(String s) throws Exception{
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859-1");
return newstring;
}catch(Exception e){
return s;
}
}
%>
[/code:1] |
|