這篇文章將為大家詳細(xì)講解有關(guān)JSP如何配置數(shù)據(jù)庫,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
10多年的子長網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整子長建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“子長網(wǎng)站設(shè)計(jì)”,“子長網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
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é)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。