作为一名新人,这是我这个废柴大一狗大一狗的练习作业,要用什么似懂非懂的DAO程序设计模式写
要求过程是这样的
过程大概是这样的,讲实话我看的是一脸懵逼,不过作业总是要交差的,只能硬着头皮,写一点是一点了
首先建表
然后我就头一次破天荒一脸懵逼的分了五个包
大概就是一个连接数据库用的,还有一个接口包,还有一个包是实体类,还有一个是用来写具体的实现,最后一个就是测试了
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import com.cn.Test.Main;
import com.cn.Test.Manager;
import com.cn.dao.BaseDao;
import com.cn.entity.Account;
import com.cn.entity.Pet;
import com.cn.entity.PetOwner;
import com.cn.entity.PetStore;
import com.cn.impl.PetOwnerService;
public class PetOwnerServiceImpl extends BaseDao implements PetOwnerService{
private String sname;
public static int i;
Scanner sc = new Scanner(System.in);
private int sell;
//登录验证实现方法
@Override
public HashMap LoginCheck() throws Exception {
BaseDao.selectPetOwnerServiceDao();
System.out.println("请先登录,请输入主人名字:");
sname=sc.next();
System.out.println("\n请输入主人密码:");
String spassword=sc.next();
Set keySet = hm.keySet();
Iterator it = keySet.iterator();
boolean falg = false;
while (it.hasNext()) {
Object user = it.next();
Object pw = hm.get(user);
if (sname.equals(user)&&spassword.equals(pw)) {
System.out.println("登录成功");
Manager.PetDeal();
falg = true;
}
}
if (falg==false) {
System.out.println("用户不存在,请重新输入!!!");
LoginCheck();
}
return hm;
}
@Override
//查看登录的宠物主人实现方法
public ResultSet selectPetOwnerService(PetOwner po) throws Exception {
String sql="select * from petowner where name='"+sname+"';";
ResultSet i = this.selectPetOwnerServiceDao(sql);
return i;
}
//查看宠物库存实现方法
@Override
public ResultSet InventoryPetOwnerService(Pet p) throws Exception {
String sql="select * from pet;";
ResultSet i = this.selectInventoryPetDao(sql);
return i;
}
//购买库存宠物实现方法
@Override
public int BuyInventoryPetOwner(Pet p, Account ac) throws Exception {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("---请选择要购买哪一个宠物,并输入序号---");
int id=sc.nextInt();
System.out.println("请输入交易金额:");
int money=sc.nextInt();
String sql1="UPDATE pet SET owner_id ="+i+" WHERE id="+id+" ;";
String sql4="UPDATE pet SET store_id ="+"null"+" WHERE id="+id+" ;";
String sql2="insert account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values("+1+","+id+","+i+","+"(SELECT store_id FROM pet WHERE id="+id+"),"+money+","+"YEAR(NOW())"+");";
String sql3="update petowner set money=money-"+money+" where id="+i+";";
int i=this.BuyPet(sql1,sql2,sql3,sql4);
return i;
}
//查询我的宠物
@Override
public ResultSet SellSelectPetOwner(Pet p, Account ac) throws Exception {
// TODO Auto-generated method stub
System.out.println("---我的宠物列表---");
String sql="select * from pet where id="+i+";";
ResultSet i = this.OwnerselectPetDao(sql);
return i;
}
//选择出售宠物
@Override
public ResultSet SellPetOwner(Pet p) throws Exception {
System.out.println("---请输入要出售的宠物序号---");
sell=sc.nextInt();
String sql="select * from pet where owner_id="+i+";";
ResultSet i=this.SellSelectPetDao(sql);
return i;
}
//出售宠物执行
@Override
public int SellPetOwnerExecute() throws Exception{
int store=sc.nextInt();
String sql1="update pet set store_id="+store+" where id="+sell+";";
String sql4="update pet set owner_id="+"null"+" where id="+sell+";";
System.out.println("请输入交易金额:");
int money=sc.nextInt();
String sql2="update petowner set money=money+"+money+" where id="+i+";";
String sql3="insert account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values("+2+","+sell+","+i+","+store+","+money+","+"YEAR(NOW())"+");";
int i=this.BuyPet(sql1,sql2,sql3,sql4);
return i;
}
}
具体的我就把主人登录页面写完了,具体就一个登录验证,购买和出售的方法
大概就是这样了
package com.cn.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import com.cn.dao.impl.PetOwnerServiceImpl;
import com.cn.entity.Pet;
public class BaseDao {
private static String driver; // 驱动
private static String url; // 链接地址
private static String user; // 用户名
private static Connection conn; // 连接对象
private static String password; // 密码
public static HashMap hm=new HashMap();
private static String name;
//连接Mysql
public static Connection getConnection() throws Exception {
driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
url = "jdbc:MYSQL://localhost:3306/ljt";
user = "root";
password = "root";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* 增,删,该的操作方法
* */
public int execute(String sql,Object[] parmar) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement st = conn.prepareStatement(sql);
//遍历
for (int i = 0; i < parmar.length; i++) {
//通过循环添加值
st.setObject(i+1, parmar[i]);
}
//执行
int i = st.executeUpdate();
return i;
}
//购买宠物执行方法
public int BuyPet(String sql,String sql2,String sql3,String sql4) throws Exception{
conn = getConnection();
//获取st对象
PreparedStatement st = conn.prepareStatement(sql);
PreparedStatement st2 = conn.prepareStatement(sql2);
PreparedStatement st3 = conn.prepareStatement(sql3);
PreparedStatement st4= conn.prepareStatement(sql4);
int i = st.executeUpdate();
i=st2.executeUpdate();
i=st3.executeUpdate();
i=st4.executeUpdate();
System.out.println("交易成功!!!");
System.out.println("台账正确插入一条信息!!!");
return i;
}
//查询Account表
public ResultSet selectAccountDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
while (i.next()) {
int id = i.getInt("id");
int deal_type = i.getInt("deal_type");
int pet_id = i.getInt("pet_id");
int seller_id = i.getInt("seller_id");
int buyer_id = i.getInt("buyer_id");
int price = i.getInt("price");
int deal_time = i.getInt("deal_time");
System.out.println(id+"\t"+deal_type+"\t"+pet_id+"\t"+seller_id+"\t"+buyer_id+"\t"+price+"\t"+deal_time );
}
return i;
}
//查询Pet表执行
public ResultSet selectPetDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"宠物名称");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String type_name=i.getString("type_name");
int health=i.getInt("health");
int love=i.getInt("love");
String birthday=i.getString("birthday");
int owner_id=i.getInt("owner_id");
int store_id=i.getInt("store_id");
System.out.println(id+"\t"+name);
}
return i;
}
//选择出售宠物信息
public ResultSet SellSelectPetDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"宠物名称");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String type_name=i.getString("type_name");
int health=i.getInt("health");
int love=i.getInt("love");
String birthday=i.getString("birthday");
int owner_id=i.getInt("owner_id");
int store_id=i.getInt("store_id");
System.out.println("宠物名字叫:"+name+"\t"+"宠物类别是:"+type_name);
}
return i;
}
//查询库存宠物执行
public ResultSet selectInventoryPetDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"宠物名称"+"\t"+"类型"+"\t"+"元宝数");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String type_name=i.getString("type_name");
int health=i.getInt("health");
int love=i.getInt("love");
String birthday=i.getString("birthday");
int owner_id=i.getInt("owner_id");
int store_id=i.getInt("store_id");
System.out.println(id+"\t"+name+"\t"+type_name);
}
return i;
}
//查询PetStore表执行
public ResultSet selectPetStoreDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"宠物商店");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String password=i.getString("password");
int balance=i.getInt("balance");
System.out.println(id+"\t"+name);
}
return i;
}
//
public static HashMap selectPetOwnerServiceDao() throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
String sql="select * from petowner;";
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
while (i.next()) {
int id = i.getInt("id");
name=i.getString("name");
password=i.getString("password");
int money=i.getInt("money");
hm.put(name, password);
}
return hm;
}
public ResultSet selectPetOwnerDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"主人姓名");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String password=i.getString("password");
int money=i.getInt("money");
System.out.println(id+"\t"+name);
}
return i;
}
public ResultSet selectPetOwnerServiceDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
while (i.next()) {
PetOwnerServiceImpl.i = i.getInt("id");
String name=i.getString("name");
String password=i.getString("password");
int money=i.getInt("money");
System.out.println("名字:"+name);
System.out.println("元宝数:"+money);
}
return i;
}
//查询主人拥有的宠物
public ResultSet OwnerselectPetDao(String sql) throws Exception{
//获取连接对象
conn = getConnection();
//获取st对象
PreparedStatement pt=conn.prepareStatement(sql);
ResultSet i = pt.executeQuery();
System.out.println("序号"+"\t"+"宠物名称"+"\t"+"类型");
while (i.next()) {
int id = i.getInt("id");
String name=i.getString("name");
String type_name=i.getString("type_name");
int health=i.getInt("health");
int love=i.getInt("love");
String birthday=i.getString("birthday");
int owner_id=i.getInt("owner_id");
int store_id=i.getInt("store_id");
System.out.println(id+"\t"+name+"\t"+type_name);
}
return i;
}
}
大部分也就这样了,虽然全是bug,但勉强能把老师糊弄过去了,