程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp---預處理語句對象PreparedStatement

jsp---預處理語句對象PreparedStatement

編輯:關於JSP

預處理語句對象PreparedStatement,使用PreparedStatement進行添加數據,更新數據,刪除數據和查詢數據
 
添加數據
 
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.sql.*" %>
03
<!DOCTYPE html>
04
<html>
05
    <head>
06
        <title>獲得第二條記錄開始的三條記錄</title>
07
    </head>
08
    <body>
09
        <%
10
            String url = "jdbc:mysql://localhost:3306/javaweb";//連接數據庫的url地址
11
            String user = "root";//登錄數據庫的用戶名
12
            String password = "zhangda890126;;";//登錄數據庫的用戶名的密碼
13
            Connection conn = null;//鏈接對象
14
            PreparedStatement pstmt = null;//語句對象
15
            //ResultSet rs = null;//結果集對象
16
            try{
17
                Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序
18
                conn = DriverManager.getConnection(url,user,password);//鏈接數據庫
19
            }catch(ClassNotFoundException e){
20
                out.println("找不到驅動類");//拋出異常時,提示信息
21
            }catch(SQLException e){
22
                out.println("鏈接MySQL數據庫失敗");//處理SQLException異常
23
            }
24
            try{
25
                String adduser = "INSERT INTO user (userid,username,password) VALUES(null,?,?)";//添加一條用戶信息
26
                pstmt = conn.<span style="color:#e53333;"><b>prepareStatement</b></span>(adduser);//創建預處理語句對象PreparedStatement
27
                
28
                //設置參數
29
                pstmt.setString(1,"YAO");
30
                pstmt.setString(2,"yao");
31
                
32
                //執行語句
33
                pstmt.executeUpdate();
34
                
35
            }catch(SQLException e){
36
                out.println("添加用戶信息失敗");
37
            }
38
            
39
            try{
40
                if(pstmt != null){
41
                    pstmt.close();
42
                    conn = null;
43
                }
44
                if(conn != null){
45
                    conn.close();
46
                    conn = null;
47
                }
48
            }catch(Exception e){
49
            
50
                out.println("數據庫關閉失敗");
51
            }
52
        %>
53
    </body>
54
</html>
提示一下一定不要用錯大小寫,紅色標記就是因為我前面的字母大寫,花費了很長時間
更新數據
 
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.sql.*" %>
03
<!DOCTYPE html>
04
<html>
05
    <head>
06
        <title>獲得第二條記錄開始的三條記錄</title>
07
    </head>
08
    <body>
09
        <%
10
            String url = "jdbc:mysql://localhost:3306/javaweb";//連接數據庫的url地址
11
            String user = "root";//登錄數據庫的用戶名
12
            String password = "zhangda890126;;";//登錄數據庫的用戶名的密碼
13
            Connection conn = null;//鏈接對象
14
            PreparedStatement pstmt = null;//語句對象
15
            //ResultSet rs = null;//結果集對象
16
            try{
17
                Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序
18
                conn = DriverManager.getConnection(url,user,password);//鏈接數據庫
19
            }catch(ClassNotFoundException e){
20
                out.println("找不到驅動類");//拋出異常時,提示信息
21
            }catch(SQLException e){
22
                out.println("鏈接MySQL數據庫失敗");//處理SQLException異常
23
            }
24
            try{
25
                String updateuser = "UPDATE user SET password = ? WHERE userid = ?";//添加一條用戶信息
26
                pstmt = conn.prepareStatement(updateuser);//創建預處理語句對象PreparedStatement
27
                
28
                //設置參數
29
                pstmt.setString(1,"hello world");
30
                pstmt.setInt(2,1);
31
                
32
                //執行語句
33
                pstmt.executeUpdate();
34
                
35
            }catch(SQLException e){
36
                out.println("添加用戶信息失敗");
37
            }
38
            
39
            try{
40
                if(pstmt != null){
41
                    pstmt.close();
42
                    conn = null;
43
                }
44
                if(conn != null){
45
                    conn.close();
46
                    conn = null;
47
                }
48
            }catch(Exception e){
49
            
50
                out.println("數據庫關閉失敗");
51
            }
52
        %>
53
    </body>
54
</html>
刪除數據
 
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.sql.*" %>
03
<!DOCTYPE html>
04
<html>
05
    <head>
06
        <title>獲得第二條記錄開始的三條記錄</title>
07
    </head>
08
    <body>
09
        <%
10
            String url = "jdbc:mysql://localhost:3306/javaweb";//連接數據庫的url地址
11
            String user = "root";//登錄數據庫的用戶名
12
            String password = "zhangda890126;;";//登錄數據庫的用戶名的密碼
13
            Connection conn = null;//鏈接對象
14
            PreparedStatement pstmt = null;//語句對象
15
            //ResultSet rs = null;//結果集對象
16
            try{
17
                Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序
18
                conn = DriverManager.getConnection(url,user,password);//鏈接數據庫
19
            }catch(ClassNotFoundException e){
20
                out.println("找不到驅動類");//拋出異常時,提示信息
21
            }catch(SQLException e){
22
                out.println("鏈接MySQL數據庫失敗");//處理SQLException異常
23
            }
24
            try{
25
                String deleteuser = "DELETE FROM user WHERE userid = ?";//添加一條用戶信息
26
                pstmt = conn.prepareStatement(deleteuser);//創建預處理語句對象PreparedStatement
27
                
28
                //設置參數
29
                pstmt.setInt(1,2);
30
                
31
                //執行語句
32
                pstmt.executeUpdate();
33
                
34
            }catch(SQLException e){
35
                out.println("添加用戶信息失敗");
36
            }
37
            
38
            try{
39
                if(pstmt != null){
40
                    pstmt.close();
41
                    conn = null;
42
                }
43
                if(conn != null){
44
                    conn.close();
45
                    conn = null;
46
                }
47
            }catch(Exception e){
48
            
49
                out.println("數據庫關閉失敗");
50
            }
51
        %>
52
    </body>
53
</html>
查詢數據
 
01
<%@page language="java" contentType="text/html;charset=gb2312"%>
02
<%@page import="java.sql.*" %>
03
<!DOCTYPE html>
04
<html>
05
    <head>
06
        <title>獲得第二條記錄開始的三條記錄</title>
07
    </head>
08
    <body>
09
        <%
10
            String url = "jdbc:mysql://localhost:3306/javaweb";//連接數據庫的url地址
11
            String user = "root";//登錄數據庫的用戶名
12
            String password = "zhangda890126;;";//登錄數據庫的用戶名的密碼
13
            Connection conn = null;//鏈接對象
14
            PreparedStatement pstmt = null;//語句對象
15
            ResultSet rs = null;//結果集對象
16
            try{
17
                Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序
18
                conn = DriverManager.getConnection(url,user,password);//鏈接數據庫
19
            }catch(ClassNotFoundException e){
20
                out.println("找不到驅動類");//拋出異常時,提示信息
21
            }catch(SQLException e){
22
                out.println("鏈接MySQL數據庫失敗");//處理SQLException異常
23
            }
24
            try{
25
                String queryAll = "SELECT * FROM user LIMIT ?,?;";//添加一條用戶信息
26
                pstmt = conn.prepareStatement(queryAll);//創建預處理語句對象PreparedStatement
27
                
28
                //設置參數
29
                pstmt.setInt(1,2);
30
                pstmt.setInt(2,5);
31
                
32
                //執行語句
33
                rs = pstmt.executeQuery();
34
                
35
                while(rs.next()){
36
                    int userid = rs.getInt(1);
37
                    String username = rs.getString(2);
38
                    String userpassword = rs.getString(3);
39
                    out.println("用戶的ID:"+userid+"用戶名:"+username+"用戶的密碼:"+userpassword+"<br />");
40
                }
41
                
42
            }catch(SQLException e){
43
                out.println("添加用戶信息失敗");
44
            }
45
            
46
            try{
47
                if(pstmt != null){
48
                    pstmt.close();
49
                    conn = null;
50
                }
51
                if(conn != null){
52
                    conn.close();
53
                    conn = null;
54
                }
55
            }catch(Exception e){
56
            
57
                out.println("數據庫關閉失敗");
58
            }
59
        %>
60
    </body>
61
</html>


摘自 張大鵬的博客

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved