- 用jsp刪除數據庫數據時數據庫無返回。但是如果SQL語句直接寫出變量,返回刪除成功了。
-
<%
response.setContentType("text/html");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"GBK");
out.print(name);
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加載JDBC驅動
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=化妝品管理系統"; //連接服務器和數據庫sample
String userName = "sa"; //默認用戶名
String userPwd = "123456"; //密碼
Connection conn;
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement stat = conn.createStatement();
String sql =
"DELETE FROM customers WHERE customername ='郭海濤'";
int i = stat.executeUpdate(sql);
out.println(i);
%>
** "DELETE FROM customers WHERE customername ='郭海濤'";**
這樣就可以刪除成功。
** "DELETE FROM customers WHERE customername ='name'";**
這樣就刪除不了。
但是打印出來name的值就是‘郭海濤’,沒有亂碼。
最佳回答:
如果使用的是這句話,"DELETE FROM customers WHERE customername ='name'",這句話被包含在雙引號中間,顯然是個字符串,其中的name被當做字符串的一部分,所以送給數據庫的語句就是DELETE FROM customers WHERE customername ='name',也就是說你的變量name沒有被'郭海濤'替換掉,你可以使用"DELETE FROM customers WHERE customername =" +'"' + name+ '"'這一句,將變量放在字符串外面。