您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)JSP如何配置數(shù)據(jù)庫,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
JSP數(shù)據(jù)庫配置步驟三
在項(xiàng)目下新建包beans,在此包下編寫一個(gè)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() { /** * 關(guān)閉數(shù)據(jù)連接的方法 * */ 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(); } /** * 調(diào)用關(guān)閉數(shù)據(jù)連接的方法,關(guān)閉數(shù)據(jù)庫連接 * */ closeConn(); return tag; } }
JSP數(shù)據(jù)庫配置步驟四
新建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>實(shí)驗(yàn)二利用JavaBean實(shí)現(xiàn)用戶登錄< /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('登錄失??!您沒有權(quán)限訪問!');< /script"); } %> < /body> < /html>
JSP數(shù)據(jù)庫配置步驟五
創(chuàng)建以歡迎登錄成功的頁面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數(shù)據(jù)庫配置步驟六
測試效果,如下:
①未進(jìn)行登錄操作:
②登錄成功
③登錄失敗
關(guān)于“JSP如何配置數(shù)據(jù)庫”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。