package custom.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import custom.struts.form.SellerForm;
import custom.struts.form.CustomForm;
import custom.dao.ConnectionManager;
public class SellerDao {
/*针对seller表*/
//1,查询
public static List findAll(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
conn = ConnectionManager.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_power='2'");
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_psw(rs.getString("seller_psw"));
sf.setSeller_power(rs.getString("seller_power"));
sf.setSeller_id(rs.getString("seller_id"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.closeConnection(conn);
}
return list;
}
public static List findId(String id){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_id='"+id+"'");
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_id(rs.getString("seller_id"));
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_power(rs.getString("seller_power"));
sf.setSeller_psw(rs.getString("seller_psw"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
//2,删除
public static int delete(String id){
Connection conn = null;
Statement stmt = null;
int flag = 0;
try{
conn = ConnectionManager.getConnection();
stmt = conn.createStatement();
flag = stmt.executeUpdate("delete seller where seller_id='"+id+"'");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return flag;
}
//3,修改
public static int update(String id,String name,String psw){
Connection conn = null;
Statement stmt = null;
int n = 0;
try{
stmt = conn.createStatement();
n = stmt.executeUpdate("update seller set seller_name='"+name+"',seller_psw='"+psw+"' where seller_id="+id);
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return n;
}
//4,插入
public static int insert(String name,String psw){
Connection conn = null;
Statement stmt = null;
int i = 0;
try{
stmt = conn.createStatement();
i = stmt.executeUpdate("insert into seller (select max(seller_id)+1 ,"+name+","+psw+",'2' from seller)");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return i;
}
//处理登录这个模块
public static String login(String name,String psw,Connection conn){
Statement stmt = null;
ResultSet rs = null;
String power = "";
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_name='"+name+"' and seller_psw='"+psw+"'");
//如果查询出姓名密码来了就获取权限值
if(rs.next()){
power = rs.getString("seller_power");
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return power;
}
/*针对custom表*/
//1,查询
public static List findAllCustom(Connection conn){
Statement stmt = null;
ResultSet rs= null;
String sql1 = "";
String sql2 = "";
List list = new ArrayList();
try{
stmt=conn.createStatement();
sql1 = "select seller_name,customer_name,customer_sex,customer_email,customer_phone ";
sql2 = "from customer,seller where customer.seller_id=seller.seller_id and customer_state='1'";
rs = stmt.executeQuery(sql1+sql2);
while(rs.next()){
CustomForm c = new CustomForm();
c.setSeller_name(rs.getString("seller_name"));
c.setCustomer_name(rs.getString("customer_name"));
c.setCustomer_sex(rs.getString("customer_sex"));
c.setCustomer_email(rs.getString("customer_email"));
c.setCustomer_phone(rs.getString("customer_phone"));
list.add(c);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static List dispaterSeller(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
String sql = "select * from seller where seller_power='2'";
rs = stmt.executeQuery(sql);
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_id(rs.getString("seller_id"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static List dispaterCustom(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
String sql = "select * from customer where customer_state='0'";
rs = stmt.executeQuery(sql);
while(rs.next()){
CustomForm cf = new CustomForm();
cf.setCustomer_name(rs.getString("customer_name"));
cf.setCustomer_id(rs.getString("customer_id"));
list.add(cf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static void dispater(String sid,String cid,Connection conn,int count,int i){
Statement stmt = null;
try{
stmt = conn.createStatement();
stmt.executeUpdate("update customer set seller_id='"+sid+"',customer_state='1' where customer_id ="+cid);
}catch(Exception e){
e.printStackTrace();
}finally{
if(count==i){
ConnectionManager.getConnection();
}
}
}
}
jdbc连接数据库
最新推荐文章于 2024-11-05 14:22:47 发布