1. 程式人生 > >用JAVA連線SQL實現刪除資料

用JAVA連線SQL實現刪除資料

刪除一條資料

<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>刪除一條記錄頁面</title>
</head>
<body>
<% 		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//載入JDBC驅動
		String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=zero";
//連線伺服器和資料庫
		String userName = "sa"; // 預設使用者名稱
		String userPwd = "123456"; // 密碼
		Connection dbConn = null;
		try {
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		request.setCharacterEncoding("UTF-8");//設定字元編碼,避免出現亂碼
		String sql="delete from stu_info where weight>=?";//sql語句
		PreparedStatement pstmt=dbConn.prepareStatement(sql);//建立介面物件
		pstmt.setFloat(1, 60);
		//pstmt.setString(2, "嘻嘻");
		int n=pstmt.executeUpdate();//這裡面不需要引數
		
		if(n>=1){%> 資料刪除成功!<br> <%}
		else{%> 資料刪除失敗!<br> <%}
		if(pstmt!=null) {pstmt.close();}
		if(dbConn!=null) {dbConn.close();}
	%>
</body>
</html>

提交刪除條件頁面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>刪除條件提交頁面</title>
</head>
<body>
請選擇刪除記錄條件<hr width="100%" size="3">
<form action="delete_stu_2.jsp" method="post">
	姓名:<input type="text" name="name"><br><br>
	性別:男<input type="radio" value="男" name="sex">
		   女<input type="radio" value="女" name="sex"><br><br>
	體重範圍:<p>
		最小:<input type="text" name="w1"><br><br>
		最大:<input type="text" name="w2"><p>
		   <input type="submit" value="提交">
		   &nbsp;&nbsp;&nbsp;&nbsp;<!-- 這是4個空格,最後網頁會變成:提交    取消 -->
		   <input type="reset" value="取消">
</form>
</body>
</html>

顯示刪除結果

<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>利用提交條件刪除記錄頁面</title>
</head>
<body>
<% 		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//載入JDBC驅動
		String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=zero";
//連線伺服器和資料庫
		String userName = "sa"; // 預設使用者名稱
		String userPwd = "123456"; // 密碼
		Connection dbConn = null;
		try {
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		request.setCharacterEncoding("UTF-8");//設定字元編碼,避免出現亂碼
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String ww1=request.getParameter("w1");
		String ww2=request.getParameter("w2");
		String s="1=1";
		if(!name.equals(""))s=s+" and name='"+name+"'";//equals()方法是判斷一個值是否等於另一個值,
													//就是判斷姓名是否為空,注意這裡是單引號,如表示:and name='張三'
		if(sex!=null)s=s+" and sex='"+sex+"'";      //s的作用是用來組成SQL語句
		float w1,w2;
		if(!ww1.equals("")){w1=Float.parseFloat(ww1);s=s+"and weight>="+w1;}
		if(!ww2.equals("")){w2=Float.parseFloat(ww2);s=s+"and weight<="+w2;}
		String sql="delete from stu_info where "+s;//sql語句
		PreparedStatement pstmt=dbConn.prepareStatement(sql);//建立介面物件
		
		int n=pstmt.executeUpdate();//這裡面不需要引數
		/*
		executeUpdate 的返回值是一個整數,指示受影響的行數(即更新計數)。
		對於 CREATE TABLE 或 DROP TABLE 等不操作行的語句,executeUpdate 的返回值總為零。 
		就是刪除了多少行
		*/
		if(n==1){%> 資料刪除成功!<br> <%}
		else{%> 資料刪除失敗!<br> <%}
		if(pstmt!=null) {pstmt.close();}
		if(dbConn!=null) {dbConn.close();}
%>
</body>
</html>