自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JSP數(shù)據(jù)庫(kù)配置:程序和頁(yè)面的設(shè)置及測(cè)試效果

開(kāi)發(fā) 后端
之前介紹了JSP數(shù)據(jù)庫(kù)配置中MySQL表的建立和Eclipse項(xiàng)目的建立,下面繼續(xù)介紹JavaBean程序以及JSP頁(yè)面的設(shè)置,以及測(cè)試結(jié)果。

以下繼續(xù)介紹JSP數(shù)據(jù)庫(kù)配置的步驟。

JSP數(shù)據(jù)庫(kù)配置步驟三

在項(xiàng)目下新建包beans,在此包下編寫一個(gè)JavaBean程序,命名為Test_2_4.java,代碼為:

  1. package 
  2. import java.io.UnsupportedEncodingException;  
  3. import java.sql.*;  
  4. import java.util.ResourceBundle;  
  5. public class Test_2_4 {  
  6.     private String username;  
  7.     private String password;  
  8.     private Connection conn = null;  
  9.     private PreparedStatement ps = null;  
  10.     private ResultSet rs = null;  
  11.     public String getUsername() {  
  12.         return username;  
  13.     }  
  14.     public void setUsername(String username)  
  15.             throws UnsupportedEncodingException {  
  16.         String temp = new String(username.getBytes("iso8859-1"), "utf-8");  
  17.         this.username = temp;  
  18.     }  
  19.     public String getPassword() {  
  20.         return password;  
  21.     }  
  22.     public void setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25.     private void closeConn() {  
  26.         /**  
  27.         * 關(guān)閉數(shù)據(jù)連接的方法  
  28.         * */ 
  29.         try {  
  30.             ps.close();  
  31.         } catch (SQLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         ps = null;  
  35.         try {  
  36.             rs.close();  
  37.         } catch (SQLException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.         rs = null;  
  41.         if (conn != null)  
  42.             try {  
  43.                 conn.close();  
  44.             } catch (SQLException e) {  
  45.                 e.printStackTrace();  
  46.             }  
  47.         conn = null;  
  48.     }  
  49.        public int query() {  
  50.         int tag = 0;  
  51.         if (username == null || password == null) {  
  52.             return 0;  
  53.         }  
  54.         ResourceBundle rb = ResourceBundle.getBundle("init");  
  55.         String dbDirver = rb.getString("connJDBC.dbDriver");  
  56.         String dbUrl = rb.getString("connJDBC.dbURL");  
  57.         String dbUsername = rb.getString("connJDBC.dbUsername");  
  58.         String dbPwd = rb.getString("connJDBC.dbPassword");  
  59.         try {  
  60.             Class.forName(dbDirver);  
  61.             conn = DriverManager.getConnection(dbUrl, dbUsername, dbPwd);  
  62.             String sql = "select * from users where username=? and password=?";  
  63.             ps = conn.prepareStatement(sql);  
  64.             ps.setString(1, username);  
  65.             ps.setString(2, password);  
  66.             rs = ps.executeQuery();  
  67.             if (rs.next()) {  
  68.                 return 1;  
  69.             } else {  
  70.                 return -1;  
  71.             }  
  72.         } catch (SQLException e) {  
  73.             e.printStackTrace();  
  74.         } catch (ClassNotFoundException e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.         /**  
  78.         * 調(diào)用關(guān)閉數(shù)據(jù)連接的方法,關(guān)閉數(shù)據(jù)庫(kù)連接  
  79.         * */ 
  80.         closeConn();  
  81.         return tag;  
  82.     }  

JSP數(shù)據(jù)庫(kù)配置步驟四

新建jsp文件,命名為test_2_4.jsp,代碼如下:

  1. < %@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. < jsp:useBean id="login" class="beans.Test_2_4" scope="session" /> 
  4. < jsp:setProperty name="login" property="*" /> 
  5. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  6. < html> 
  7. < head> 
  8. < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  9. < title>實(shí)驗(yàn)二利用JavaBean實(shí)現(xiàn)用戶登錄< /title> 
  10. < /head> 
  11. < body> 
  12. < form action="test_2_3.jsp" method="post"> 
  13. < div align="center">用戶名< input type="text" name="username" 
  14.     size="16">< /div> 
  15. < div align="center">密&nbsp;&nbsp;&nbsp;&nbsp;碼< input 
  16.     type="password" name="password" size="16">< /div> 
  17. < div align="center">< input type="submit" value="登錄">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;< input 
  18.     type="reset" value="重置">< /div> 
  19. < /form> 
  20. < %  
  21.     request.setCharacterEncoding("utf-8");  
  22.     int isLogin = login.query();  
  23.     if (isLogin == 1) {  
  24.         String username = request.getParameter("username");  
  25.         session.putValue("username", username);  
  26.         response.sendRedirect("welcome.jsp");  
  27.     } else if (isLogin == -1) {  
  28.         out.println("< script language=javascript>alert('登錄失敗!您沒(méi)有權(quán)限訪問(wèn)!');< /script");  
  29.     }  
  30. %> 
  31. < /body> 
  32. < /html> 

JSP數(shù)據(jù)庫(kù)配置步驟五

創(chuàng)建以歡迎登錄成功的頁(yè)面welcome.jsp,代碼如下:

  1. < %@ page language="java" contentType="text/html; charset=UTF-8" 
  2.      pageEncoding="UTF-8"%> 
  3. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. < html> 
  5. < head> 
  6. < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. < title>登錄成功< /title> 
  8. < /head> 
  9. < body> 
  10. < %  
  11.      request.setCharacterEncoding("utf-8");  
  12.      if (session.getValue("username") == ""  
  13.               || session.getValue("username") == null) {  
  14.          response.sendRedirect("test_2_4.jsp");  
  15.      } else {  
  16.          String username = session.getValue("username").toString();  
  17.          String user = new String(username.getBytes("iso8859-1"),  
  18.                    "utf-8");  
  19. %> 
  20. < %=user%>,歡迎您訪問(wèn)!  
  21. < %  
  22.      }  
  23. %> 
  24. < /body> 
  25. < /html> 

JSP數(shù)據(jù)庫(kù)配置步驟六

測(cè)試效果,如下:

①未進(jìn)行登錄操作:

未進(jìn)行登錄操作 

②登錄成功

登錄成功 

登錄成功 

③登錄失敗

登錄失敗 

【編輯推薦】

  1. JSP數(shù)據(jù)庫(kù)配置:創(chuàng)建MySQL表以及Eclipse項(xiàng)目
  2. 從JSP數(shù)據(jù)庫(kù)的連接看J2EE服務(wù)器和連接池的必要性
  3. 淺談如何在JSP連接MySQL數(shù)據(jù)庫(kù)
  4. 使用JDBC連接數(shù)據(jù)庫(kù)
  5. 在JSP中獲取數(shù)據(jù)庫(kù)連接
責(zé)任編輯:yangsai 來(lái)源: 百度空間
相關(guān)推薦

2009-01-18 10:56:27

JSTLEL表達(dá)式JSP標(biāo)準(zhǔn)標(biāo)記庫(kù)

2009-07-06 18:23:56

Struts和JSPJSP頁(yè)面

2009-07-03 18:12:49

JSP頁(yè)面

2009-07-06 09:34:19

JSP頁(yè)面

2011-09-01 12:42:09

SQL Server創(chuàng)建加密視圖控制視圖頁(yè)面的訪問(wèn)權(quán)限

2011-05-11 16:54:49

JSP

2009-07-02 13:36:24

動(dòng)態(tài)頁(yè)面JSP技術(shù)

2011-09-01 12:53:02

SQL Server控制視圖頁(yè)面的訪問(wèn)權(quán)限

2017-10-25 15:27:52

MySQL數(shù)據(jù)庫(kù)超時(shí)設(shè)置

2009-07-07 16:39:33

JSP數(shù)據(jù)庫(kù)配置

2009-06-30 15:15:30

JSP數(shù)據(jù)庫(kù)

2019-12-24 10:12:09

數(shù)據(jù)庫(kù)工具技術(shù)

2019-09-16 16:30:56

2009-07-01 11:08:14

JSP DestoryJSP Init數(shù)據(jù)庫(kù)鏈接

2012-03-28 11:17:48

JavaSSHJSP

2009-05-11 09:46:37

JDBC數(shù)據(jù)庫(kù)驅(qū)動(dòng)JDBC驅(qū)動(dòng)

2011-03-17 14:19:23

JDBC數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序

2009-07-20 13:31:50

Ruby on Rai

2009-07-03 13:56:21

JSP編程技巧

2009-07-06 15:57:56

獲取數(shù)據(jù)庫(kù)連接JSP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)