DBHelper类:
package com.zking.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBHelper {
private static String user = "scott";
private static String upwd = "tiger";
private static String cname = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//注册驱动类
static {
try {
Class.forName(cname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//连接数据库
/**
* 连接数据库
* @return
*/
public static Connection getCon() {
Connection con = null;
try {
con = DriverManager.getConnection(url, user, upwd);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭连接
* @param con
* @param ps
* @param rs
*/
public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下一个获取下一个编号的方法
* @return 下一个编号
*/
public static int getNextId(String tableName,String col) {
int id = 1;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select max("+col+") from "+tableName);
rs = ps.executeQuery();
if(rs.next()) {
id = rs.getInt(1)+1;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return id;
}
//关闭连接
}
dao类:
NesDao:新闻的增删改查方法
package com.zking.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.entity.News;
import com.zking.util.DBHelper;
public class NewsDao {
//添加新闻
/**
* 添加新闻
* @param news 要添加的新闻对象
* @return 成功返回1,失败返回0
*/
public int addNews(News news) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("insert into news(nid,tid,ntitle,nzz,ncontent,nzy,ndate,nlook) values(?,?,?,?,?,?,sysdate,0)");
ps.setInt(1, DBHelper.getNextId("news", "nid"));
ps.setInt(2, news.getTid());
ps.setString(3, news.getNtitle());
ps.setString(4, news.getNzz());
ps.setString(5, news.getNnr());
ps.setString(6, news.getNzy());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
//删除新闻
/**
* 删除新闻
* @param nid 要删除的新闻编号
* @return 成功返回1,失败返回0
*/
public int delete(int nid) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("delete news where nid="+nid);
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
//修改新闻
/**
* 修改新闻
* @param nid 要修改的 新闻编号
* @param news 修改后的新闻
* @return 成功返回1,失败返回0
*/
public int upNews(int nid,News news) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("update news set tid=?,ntitle=?,nzz=?,ncontent=?,nzy=? where nid="+nid);
ps.setInt(1, news.getTid());
ps.setString(2, news.getNtitle());
ps.setString(3, news.getNzz());
ps.setString(4, news.getNnr());
ps.setString(5, news.getNzy());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
//分页查询:
/**
* 分页查询
* @param pageIndex 页码
* @param pageSize 每页数据条数
* @return 范湖查询到的集合
*/
public ArrayList<News> pageNews(int pageIndex,int pageSize){
ArrayList<News> nlist = new ArrayList<>();
int start = (pageIndex-1)*pageSize+1;
int end = pageIndex*pageSize;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
String sql = "select * from(select a.*,rownum mid from news a)b where mid>=? and mid<=?";
ps = con.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while(rs.next()) {
int nid = rs.getInt(1);
int tid = rs.getInt(2);
String nzz = rs.getString(4);
String ntitle = rs.getString(3);
String nnr = rs.getString(5);
Date ndate = rs.getDate(6);
int nlook = rs.getInt(7);
String nzy = rs.getString(8);
String nimage = rs.getString(9);
//实例化对象
News news = new News(nid, tid, ntitle, nzz, nnr, nzy, ndate, nlook, nimage);
//把新闻对象放到集合中
nlist.add(news);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return nlist;
}
//最大页码方法
/**
* 求出最大也,啊
* @param pageSize 每页数据条数
* @return 返回最大页码
*/
public int getMaxPage(int pageSize) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int maxPage = 0;
int count = 0;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select count(*) from news");
rs = ps.executeQuery();
if(rs.next()) {
count = rs.getInt(1);
maxPage = count/pageSize;
if(count%pageSize!=0) {
maxPage++;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return maxPage;
}
//模糊查询
//分页模糊查询
//。。。。
}
userDao类:用户登陆与用户注册
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zking.entity.User;
import com.zking.util.DBHelper;
public class UserDao {
//用户登录-根据帐号 密码 进行查询
/**
* 用户登录
* @param uname 用户名
* @param upwd 用户密码
* @return 登录成功返回用户对象,失败返回null
*/
public User login(String uname,String upwd) {
User user = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs= null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from T277 where uname=? and upwd=?");
ps.setString(1, uname);
ps.setString(2, upwd);
rs = ps.executeQuery();
//操作数据
if(rs.next()) {
int uuid = rs.getInt(1);
String uinfo = rs.getString(4);
user = new User(uuid, uname, upwd, uinfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return user;
}
/**
* 用户注册
* @param user 注册的用户对象
* @return
*/
public int register(User user) {
int i = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("insert into T277 values(?,?,?,?)");
ps.setInt(1, DBHelper.getNextId("T277","uuid"));
ps.setString(2, user.getUname());
ps.setString(3, user.getUpwd());
ps.setString(4, user.getUinfo());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, null);
}
return i;
}
}