登录
1、utils类中连接数据库,创建BaseDao
2、entity类写入用户实体:
3、Dao类编写实现用户登录的方法
4、用户接口biz层:IUserBiz . java
5、在index.jsp界面中编写登录的方法
实现用户登录需运用到三层构架:utils类、entity类、dao类、biz层
登录
1、utils类中连接数据库,创建BaseDao
package com.zking.utils;
/*
* 数据库帮助类
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBHelper {
// 加载驱动
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;
}
// 关闭数据库
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;
}
/**
* 方法功能:获取任意表格的总记录数
*
* @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>个人中心</button>
</div>
</c:if>