您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關JSP如何配置數據庫,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
JSP數據庫配置步驟三
在項目下新建包beans,在此包下編寫一個JavaBean程序,命名為Test_2_4.java,代碼為:
package import java.io.UnsupportedEncodingException; import java.sql.*; import java.util.ResourceBundle; public class Test_2_4 { private String username; private String password; private Connection conn = null; private PreparedStatement ps = null; private ResultSet rs = null; public String getUsername() { return username; } public void setUsername(String username) throws UnsupportedEncodingException { String temp = new String(username.getBytes("iso8859-1"), "utf-8"); this.username = temp; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private void closeConn() { /** * 關閉數據連接的方法 * */ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } ps = null; try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } public int query() { int tag = 0; if (username == null || password == null) { return 0; } ResourceBundle rb = ResourceBundle.getBundle("init"); String dbDirver = rb.getString("connJDBC.dbDriver"); String dbUrl = rb.getString("connJDBC.dbURL"); String dbUsername = rb.getString("connJDBC.dbUsername"); String dbPwd = rb.getString("connJDBC.dbPassword"); try { Class.forName(dbDirver); conn = DriverManager.getConnection(dbUrl, dbUsername, dbPwd); String sql = "select * from users where username=? and password=?"; ps = conn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, password); rs = ps.executeQuery(); if (rs.next()) { return 1; } else { return -1; } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } /** * 調用關閉數據連接的方法,關閉數據庫連接 * */ closeConn(); return tag; } }
JSP數據庫配置步驟四
新建jsp文件,命名為test_2_4.jsp,代碼如下:
< %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < jsp:useBean id="login" class="beans.Test_2_4" scope="session" /> < jsp:setProperty name="login" property="*" /> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html> < head> < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> < title>實驗二利用JavaBean實現用戶登錄< /title> < /head> < body> < form action="test_2_3.jsp" method="post"> < div align="center">用戶名< input type="text" name="username" size="16">< /div> < div align="center">密 碼< input type="password" name="password" size="16">< /div> < div align="center">< input type="submit" value="登錄"> < input type="reset" value="重置">< /div> < /form> < % request.setCharacterEncoding("utf-8"); int isLogin = login.query(); if (isLogin == 1) { String username = request.getParameter("username"); session.putValue("username", username); response.sendRedirect("welcome.jsp"); } else if (isLogin == -1) { out.println("< script language=javascript>alert('登錄失敗!您沒有權限訪問!');< /script"); } %> < /body> < /html>
JSP數據庫配置步驟五
創建以歡迎登錄成功的頁面welcome.jsp,代碼如下:
< %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html> < head> < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> < title>登錄成功< /title> < /head> < body> < % request.setCharacterEncoding("utf-8"); if (session.getValue("username") == "" || session.getValue("username") == null) { response.sendRedirect("test_2_4.jsp"); } else { String username = session.getValue("username").toString(); String user = new String(username.getBytes("iso8859-1"), "utf-8"); %> < %=user%>,歡迎您訪問! < % } %> < /body> < /html>
JSP數據庫配置步驟六
測試效果,如下:
①未進行登錄操作:
②登錄成功
③登錄失敗
關于“JSP如何配置數據庫”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。