62,614
社区成员
发帖
与我相关
我的任务
分享
package cn.jbit.vo;
/**
* 宠物类
*/
public class Pet {
private int id;//宠物id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int masterId;//主人id
public int getMasterId() {
return masterId;
}
public void setMasterId(int masterId) {
this.masterId = masterId;
}
private String name;//昵称
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int typeId;//类型id
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
private int health;//健康值
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
private int love ;//亲密度
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
private String adoptTime;//领养时间
public String getAdoptTime() {
return adoptTime;
}
public void setAdoptTime(String adoptTime) {
this.adoptTime = adoptTime;
}
private int status;//状态
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
package cn.jbit.dao;
import java.util.List;
import cn.jbit.vo.Pet;
/**
* 接口
*/
public interface IPetDAO {
public boolean doCreate(Pet pet)throws Exception;
public List<Pet>findInfo(int keyword)throws Exception;
}
package cn.jbit.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
/**
* 数据库连接
*/
public class DataBaseConnection {
//记录日志
private Logger logger = Logger.getLogger(DataBaseConnection.class);
private static final String driverClassName = "oracle.jdbc.driver.OracleDriver"; //驱动字符串
private static final String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle10"; //连接字符串
private static final String user = "admin";
private static final String password = "admin";
private Connection conn = null;
public DataBaseConnection()throws Exception{
Class.forName(driverClassName);
this.conn = DriverManager.getConnection(url,user,password);
logger.debug("连接成功");
}
public Connection getConnection(){
return this.conn;
}
/**
* 关闭连接
*/
public void close(){
if(null!=conn){
try {
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
package cn.jbit.factory;
import cn.jbit.Proxy.PetDaoProxy;
import cn.jbit.dao.IPetDAO;
public class factory {
public static IPetDAO getIpetDAOinstance()throws
Exception{
return new PetDaoProxy();
}
}
package cn.jbit.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.jbit.dao.IPetDAO;
import cn.jbit.vo.Pet;
public class PetDaoImpl implements IPetDAO {
Connection conn = null;
PreparedStatement patmt = null;
ResultSet rs = null;
public PetDaoImpl(Connection conn){
this.conn = conn;
}
public boolean doCreate(Pet pet) throws Exception {
boolean flag = false;
String sql = "insert into pet values(pet_sep.nextval,?,?,?,?,?,sysdate,?)";
this.patmt = this.conn.prepareStatement(sql);
this.patmt.setInt(1, pet.getMasterId());
this.patmt.setString(2, pet.getName());
this.patmt.setInt(3,pet.getTypeId());
this.patmt.setInt(4, pet.getHealth());
this.patmt.setInt(5, pet.getLove());
this.patmt.setInt(6, pet.getStatus());
if(this.patmt.executeUpdate()>0){
System.out.println("插入成功");
flag = true;
}
return flag;
}
public List<Pet> findInfo(int keyword) throws Exception {
List<Pet> list = new ArrayList<Pet>();
String sql = "select id,master_id,name,health,love from pet where id = ?";
this.patmt = this.conn.prepareStatement(sql);
this.patmt.setInt(1, keyword);
rs = this.patmt.executeQuery();
Pet pet = new Pet();
while(rs.next()){
pet = new Pet();
pet.setId(rs.getInt(1));
pet.setMasterId(rs.getInt(2));
pet.setName(rs.getString(3));
pet.setHealth(rs.getInt(4));
pet.setLove(rs.getInt(5));
}
list.add(pet);
System.out.println("***狗狗***");
System.out.println("编号\t主人编号\t名字\t健康值\t亲密度");
System.out.print(pet.getId()+"\t");
System.out.print(pet.getMasterId()+"\t");
System.out.print(pet.getName()+"\t");
System.out.print(pet.getHealth()+"\t");
System.out.print(pet.getLove()+"\n");
this.patmt.close();
return list;
}
}
package cn.jbit.Test;
import cn.jbit.factory.factory;
import cn.jbit.vo.Pet;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Pet pet=new Pet();
pet.setMasterId(1);
pet.setName("СС");
pet.setTypeId(1);
pet.setHealth(85);
pet.setLove(100);
pet.setStatus(1);
try {
factory.getIpetDAOinstance().doCreate(pet);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
/**
* 测试类
*
*/
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入你要查询的编号:");
int no = input.nextInt();
try {
serachData(no);
} catch (Exception e) {
System.err.println(e);
}
}
/**
* 按编号查询狗狗的信息
* @param no
* @throws Exception
*/
public static void serachData(int no) throws Exception{
Connection connection = Connection();
PreparedStatement pstms = null;
ResultSet rs = null;
String sql = "select id,master_id,name,health,love from pet where id = ?";
pstms = connection.prepareStatement(sql);
pstms.setInt(1, no);
rs = pstms.executeQuery();
System.out.println("***狗狗***");
System.out.println("编号\t主人编号\t名字\t健康值\t亲密度");
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getInt(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5));
}
}
/**
* 连接的方法
* @return conn
* @throws Exception
*/
public static Connection Connection() throws Exception{
Connection conn = null;
if(conn == null){
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:oracle10","admin","admin");
System.out.println("连接成功");
}
return conn;
}
}