Java《鲜花销售系统》案例(1、底层代码设计)

类connect

//连接数据库操作

package Connection;
import java.sql.*;
public class connect {
	private final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
	  //定义数据库连接URL
	 private final String DBURL = "jdbc:mysql://localhost:3306/flowerdb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
	 //定义数据库连接用户名
	 private final String DBUSER = "root";
	 //定义数据库连接密码
	 private final String DBPASSWORD = "ww20010208";
	 //定义数据库连接对象
	 private Connection conn = null;
	 //构造方法,加载驱动
	 public connect() {
	 try{
	 Class.forName(DBDRIVER);
	 this.conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
	 }catch(Exception e){
	 System.out.println("加载驱动失败!");
	 System.exit(0);
	 }
	 }
	 
	 //取得数据库连接
	 public Connection getConnection(){
	 return conn;
	 }
	 
	 //关闭数据库
	 public void close(){ 
	 try {
	 conn.close();
	 } catch (SQLException e) {
	 System.out.println("数据库连接关闭失败!");
	 }
	 }
}

包DAO

//采用dao层工厂模式设计

类CustomerDAO

//顾客操作接口

package DAO;

import java.util.ArrayList;
import java.util.List;

import StoreManager.Customer;

public interface CustomerDAO {
	public int flagin = 0;
	public int i = 1;
	List storemessage = new ArrayList();
	public String[][] ById = new String[1][4];
	public int insert(Customer cus) throws Exception;
	public void delete(String cusid) throws Exception;
	public String[][] queryById(String cusid) throws Exception;
	public List queryAll() throws Exception;
	public void update(Customer cus) throws Exception;
	public int update1(Customer cus) throws Exception;
	public int shopping(double balance,String id) throws Exception;
	public double getBalance(String id) throws Exception;
	public void update2(Customer cus) throws Exception;
}

类FlowerDAO

//鲜花操作接口

package DAO;

import java.util.ArrayList;
import java.util.List;

import StoreManager.Flower;

public interface FlowerDAO {
	public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById_Name = new String[1][4];
	 public void insert(Flower flo) throws Exception;
	 public void delete(int flid) throws Exception;
	 public String[][] queryById(int flid) throws Exception;
	 public String[][] queryByName(String flname) throws Exception;
	 public double getPrice(String flname) throws Exception;
	 public List queryAll() throws Exception;
	 public List queryComBox() throws Exception;
	 public void update(Flower flo) throws Exception;
	 public int getStore(String name) throws Exception;
	 public int shopping(int number,String name) throws Exception;
}

类OrdersDAO

//订单操作接口

package DAO;

import java.util.ArrayList;
import java.util.List;

import StoreManager.Orders;

public interface OrdersDAO {
	public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById = new String[1][5];
	 
	 public void insert(String s1,String s2,String s3,double s4) throws Exception;
	 public boolean queryById(String ordid) throws Exception;
	 public List queryByC_Id(String cusid) throws Exception;
	 public List queryAll() throws Exception;
	 public void delete(int ordid) throws Exception;
	 public void update(Orders ord) throws Exception;
	 public void storemessageclear();
}

类UserDAO

//员工(管理员)操作接口

package DAO;

import java.util.ArrayList;
import java.util.List;

import StoreManager.User;

public interface UserDAO {
	public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById = new String[1][4];
	 public void insert(User user) throws Exception;
	 public void delete(String userid) throws Exception;
	 public String[][] queryById(String userid) throws Exception;
	 public List queryAll() throws Exception;
	 public void update(User user) throws Exception;
	 
}

包sqlExecuteImpl

//接口功能实现

类login

//登录功能

package SqlExecute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import StoreManager.Customer;
import StoreManager.User;
import Connection.connect;

public class login {
	
	public int flag = 0;
	public void queryLogin(User user) throws Exception {
		 String sql = "select*from flowerstore where id = ? and code = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, user.getUserid());
		 pstmt.setString(2, user.getPassword()); 
		 rs = pstmt.executeQuery();
				 if(rs.next())
				 {
					 flag = 1;
				 }
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	}
	
	public void queryLogin1(Customer cus) throws Exception {
		 String sql = "select*from customer where id = ? and code = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cus.getCusid());
		 pstmt.setString(2, cus.getPassword()); 
		 rs = pstmt.executeQuery();
				 if(rs.next())
				 {
					 flag = 1;
				 }
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	}
}

类SqlCustomerImpl

//对顾客表功能

package SqlExecute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Connection.connect;
import DAO.CustomerDAO;
import StoreManager.Customer;
import StoreManager.User;

public class SqlCustomerImpl implements CustomerDAO{
	 public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById = new String[1][4];
	 public int insert(Customer cus) throws Exception {
	 String sql = "INSERT INTO customer VALUES (?,?,?,?)";
	 PreparedStatement pstmt = null;
	 connect dbc = null;
	 //下面是针对数据库的具体操作
	 try{
	 dbc= new connect();
	 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
	 pstmt.setString(1,cus.getCusid());
	 pstmt.setString(2, cus.getPassword());
	 pstmt.setString(3, cus.getCusphone());
	 pstmt.setDouble(4, cus.getBalance());
	 return pstmt.executeUpdate();
	 }catch(Exception e){
	 throw new Exception ("操作出现异常");
	 }finally{
	 //关闭数据库
	 dbc.close();
	 }
	 }
	 
	 public void delete(String cusid) throws Exception {
		 String sql = "delete from customer where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cusid);
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public String[][] queryById(String cusid) throws Exception{
		 String sql = "select*from customer where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cusid);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				ById[0][0] = rs.getString(1);
				ById[0][1] = rs.getString(2);
				ById[0][2] = rs.getString(3);
				ById[0][3] = rs.getDouble(4)+"";
			 	return ById;
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return null;
	 }
	 
	 public List queryAll() throws Exception {
		 String sql = "select*from customer";
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 Statement sta = dbc.getConnection().createStatement();
		 rs = sta.executeQuery(sql);
		 while(rs.next())
			{
			 	storemessage.add(rs.getString(1));
			 	storemessage.add(rs.getString(2));
			 	storemessage.add(rs.getString(3));
			 	storemessage.add(rs.getDouble(4)+"");
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 
	 return storemessage;
	 }
	 
	 public void update(Customer cus) throws Exception {
		 String sql = "update customer set code = ?,phone = ?,balance = ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cus.getPassword());
		 pstmt.setString(2, cus.getCusphone());
		 pstmt.setDouble(3, cus.getBalance());
		 pstmt.setString(4, cus.getCusid());
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public void update2(Customer cus) throws Exception {
		 String sql = "update customer set code = ?,phone = ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cus.getPassword());
		 pstmt.setString(2, cus.getCusphone());
		 pstmt.setString(3, cus.getCusid());
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public int update1(Customer cus) throws Exception {
		 String sql = "update customer set balance = balance + ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setDouble(1, cus.getBalance());
		 pstmt.setString(2, cus.getCusid());
		 return pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public int shopping(double balance,String id) throws Exception {
		 String sql = "update customer set balance = balance - ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setDouble(1, balance);
		 pstmt.setString(2, id);
		 return pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public double getBalance(String id) throws Exception{
		 String sql = "select balance from customer where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, id);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				return rs.getDouble(1);
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return 0;
	 }
}

类SqlFlowerImpl

//对鲜花表功能

package SqlExecute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import StoreManager.Flower;
import Connection.connect;
import DAO.FlowerDAO;

public class SqlFlowerImpl implements FlowerDAO{
	 public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById_Name = new String[1][4];
	 public void insert(Flower flo) throws Exception {
	 String sql = "INSERT INTO flower VALUES (?,?,?,?)";
	 PreparedStatement pstmt = null;
	 connect dbc = null;
	 //下面是针对数据库的具体操作
	 try{
	 dbc= new connect();
	 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
	 pstmt.setLong(1, flo.getFlid());
	 pstmt.setString(2, flo.getFlname());
	 pstmt.setDouble(3, flo.getPrice());
	 pstmt.setInt(4, flo.getStore());
	 flagin = pstmt.executeUpdate();
	 }catch(Exception e){
	 throw new Exception ("操作出现异常");
	 }finally{
	 //关闭数据库
	 dbc.close();
	 }    
	 }
	 
	 public void delete(int flid) throws Exception {
		 String sql = "delete from flower where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setLong(1, flid);
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public String[][] queryById(int flid) throws Exception{
		 String sql = "select*from flower where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setLong(1, flid);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				ById_Name[0][0] = rs.getInt(1)+"";
				ById_Name[0][1] = rs.getString(2);
				ById_Name[0][2] = rs.getDouble(3)+"";
				ById_Name[0][3] = rs.getInt(4)+"";
			 	return ById_Name;
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return null;
	 }
	 
	 public String[][] queryByName(String flname) throws Exception{
		 String sql = "select*from flower where name = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, flname);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				ById_Name[0][0] = rs.getInt(1)+"";
				ById_Name[0][1] = rs.getString(2);
				ById_Name[0][2] = rs.getDouble(3)+"";
				ById_Name[0][3] = rs.getInt(4)+"";
			 	return ById_Name;
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return null;
	 }
	 
	 public double getPrice(String flname) throws Exception{
		 String sql = "select*from flower where name = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, flname);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				return rs.getDouble(3);
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return 0;
	 }
	 
	 public List queryAll() throws Exception {
		 String sql = "select*from flower";
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 Statement sta = dbc.getConnection().createStatement();
		 rs = sta.executeQuery(sql);
		 while(rs.next())
			{
			 	storemessage.add(rs.getInt(1)+"");
			 	storemessage.add(rs.getString(2));
			 	storemessage.add(rs.getDouble(3)+"");
			 	storemessage.add(rs.getInt(4)+"");
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 
	 return storemessage;
	 }
	 
	 public List queryComBox() throws Exception {
		 String sql = "select*from flower";
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 Statement sta = dbc.getConnection().createStatement();
		 rs = sta.executeQuery(sql);
		 while(rs.next())
			{
			 	storemessage.add(rs.getString(2));
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 
	 return storemessage;
	 }
	 
	 public void update(Flower flo) throws Exception {
		 String sql = "update flower set price = ?,store = ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setDouble(1, flo.getPrice());
		 pstmt.setInt(2, flo.getStore());
		 pstmt.setInt(3, flo.getFlid());
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public int getStore(String name) throws Exception{
		 String sql = "select store from flower where name = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, name);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				return rs.getInt(1);
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return 0;
	 }
	 
	 public int shopping(int number,String name) throws Exception {
		 String sql = "update flower set store = store - ? where name = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setInt(1, number);
		 pstmt.setString(2, name);
		 return pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
}

类SqlManagerImpl

//对店员表功能


package SqlExecute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Connection.connect;
import DAO.UserDAO;
import StoreManager.User;

public class SqlManagerImpl implements UserDAO{
	//添加操作
	 public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById = new String[1][4];
	 public void insert(User user) throws Exception {
	 String sql = "INSERT INTO flowerstore VALUES (?,?,?,?)";
	 PreparedStatement pstmt = null;
	 connect dbc = null;
	 //下面是针对数据库的具体操作
	 try{
	 dbc= new connect();
	 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
	 pstmt.setString(1, user.getUserid());
	 pstmt.setString(2, user.getPassword());
	 pstmt.setString(3, user.getUserpost());
	 pstmt.setString(4, user.getUserphone());
	 flagin = pstmt.executeUpdate();
	 }catch(Exception e){
	 throw new Exception ("操作出现异常");
	 }finally{
	 //关闭数据库
	 dbc.close();
	 }    
	 }
	 
	 public void delete(String userid) throws Exception {
		 String sql = "delete from flowerstore where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, userid);
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public String[][] queryById(String userid) throws Exception{
		 String sql = "select*from flowerstore where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, userid);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				ById[0][0] = rs.getString(1);
				ById[0][1] = rs.getString(2);
				ById[0][2] = rs.getString(3);
				ById[0][3] = rs.getString(4);
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return ById;
	 }
	 
	 public List queryAll() throws Exception {
		 String sql = "select*from flowerstore";
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 Statement sta = dbc.getConnection().createStatement();
		 rs = sta.executeQuery(sql);
		 while(rs.next())
			{
			 	storemessage.add(rs.getString(1));
			 	storemessage.add(rs.getString(2));
			 	storemessage.add(rs.getString(3));
			 	storemessage.add(rs.getString(4));
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 
	 return storemessage;
	 }
	 
	 public void update(User user) throws Exception {
		 String sql = "update flowerstore set code = ?,post = ?,phone = ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, user.getPassword());
		 pstmt.setString(2, user.getUserpost());
		 pstmt.setString(3, user.getUserphone());
		 pstmt.setString(4, user.getUserid());
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
}

类SqlOrdersImpl

//对订单表功能

package SqlExecute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Connection.connect;
import DAO.OrdersDAO;
import StoreManager.Orders;
import StoreManager.User;

public class SqlOrdersImpl implements OrdersDAO{
	 public int flagin = 0;
	 public int i = 1;
	 List storemessage = new ArrayList();
	 public String[][] ById = new String[1][5];
	 
	 public void insert(String s1,String s2,String s3,double s4) throws Exception {
		 String sql = "INSERT into orders (c_id,message,datetime,money) VALUES (?,?,?,?)";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, s1);
		 pstmt.setString(2, s2);
		 pstmt.setString(3, s3);
		 pstmt.setDouble(4, s4);
		 flagin = pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
		 }
	 
	 public void delete(int ordid) throws Exception {
		 String sql = "delete from orders where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setLong(1, ordid);
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public boolean queryById(String ordid) throws Exception{
		 String sql = "select*from orders where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, ordid);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			if(rs.next())
			{
				ById[0][0] = rs.getInt(1)+"";
				ById[0][1] = rs.getString(2);
				ById[0][2] = rs.getString(3);
				ById[0][3] = rs.getString(4);
				ById[0][4] = rs.getDouble(5)+"";
			 	return true;
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return false;
	 }
	 
	 public List queryByC_Id(String cusid) throws Exception{
		 String sql = "select*from orders where c_id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, cusid);
		 //进行数据库更新
		 rs  = pstmt.executeQuery();
			while(rs.next())
			{
				storemessage.add(rs.getInt(1)+"");
			 	storemessage.add(rs.getString(2));
			 	storemessage.add(rs.getString(3));
			 	storemessage.add(rs.getString(4)+"");
			 	storemessage.add(rs.getDouble(5)+"");
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 return storemessage;
	 }
	 
	 public List queryAll() throws Exception {
		 String sql = "select*from orders";
		 connect dbc = null;
		 ResultSet rs = null;	
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 Statement sta = dbc.getConnection().createStatement();
		 rs = sta.executeQuery(sql);
		 while(rs.next())
			{
			 	storemessage.add(rs.getInt(1)+"");
			 	storemessage.add(rs.getString(2));
			 	storemessage.add(rs.getString(3));
			 	storemessage.add(rs.getString(4)+"");
			 	storemessage.add(rs.getDouble(5)+"");
			}
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 } 
		 
	 return storemessage;
	 }
	 
	 public void update(Orders ord) throws Exception {
		 String sql = "update orders set c_id = ?,message = ?,datetime = ?, money = ? where id = ?";
		 PreparedStatement pstmt = null;
		 connect dbc = null;
		 //下面是针对数据库的具体操作
		 try{
		 dbc= new connect();
		 pstmt = (PreparedStatement)dbc.getConnection().prepareStatement(sql);
		 pstmt.setString(1, ord.getCusid());
		 pstmt.setString(2, ord.getMessage());
		 pstmt.setString(3, ord.getDatetime());
		 pstmt.setDouble(4, ord.getMoney());
		 pstmt.setLong(5, ord.getOrdid());
		 pstmt.executeUpdate();
		 }catch(Exception e){
		 throw new Exception ("操作出现异常");
		 }finally{
		 //关闭数据库
		 dbc.close();
		 }    
	 }
	 
	 public void storemessageclear() {
		storemessage.clear();
	}
}

你可能感兴趣的:(sql,mysql,eclipse)