日期:2014-05-20 浏览次数:21071 次
Connection con = null;
try
{
String sql = "";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, "你的参数");
pstmt.executeUpdate();
pstmt.close();
}catch(Exception e)
{
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
if(con != null)
{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------解决方案--------------------
public static String getSafeSQL(String inStr) {
String result = "";
try {
inStr = inStr.trim();
char c;
int strLen = inStr.length();
for (int i = 0; i < strLen; i++) {
c = inStr.charAt(i);
switch (c) {
case '\'':
result = result + "''";
break;
case '\\':
result = result + "\\\\";
break;
default:
result = result + String.valueOf(c);
break;
}
}
} catch (Exception e) {
return "";
}
return result;
}