📄 supplydao.java
字号:
package com.wl.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.wl.dbconnection.DBManager;
import com.wl.entity.Supply;
/**
* 供应商DAO
* @author wangJianwei
*
*/
public class SupplyDAO {
DBManager db = new DBManager();
boolean flag;
ResultSet rs = null;
/**
* 添加供应商
* @param s
* @return boolean
*/
public boolean insertSuppley(Supply s) {
flag = db.openDB();
if(flag) {
try {
db.ps = db.conn.prepareStatement("insert into supplytable values(?,?,?,?,?,?,?,?,?)");
db.ps.setString(1, s.getSupplyId());
db.ps.setString(2, s.getSupplyName());
db.ps.setString(3, s.getSupplyCharge());
db.ps.setString(4, s.getSupplyAddress());
db.ps.setString(5, s.getSupplyPhone());
db.ps.setString(6, s.getSupplyMobile());
db.ps.setString(7, s.getSupplyFax());
db.ps.setString(8, s.getSupplyEmail());
db.ps.setString(9, s.getSupplyHttp());
db.ps.executeUpdate();
} catch (SQLException e) {
return false;
}
finally {
db.closeDB();
}
return true;
}
else {
return false;
}
}
/**
* 查询所有供应商
* @return list
*/
public List<Supply> querySupply(){
List<Supply> list = new ArrayList<Supply>();
flag = db.openDB();
Supply s = null;
if(flag){
try {
db.ps = db.conn.prepareStatement("select * from supplytable");
rs = db.ps.executeQuery();
while(rs.next()) {
s = new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),
rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10));
list.add(s);
}
} catch (Exception e) {
System.out.print("查询错误!");
}
finally {
db.closeDB();
}
}
else {
System.out.println("打开数据库失败");
}
return list;
}
/**
* 根据供应商的iD删除一条记录
* @return boolean
*/
public boolean deleteSupplyId(int SupplyId) {
flag = db.openDB();
if(flag) {
try {
db.ps = db.conn.prepareStatement("delete from supplytable where Id=?");
db.ps.setInt(1, SupplyId);
db.ps.executeUpdate();
} catch (SQLException e) {
System.out.println("123");
}
return true;
}
else {
return false;
}
}
/**
* 根据id查询一行记录
* @return
*/
public Supply querySupplyId(int id) {
flag = db.openDB();
Supply s = null;
if(flag) {
String sql = "select * from supplytable where id=?";
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(1, id);
rs = db.ps.executeQuery();
while(rs.next())
{
s = new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
db.closeDB();
}
}
return s;
}
/**
* 修改方法
* @param id
* @return
*/
public int queryUpdateSupply(Supply s){
flag = db.openDB();
int rows = 0;
if(flag){
String sql ="update supplytable set supplyId=?,supplyName=?,supplyCharge=?,supplyAddress=?,supplyPhone=?,supplyMobile=?,supplyFax=?,supplyEmail=?,supplyHttp=? where Id=?";
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setInt(10, s.getId());
db.ps.setString(1, s.getSupplyId());
db.ps.setString(2, s.getSupplyName());
db.ps.setString(3, s.getSupplyCharge());
db.ps.setString(4, s.getSupplyAddress());
db.ps.setString(5, s.getSupplyPhone());
db.ps.setString(6, s.getSupplyMobile());
db.ps.setString(7, s.getSupplyFax());
db.ps.setString(8, s.getSupplyEmail());
db.ps.setString(9, s.getSupplyHttp());
rows = db.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
db.closeDB();
}
}return rows;
}
public List<Supply> selectSupplyId(String id){
List<Supply> list = new ArrayList<Supply>();
flag = db.openDB();
if(flag){
String sql = "select * from supplytable where SupplyId=?";
try {
db.ps = db.conn.prepareStatement(sql);
db.ps.setString(1, id);
rs = db.ps.executeQuery();
while(rs.next()){
Supply supply = new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10));
list.add(supply);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
db.closeDB();
}
}
return list;
}
/**
* 封装按姓名查询供应商信息
* @param supplyName
* @author joypen
* @return
*/
public Supply selectSupplyByName(String supplyName){
flag=db.openDB();
Supply supply=null;
if(flag){
try{
db.ps=db.conn.prepareStatement("select * from supplytable where SupplyName=?");
db.ps.setString(1, supplyName);
rs=db.ps.executeQuery();
while(rs.next()){
supply=new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),
rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),
rs.getString(8),rs.getString(9),rs.getString(10));
}
}
catch(Exception e){
System.out.print("查询出错了!");
}
finally{
db.closeDB();
}
}
else{
System.out.print("数据库打开失败");
}
return supply;
}
/**
* 封装按姓名查询供应商信息
* @param supplyName
* @desc 根据名字模糊查询
* @return
*/
public List<Supply> selectSupplyByNameList(String supplyName){
flag=db.openDB();
List<Supply> list = new ArrayList<Supply>();
if(flag){
try{
db.ps=db.conn.prepareStatement("select * from supplytable where SupplyName like ?");
db.ps.setString(1, "%"+supplyName+"%");
rs=db.ps.executeQuery();
while(rs.next()){
Supply supply=new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),
rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),
rs.getString(8),rs.getString(9),rs.getString(10));
list.add(supply);
}
}
catch(Exception e){
System.out.print("查询出错了!");
}
finally{
db.closeDB();
}
}
else{
System.out.print("数据库打开失败");
}
return list;
}
// public static void main(String [] args){
// SupplyDAO sdao = new SupplyDAO();
// System.out.print(sdao.selectSupplyByName("公司"));
// }
/**
* 分页查询供应商信息
*/
public List<Supply> selectSupplyPage(int pageSize, int pageNum){
flag=db.openDB();
List<Supply> list = new ArrayList<Supply>();
if(flag){
try{
db.ps = db.conn.prepareStatement("select top " + pageSize
+ "* from supplytable where Id not in(select top "
+ pageSize * pageNum
+ " Id from supplytable order by Id)order by Id");
rs=db.ps.executeQuery();
while(rs.next()){
Supply supply=new Supply(rs.getInt(1),rs.getString(2),rs.getString(3),
rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),
rs.getString(8),rs.getString(9),rs.getString(10));
list.add(supply);
}
}
catch(Exception e){
System.out.print("查询出错了!");
}
}
else{
System.out.print("数据库打开失败");
}
return list;
}
//通过页面大小获取总页数
public int getPageNum(int pageSize) {
int num = 0;
flag=db.openDB();
if(flag){
try {
db.ps=db.conn.prepareStatement("select count(*) from supplytable");
rs = db.ps.executeQuery();
while(rs.next()){
num = rs.getInt(1);
}
if (num % pageSize == 0) {
return num / pageSize;
} else {
return num / pageSize + 1;
}
} catch (SQLException e) {
System.out.println("数据库连接失败!");
e.printStackTrace();
}finally{
db.closeDB();
}
}
return num;
}
public static void main(String[] args)
{
SupplyDAO dao = new SupplyDAO();
System.out.println(dao.selectSupplyPage(5,1));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -