'可能是执行sql语句引起的冲突,如:update router set keeper='aa'','aa'.......
当然回出错咯,可以把'替换掉,网上有很多这样的文章吧
public class Format { /**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source 源字符串
* @param oldString 老的字符串
* @param newString 新的字符串
* @return 替换后的字符串
*/
public static String Replace(String source, String oldString, String newString) {
StringBuffer output = new StringBuffer(); int lengthOfSource = source.length(); // 源字符串长度
int lengthOfOld = oldString.length(); // 老字符串长度 int posStart = 0; // 开始搜索位置
int pos; // 搜索到老字符串的位置 while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos)); output.append(newString);
posStart = pos + lengthOfOld;
} if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
} return output.toString();
} /**
* 将字符串格式化成 HTML 代码输出
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtml(String str) {
String html = str; html = Replace(html, "&", "&");
html = Replace(html, "<", "<");
html = Replace(html, ">", ">");
html = Replace(html, "\r\n", "\n");
html = Replace(html, "\n", "<br>\n");
html = Replace(html, "\t", " ");
html = Replace(html, " ", " "); return html;
} /**
* 把单引号替换为双单引号,用于MSsql
*/ public static String toSql(String sqlstr) {
String strsql = sqlstr; strsql = Replace(strsql, "'", "''");
return strsql;
}}
当然回出错咯,可以把'替换掉,网上有很多这样的文章吧
public class Format { /**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source 源字符串
* @param oldString 老的字符串
* @param newString 新的字符串
* @return 替换后的字符串
*/
public static String Replace(String source, String oldString, String newString) {
StringBuffer output = new StringBuffer(); int lengthOfSource = source.length(); // 源字符串长度
int lengthOfOld = oldString.length(); // 老字符串长度 int posStart = 0; // 开始搜索位置
int pos; // 搜索到老字符串的位置 while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos)); output.append(newString);
posStart = pos + lengthOfOld;
} if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
} return output.toString();
} /**
* 将字符串格式化成 HTML 代码输出
*
* @param str 要格式化的字符串
* @return 格式化后的字符串
*/
public static String toHtml(String str) {
String html = str; html = Replace(html, "&", "&");
html = Replace(html, "<", "<");
html = Replace(html, ">", ">");
html = Replace(html, "\r\n", "\n");
html = Replace(html, "\n", "<br>\n");
html = Replace(html, "\t", " ");
html = Replace(html, " ", " "); return html;
} /**
* 把单引号替换为双单引号,用于MSsql
*/ public static String toSql(String sqlstr) {
String strsql = sqlstr; strsql = Replace(strsql, "'", "''");
return strsql;
}}
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement("update Router set Keeper=?, SlotNum=? where EquipmentID=?"
pstmt.setString(1,Keeper);
pstmt.setInt(2,SlotNum);
pstmt.setInt(EquipmentID);
pstmt.executeUpdate();
试试吧!
pstmt.setInt(3,EquipmentID);
不用关心'的问题.
例如:
PreparedStatement pstmt = conn.prepareStatement("Insert into IP(IP,Mask,PossessID1,"+
"PossessID2,Descri,State,Attribute) values(?,?,?,?,?,?,?)");
pstmt.setString(1,IP.trim()); pstmt.setString(2,Mask.trim());
pstmt.setString(3,PossessID1.trim()); pstmt.setString(4,PossessID2);
pstmt.setString(5,Description.trim()); pstmt.setString(6,State.trim());
pstmt.setString(7,Attribute.trim());
pstmt.executeUpdate();
它是无中文乱码问题.
// stmt.executeUpdate("update Router set Keeper='"+Keeper+"',SlotNum='"+iSlotNum +"' where //EquipmentID='"+EquipmentID+"'");
//但万一变量Keeper有一字符是 ' ,这语句就不能执行了。所以还是希望用前面的办法。对特殊字符'进行处理不就完了,在sybase ASE, ASA 下是把'边为''
像上面的程序你碰到big5,也来一次碰到日文你又单独处理?
当然你也可以通过将要写的数据转换为Byte型的数据,而不使用String来避免乱码,不过读数据库时一样要通过读字节数据来组成String,这仅适用于该数据库只由你的JAVA程序使用的情况,否则别的语言编写的程序可能无法读取你的JAVA程序写入的数据(也可以转换)
我从来没有使用过JB,慢如牛.
把中文转换为Byte型,是否是二进制的内码,如果是,那也太麻烦了,又不是多媒体数据.
不过也不失为一种解决方法.