二.编写代码
1.首先在swsm.news.entity下建立用户实体类:NewsUser
代码如下:
package swsm.news.entity;
/*
*
* 用户实体类
*/
public class NewsUser {
private int uid; //用户id
private String uname;//用户名
private String upwd;//用户密码
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
2.在swsm.news.dao 封装数据库操作类:ConnManager
代码如下:
package swsm.news.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
* 数据库操作类,封装了获取数据库连接的方法
* @author Administrator
*
*/
public class ConnManager {
private Connection connection = null;
/*
* 获取数据库连接(传统jdbc)
*/
public Connection getConnection_jdbc(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lynews", "root", "111");
} catch (Exception e) {
e.printStackTrace();
}
return connection; //返回数据库连接
}
/*
* 获取数据库连接(JNDI)
*/
public Connection getConnection_jndi(){
try {
//通过数据源获取数据库连接
//通过JNDI接口查询名称叫做 tjndi 的服务
//建立上下文
Context cxt = new InitialContext();
//调用lookup方法查找配置的服务(数据源)
DataSource source = (DataSource)cxt.lookup("java:comp/env/jdbc/lynews");
//通过数据源获取数据库连接
connection = source.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
//返回数据库连接
return connection;
}
/*
* 关闭数据库连接
*/
public void closeConnection(){
try {
if(connection!=null){
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.在swsm.news.dao 编写用户数据访问层:NewsUserDao
代码如下:
package swsm.news.dao;
import java.sql.*;
import swsm.news.entity.NewsUser;
import swsm.news.iface.INewsUserDao;
/**
* 用户数据访问层
*
* @author Administrator
*
*/
public class NewsUserDao implements INewsUserDao {
public NewsUser doLogin(String uname, String upwd) {
//返回值
NewsUser user = null;//默认为NULL
Connection con = null;//数据库连接
ConnManager connManager = new ConnManager();
con = connManager.getConnection_jdbc();
Statement pstm;
try {
pstm = con.createStatement();
String sql = "select * from news_users where uname = '"+uname+"' and upwd = '"+upwd+"'";
ResultSet rs = pstm.executeQuery(sql);
if(rs.next()){
//登录成功
//获取用户
int uid = rs.getInt("UID");
String uname_tmp=rs.getString("UNAME");
String upwd_tmp=rs.getString("UPWD");
//讲数据封装至用户对象
user = new NewsUser();
user.setUid(uid);
user.setUname(uname_tmp);
user.setUpwd(upwd_tmp);
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;//返回用户对象
}
}
4.在swsm.news.iface编写用户数据访问层接口:INewsUserDao
代码如下:
package swsm.news.iface;
import swsm.news.entity.NewsUser;
/**
* 用户数据访问层接口
* @author Administrator
*
*/
public interface INewsUserDao {
/**
* 登录功能
* 参数:uname,upwd
* 返回值:用户对象
*/
public NewsUser doLogin(String uname,String upwd);
}
5.在swsm.news.biz下创建用户业务层:NewsUserBiz
代码如下:
package swsm.news.biz;
import swsm.news.dao.NewsUserDao;
import swsm.news.entity.NewsUser;
import swsm.news.iface.INewsUserBiz;
import swsm.news.iface.INewsUserDao;
/**
* 用户业务层
* @author Administrator
*
*/
public class NewsUserBiz implements INewsUserBiz {
//数据访问层对象
private INewsUserDao udao = new NewsUserDao();
public NewsUser doLogin(String uname, String upwd) {
// TODO Auto-generated method stub
return udao.doLogin(uname, upwd);
}
}
6.在swsm.news.iface下编写用户逻辑层接口:INewsUserBiz
代码如下:
package swsm.news.iface;
import swsm.news.entity.NewsUser;
/**
* 用户逻辑层接口
* @author Administrator
*
*/
public interface INewsUserBiz {
/**
* 登录功能
* 参数:uname,upwd
* 返回值:用户对象
*/
public NewsUser doLogin(String uname,String upwd);
}