【E家园项目】
登錄
? ? ? ? 1、utils類中連接數(shù)據(jù)庫,創(chuàng)建BaseDao
? ? ? ? 2、entity類寫入用戶實(shí)體:
? ? ? ? 3、Dao類編寫實(shí)現(xiàn)用戶登錄的方法
? ? ? ? 4、用戶接口biz層:IUserBiz . java
? ? ? ? 5、在index.jsp界面中編寫登錄的方法
實(shí)現(xiàn)用戶登錄需運(yùn)用到三層構(gòu)架:utils類、entity類、dao類、biz層
登錄
1、utils類中連接數(shù)據(jù)庫,創(chuàng)建BaseDao
package com.zking.utils;
?
/*
?* 數(shù)據(jù)庫幫助類
?*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
?
public class DBHelper {
? ? // 加載驅(qū)動
? ? static {
? ? ? ? try {
? ? ? ? ? ? Class.forName("oracle.jdbc.driver.OracleDriver");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
?
? ? // 建立連接
? ? public static Connection getConn() {
? ? ? ? Connection conn = null;
? ? ? ? try {
? ? ? ? ? ? String url = "jdbc:oracle:thin:@localhost:1521:orc";
? ? ? ? ? ? conn = DriverManager.getConnection(url, "scott", "123");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
?
? ? ? ? }
? ? ? ? return conn;
? ? }
?
? ? // 關(guān)閉數(shù)據(jù)庫
? ? public static void myClose(Connection conn, PreparedStatement ps, ResultSet rs) {
? ? ? ? try {
? ? ? ? ? ? if (conn != null && !conn.isClosed()) {
? ? ? ? ? ? ? ? conn.close();
? ? ? ? ? ? }
? ? ? ? ? ? if (ps != null) {
? ? ? ? ? ? ? ? ps.close();
? ? ? ? ? ? }
? ? ? ? ? ? if (rs != null) {
? ? ? ? ? ? ? ? rs.close();
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
?
? ? }
?
BaseDao . java
package com.zking.utils;
?
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
?
/**
?* 增刪改查通用類
?*?
?* @author ling
?*
?*/
public class BaseDao {
? ? protected ResultSet rs = null;
? ? protected Connection conn = null;
? ? protected PreparedStatement ps = null;
?
? ? /**
? ? ?* 方法功能:通用增刪改
? ? ?*?
? ? ?*/
? ? public int executeUpdate(String sql, Object... objects) {
? ? ? ? int n = 0;
? ? ? ? try {
? ? ? ? ? ? conn = DBHelper.getConn();
? ? ? ? ? ? ps = conn.prepareStatement(sql);
? ? ? ? ? ? for (int i = 0; i < objects.length; i++) {
? ? ? ? ? ? ? ? ps.setObject(i + 1, objects[i]);
? ? ? ? ? ? }
? ? ? ? ? ? n = ps.executeUpdate();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? System.out.println("DAO增刪改異常");
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? DBHelper.myClose(conn, ps, null);
? ? ? ? }
? ? ? ? return n;
? ? }
?
? ? /**
? ? ?* 方法功能:通用查詢
? ? ?*?
? ? ?*/
? ? public ResultSet executeQuery(String sql, Object... objects) {
? ? ? ? try {
? ? ? ? ? ? conn = DBHelper.getConn();
? ? ? ? ? ? ps = conn.prepareStatement(sql);
? ? ? ? ? ? for (int i = 0; i < objects.length; i++) {
? ? ? ? ? ? ? ? ps.setObject(i + 1, objects[i]);
? ? ? ? ? ? }
? ? ? ? ? ? rs = ps.executeQuery();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? System.out.println("DAO查詢異常");
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return rs;
? ? }
?
? ? /**
? ? ?* 方法功能:獲取指定表的最大ID
? ? ?*?
? ? ?*/
? ? public int getMaxID(String colID, String tableName) {
? ? ? ? String sql = "select nvl(max(" + colID + "),0) from " + tableName;
? ? ? ? ResultSet rs = null;
? ? ? ? try {
? ? ? ? ? ? rs = this.executeQuery(sql);
? ? ? ? ? ? if (rs.next()) {
? ? ? ? ? ? ? ? return rs.getInt(1) + 1;
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? // TODO: handle exception
? ? ? ? }
? ? ? ? return 0;
? ? }
?
? ? /**
? ? ?* 方法功能:獲取任意表格的總記錄數(shù)
? ? ?*?
? ? ?* @param 表名
? ? ?*/
? ? public int getTableCount(String tableName) {
? ? ? ? int count = 0;
? ? ? ? String sql = "select count(*) from " + tableName;
?
? ? ? ? ResultSet rs = this.executeQuery(sql);
? ? ? ? try {
? ? ? ? ? ? if (rs.next()) {
? ? ? ? ? ? ? ? count = rs.getInt(1);
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? DBHelper.myClose(this.conn, this.ps, rs);
? ? ? ? }
? ? ? ? return count;
? ? }
<!-- 登錄 -->
? ? ? ? ? ? ? ? ??
? <c:if test="${empty users }">
? ? ? ? ? ? ? ? ? ? ? ? <form action="dologin.jsp" method="post">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <table style="margin-left: 5px;">
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <td></td>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <td>登錄名:<input name="username" style="width: 110px;" />
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <td>密碼 :<input name="password" style="width: 110px;" /></td>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <td><input type="image" src="image/button01.gif" /> <img
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? src="image/button02.gif" οnclick="register();" /></td>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </table>
? ? ? ? ? ? ? ? ? ? ? ? </form>
? ? ? ? ? ? ? ? ? ? </c:if>
? ? ? ? ? ? ? ? ? ? <c:if test="${not empty users }">
? ? ? ? ? ? ? ? ? ? ? ? <div style="text-align: center;">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <p>歡迎${users.username }回來!</p>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- <h5></h5> -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? <button>修改密碼</button>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <button>個(gè)人中心</button>
? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? </c:if>
總結(jié)
- 上一篇: rstudio线性回归_R语言统计分析(
- 下一篇: 构建:什么是构建