資料修改-----資料庫
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>
<html>
<head>
<title>使用PreparedStatement修改記錄頁面</title>
</head>
<body>
<%
String driverName = "com.mysql.jdbc.Driver"; //驅動程式名
String userName = "root"; //資料庫使用者名稱
String userPwd = "123456"; //密碼
String dbName = "students"; //資料庫名
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2 ="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=GB2312";
String url =url1+url2+url3; //形成帶資料庫讀寫編碼的資料庫連線字
Connection conn=null;
PreparedStatement pstmt=null;
String sql;
try{
Class.forName(driverName);
conn=DriverManager.getConnection(url);
sql="update stu_info set weight=? where name=?";
pstmt= conn.prepareStatement(sql);
pstmt.setFloat(1,80);
pstmt.setString(2,"張三");
int n= pstmt.executeUpdate();
if(n==1){%>
資料修改操作成功!<br>
<%}
else{%>
資料修改操作失敗!<br>
<%}
}catch(Exception e){%>
修改過程出現異常錯誤!<br>
<%=e.getMessage()%>
<%
}
if(pstmt!=null){ pstmt.close(); }
if(conn!=null){ conn.close(); }
%>
</body>
</html>