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>利用PreparedStatement物件新增一條記錄頁面</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);
		//	System.out.println("Connection Successful!");
			// 如果連線成功 控制檯輸出Connection Successful!
		} catch (Exception e) {
			e.printStackTrace();
		//	System.out.println("連線失敗");
		}
		
		String sql="Insert into stu_info(id,name,sex,age,weight,hight) values(?,?,?,?,?,?)";
		PreparedStatement pstmt=dbConn.prepareStatement(sql);
		pstmt.setInt(1, 16);
		pstmt.setString(2, "張三");
		pstmt.setString(3, "男");
		pstmt.setInt(4, 20);
		pstmt.setFloat(5,70);
		pstmt.setFloat(6,175);
		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>
<form action="insert_stu_2.jsp" method="post">
	<table border="0" width="238" height=252"">
		<tr><td>學號</td><td><input type="text" name="id"></td></tr>
		<tr><td>姓名</td><td><input type="text" name="name"></td></tr>
		<tr><td>性別</td><td><input type="text" name="sex"></td></tr>
		<tr><td>年齡</td><td><input type="text" name="age"></td></tr>
		<tr><td>體重</td><td><input type="text" name="weight"></td></tr>
		<tr><td>身高</td><td><input type="text" name="hight"></td></tr>
		<tr align="center">
			<td colspan="2">
				<input type="submit" value="提 交">&nbsp;&nbsp;&nbsp;<!-- 表示空格,因為表格中如果是手打的空格會合併為一個 -->
				<input type="reset" value="取 消">
				</td>
				</tr>
	</table>
</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>利用PreparedStatement新增一條記錄</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);
		//	System.out.println("Connection Successful!");
			// 如果連線成功 控制檯輸出Connection Successful!
		} catch (Exception e) {
			e.printStackTrace();
		//	System.out.println("連線失敗");
		}
		String sql="Insert into stu_info(id,name,sex,age,weight,hight) values(?,?,?,?,?,?)";
		PreparedStatement pstmt=dbConn.prepareStatement(sql);
		request.setCharacterEncoding("UTF-8");//設定字元編碼,避免出現亂碼
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		int age=Integer.parseInt(request.getParameter("age"));
		float weight=Float.parseFloat(request.getParameter("weight"));
		float hight=Float.parseFloat(request.getParameter("hight"));
		
		pstmt.setInt(1,id);//這裡不能有空格,雖然這裡並不會報錯,但執行會出現錯誤500提示。
		pstmt.setString(2,name);
		pstmt.setString(3,sex);
		pstmt.setInt(4,age);
		pstmt.setFloat(5,weight);
		pstmt.setFloat(6,hight);
		int n=pstmt.executeUpdate();
		if(n==1){%> 資料插入成功!<br> <%}
		else{%> 資料插入失敗!<br> <%}
		if(pstmt!=null) {pstmt.close();}
		if(dbConn!=null) {dbConn.close();}
%>
</body>
</html>