标签:
package com.dao; import java.util.List; import com.entity.Master; public interface MasterDao { public List<Master> findAll(); public Master findByName(String name); public Master findByNameAndPwd(String name,String pwd); } package com.dao; import java.util.List; import com.entity.Pet_type; public interface Pet_typeDao { public List<String> findAllName(); public Pet_type findByName(String name); } package com.dao; import com.entity.Pet; public interface PetDao { public void save(Pet pet); }
package com.daoImpl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.dao.MasterDao; import com.entity.Master; import com.util.JDBCTool; public class MasterDaoImpl implements MasterDao { private ResultSet rs; @Override public List<Master> findAll() { String sql="SELECT * FROM master"; rs=JDBCTool.Query(sql); ArrayList<Master> masterArray=new ArrayList<Master>(); try{ while(rs.next()){ Master master=new Master(); master.setId(rs.getInt(1)); master.setLoginName(rs.getString(2)); master.setPasswrod(rs.getString(3)); masterArray.add(master); } } catch (SQLException e) { e.printStackTrace(); } return masterArray; } @Override public Master findByName(String name) { String sql="SELECT * FROM master WHERE loginname=?"; rs=JDBCTool.Query(sql, name); Master master=new Master(); try{ master.setId(rs.getInt(1)); master.setLoginName(rs.getString(2)); master.setPasswrod(rs.getString(3)); }catch(SQLException e){ e.printStackTrace(); } return master; } @Override public Master findByNameAndPwd(String name, String pwd) { String sql="SELECT * FROM master WHERE loginname=? AND password=?"; rs=JDBCTool.Query(sql, name, pwd); Master m=new Master(); try{ if(!rs.next()){ return null; } m.setId(rs.getInt(1)); m.setLoginName(rs.getString(2)); m.setPasswrod(rs.getString(3)); }catch(SQLException e){ e.printStackTrace(); } return m; } } package com.daoImpl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.dao.Pet_typeDao; import com.entity.Pet_type; import com.util.JDBCTool; public class Pet_typeDaoImpl implements Pet_typeDao { private ResultSet rs; @Override public List<String> findAllName() { String sql="SELECT name FROM pet_type"; ArrayList<String> names=new ArrayList<String>(); rs=JDBCTool.Query(sql); try { while(rs.next()){ names.add(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } return names; } @Override public Pet_type findByName(String name) { String sql="SELECT * FROM pet_type WHERE NAME=?"; rs=JDBCTool.Query(sql, name); Pet_type pt=new Pet_type(); try{ if(rs.next()){ pt.setId(rs.getInt(1)); pt.setName(rs.getString(2)); } }catch(SQLException e){ e.printStackTrace(); } return pt; } } package com.daoImpl; import com.dao.PetDao; import com.entity.Pet; import com.util.JDBCTool; public class PetDaoImpl implements PetDao { @Override public void save(Pet pet) { String sql="INSERT INTO pet (master_id,name,type_id,health,love,adopt_time)" + "VALUES (?,?,?,?,?,?)"; String sql1="INSERT INTO pet (master_id,name,type_id,love,adopt_time)" + "VALUES (?,?,?,?,?)"; if(pet.getHealth()==0){ JDBCTool.DML(sql1, pet.getMaster_id(),pet.getName(),pet.getType_id(),pet.getLove(),pet.getAdopt_time()); }else{ JDBCTool.DML(sql, pet.getMaster_id(),pet.getName(),pet.getType_id(),pet.getHealth(),pet.getLove(),pet.getAdopt_time()); } } }
package com.entity; public class Master { private int id; private String loginName; private String passwrod; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPasswrod() { return passwrod; } public void setPasswrod(String passwrod) { this.passwrod = passwrod; } public Master(int id, String loginName, String passwrod) { super(); this.id = id; this.loginName = loginName; this.passwrod = passwrod; } public Master() { super(); } } package com.entity; public class Pet_type { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Pet_type() { super(); } public Pet_type(int id, String name) { super(); this.id = id; this.name = name; } } package com.entity; import java.sql.Date; public class Pet { private int id; private int master_id; private String name; private int type_id; private int health; private String love; private Date adopt_time; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getMaster_id() { return master_id; } public void setMaster_id(int master_id) { this.master_id = master_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType_id() { return type_id; } public void setType_id(int type_id) { this.type_id = type_id; } public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } public Date getAdopt_time() { return adopt_time; } public void setAdopt_time(Date adopt_time) { this.adopt_time = adopt_time; } public Pet() { super(); } public Pet( int master_id, String name, int type_id, Date adopt_time) { super(); this.master_id = master_id; this.name = name; this.type_id = type_id; this.adopt_time = adopt_time; } }
package com.server; import java.util.List; import com.dao.MasterDao; import com.dao.PetDao; import com.dao.Pet_typeDao; import com.daoImpl.MasterDaoImpl; import com.daoImpl.PetDaoImpl; import com.daoImpl.Pet_typeDaoImpl; import com.entity.Master; import com.entity.Pet; import com.entity.Pet_type; public class MasterManager { private Master master; private Pet_type tp; private Pet pet; private MasterDao masterdao; private Pet_typeDao pet_typedao; private PetDao petdao; public MasterManager(){ masterdao=new MasterDaoImpl(); pet_typedao=new Pet_typeDaoImpl(); petdao=new PetDaoImpl(); } public boolean login(String name,String pwd){ Master m=masterdao.findByNameAndPwd(name, pwd); this.master=m; return m!=null; } public boolean checkPet(List<String> pets,String petname){ for(String s:pets){ if(s.equals(petname)){ tp=pet_typedao.findByName(s); return true; } } System.out.println("宠物类型有误,领养失败!"); return false; } public List<String> showPet(){ List<String> pets=pet_typedao.findAllName(); for(String petname:pets){ System.out.print(petname+" | "); } System.out.println(); return pets; } public void adoptPet(Pet pt){ pet=pt; pet.setMaster_id(master.getId()); pet.setType_id(tp.getId()); petdao.save(pet); } }
package com.test; import java.sql.Date; import java.util.List; import java.util.Scanner; import com.entity.Pet; import com.server.MasterManager; import com.util.JDBCTool; public class Test { public static void main(String[] args) { JDBCTool.getConnecttion(); Scanner sc=new Scanner(System.in); MasterManager mm=new MasterManager(); System.out.println("----欢迎光临宠物乐园-----"); System.out.print("请输入用户名:"); String user=sc.nextLine(); System.out.print("请输入密码:"); String pwd=sc.nextLine(); boolean isLogin=mm.login(user, pwd); if(!isLogin){ System.out.println("登陆失败,用户名或密码错误!"); System.exit(0); } System.out.println("登陆成功!"); System.out.println("请选择宠物类型:"); List<String> namelist=mm.showPet(); String petname=sc.nextLine(); boolean flag=mm.checkPet(namelist, petname); if(!flag){ System.out.println("宠物类型有误,领养失败!"); System.exit(0); } System.out.print("请输入宠物昵称:"); petname=sc.nextLine(); Pet pt=new Pet(); pt.setName(petname); pt.setAdopt_time(new Date(new java.util.Date().getTime())); mm.adoptPet(pt); System.out.println("领养成功!"); } }
package com.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class JDBCTool { private JDBCTool(){} private static Connection conn; private static PreparedStatement pstml; private static ResultSet rs; public static void getConnecttion(){ Properties prop=new Properties(); try { prop.load(new FileInputStream("src/jdbc.properties")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } String driver=prop.getProperty("driver"); String url=prop.getProperty("url"); String user=prop.getProperty("user"); String pwd=prop.getProperty("pwd"); try { Class.forName(driver); conn=DriverManager.getConnection(url,user,pwd); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static int DML(String sql,Object ... params){//使用可变参数 int num=0; try { pstml=conn.prepareStatement(sql); if(params!=null){ for(int i=0;i<params.length;i++){ pstml.setObject((i+1), params[i]); } } conn.setAutoCommit(false);//关闭自动提交,开启事务 try{ num=pstml.executeUpdate(); conn.commit(); }catch(Exception e){ conn.rollback(); e.printStackTrace(); } } catch (SQLException e) { e.printStackTrace(); } return num; } public static ResultSet Query(String sql,Object ... params){ try { pstml=conn.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { pstml.setObject((i + 1), params[i]); } } return rs = pstml.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void closeAll(){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pstml!=null){ try { pstml.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static boolean isConnect(){ return conn==null?false:true; } }
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db_gz?CharacterEncoding=UTF-8 user=guodaxia pwd=961012gz
CREATE DATABASE db_gz DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE db_gz CREATE TABLE master ( id int AUTO_INCREMENT PRIMARY KEY, loginname varchar(50) UNIQUE NOT NULL, password varchar(50) ); CREATE TABLE pet_type ( id int AUTO_INCREMENT PRIMARY KEY, name varchar(50) UNIQUE NOT NULL ); CREATE TABLE pet ( id int AUTO_INCREMENT PRIMARY KEY, master_id int NOT NULL, name varchar(50) NOT NULL, type_id int NOT NULL, health int NOT NULL DEFAULT 80, love varchar(100), adopt_time date, FOREIGN KEY (master_id) REFERENCES master (id), FOREIGN KEY (type_id) REFERENCES pet_type(id) ); INSERT INTO master (loginname,password) VALUES (‘郭大侠‘,‘123456‘); INSERT INTO pet_type (name) VALUES (‘狗狗‘),(‘企鹅‘),(‘猫咪‘); select * from master; select * from master where loginname=‘郭大侠‘ and password=‘123456‘; SELECT * FROM master WHERE loginname=‘郭大侠‘ AND password=‘123456‘; select * from pet;
标签:
原文地址:http://www.cnblogs.com/aigeileshei/p/5606603.html