目录
登录
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();
}
}
}
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;
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;
}
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;
}
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;
}
public int getTableCount(String tableName, int ntid) {
int count = 0;
String sql = "select count(*) from " + tableName;
if (ntid != 0) {
sql += " where ntid = " + ntid;
}
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;
}
public static void main(String[] args) {
int count = new BaseDao().getTableCount("tb_news", 1);
System.out.println(count);
}
}
2、entity类写入用户实体:
package com.zking.entity;
public class Users {
private int userid;
private String username;
private String password;
private String realname;
private String usex;
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(String username, String password, String realname, String usex) {
super();
this.username = username;
this.password = password;
this.realname = realname;
this.usex = usex;
}
public Users(int userid, String username, String password, String realname, String usex) {
super();
this.userid = userid;
this.username = username;
this.password = password;
this.realname = realname;
this.usex = usex;
}
public Users(String username, String password) {
super();
this.username = username;
this.password = password;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
@Override
public String toString() {
return "Users [userid=" + userid + ", username=" + username + ", password=" + password + ", realname="
+ realname + ", usex=" + usex + "]";
}
}
3、Dao类编写实现用户登录的方法
package com.zking.dao;
import com.zking.entity.Users;
public interface IUsersDao {
Users login(Users u);
int userID();
}
package com.zking.dao;
import com.zking.entity.Users;
public interface IUsersDao {
Users login(Users u);
int userID();
}
创建接口实现dao方法
package com.zking.dao.impl;
import java.sql.ResultSet;
import com.zking.dao.IUsersDao;
import com.zking.entity.Users;
import com.zking.utils.BaseDao;
import com.zking.utils.DBHelper;
public class UsersDaoImpl extends BaseDao implements IUsersDao {
public Users login(Users u) {
String sql = "select * from tb_03_user where username=? and password=?";
ResultSet rs = executeQuery(sql, new Object[] { u.getUsername(), u.getPassword() });
try {
if (rs.next()) {
return new Users(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public int addUser(Users user) {
String sql = "insert into tb_03_user (userid,username,password,realname,usex)values(?,?,?,?,?)";
return this.executeUpdate(sql, new Object[] { user.getUserid(), user.getUsername(), user.getPassword(),
user.getRealname(), user.getUsex() });
}
@Override
public int userID() {
String sql = "select nvl(max(userid),0) from tb_03_user";
rs = executeQuery(sql);
int n = 0;
try {
if (rs.next()) {
n = rs.getInt(1) + 1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(conn, ps, rs);
}
return n;
}
public static void main(String[] args) {
System.out.println(new UsersDaoImpl().login(new Users("username", "password")));
}
}
4、用户接口biz层:IUserBiz . java
package com.zking.biz;
import com.zking.dao.impl.UsersDaoImpl;
import com.zking.entity.Users;
public interface IUsersBiz {
Users login(Users u);
int userID();
}
调用接口的方法:UserBizImpl . java
package com.zking.biz.impl;
import com.zking.biz.IUsersBiz;
import com.zking.dao.IUsersDao;
import com.zking.dao.impl.UsersDaoImpl;
import com.zking.entity.Users;
public class UserBizImpl implements IUsersBiz {
private IUsersDao iud = new UsersDaoImpl();
public Users login(Users u) {
return iud.login(u);
}
@Override
public int userID() {
// TODO Auto-generated method stub
return iud.userID();
}
}
5、在index.jsp界面中编写登录的方法
===========================================================================
欢迎${users.username }回来!
运行结果:
欢迎${users.username }回来!
在登录时会弹出一个登录成功的弹窗(登录时的信息错误时则显示登录失败)
登录后在当前页面显示



