//Javabean。pojo
package com.dhcode.Bean;
import java.io.Serializable;
public class DOG implements Serializable{
private int id;
private String name;
private int health;
private int love;
private String strain;
public DOG() {
super();
}
public DOG(int id,String name,int health,int love,String strain) {
super();
this.id = id;
this.name = name;
this.health = health;
this.love = love;
this.strain = strain;
}
public DOG(String name, int health , int love, String strain) {
super();
this.health = health;
this.name = name;
this.love = love;
this.strain = strain;
}
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 int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
}
//声明
package com.dhcode.SM;
import java.util.List;
import com.dhcode.Bean.DOG;
public interface DogDAO {
public void addDog(DOG dog);
public int deleteDog(int id);
public int updateDog(DOG dog);
public List getAllDog();
public DOG getDogById(int id);
public List getDogBycondition(String name,String strain);
}
//实现impl
package com.dhcode.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 com.dhcode.Bean.DOG;
import com.dhcode.SM.DogDAO;
import com.dhcode.Until.DBUntil;
public class Dogimpl implements DogDAO{
Connection con=null;
PreparedStatement pstmt = null;
ResultSet rs = null;
@Override
public void addDog(DOG dog) {
// TODO Auto-generated method stub
String sql = "insert into dog values(dog_emp.nextval,?,?,?,?)";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dog.getName());
pstmt.setInt(2, dog.getHealth());
pstmt.setInt(3, dog.getLove());
pstmt.setString(4, dog.getStrain());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUntil.closeAll(rs,pstmt,con);
}
}
@Override
public int deleteDog(int id) {
// TODO Auto-generated method stub
int num = 0;
String sql = "delete from dog where id=?";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setObject(1, id);
num = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUntil.closeAll(rs, pstmt, con);
}
return num;
}
@Override
public int updateDog(DOG dog) {
// TODO Auto-generated method stub
int num = 0;
String sql = "update dog set name = ?,health =?,love =?,strain=?,where id=?";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dog.getName());
pstmt.setInt(2, dog.getHealth());
pstmt.setInt(3, dog.getLove());
pstmt.setString(4, dog.getStrain());
pstmt.setInt(5,dog.getId());
num = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
@Override
public List getAllDog() {
// TODO Auto-generated method stub
List dogs = new ArrayList();
String sql = "select * from dog";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
DOG dog = new DOG(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5));
dogs.add(dog);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUntil.closeAll(rs, pstmt, con);
}
return dogs;
}
@Override
public DOG getDogById(int id) {
// TODO Auto-generated method stub
DOG dog = null;
String sql = "select * from student where id=?";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
dog = new DOG(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUntil.closeAll(rs, pstmt, con);
}
return dog;
}
@Override
public List getDogBycondition(String name, String strain) {
// TODO Auto-generated method stub
List dogs = new ArrayList();
String sql = "select * from dog where name ? like ? and strain=?";
try {
con = DBUntil.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setObject(1, "%"+name+"%");
pstmt.setObject(2, strain);
rs = pstmt.executeQuery();
while(rs.next()) {
DOG dog = new DOG(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5));
dogs.add(dog);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUntil.closeAll(rs, pstmt, con);
}
return dogs;
}
}
//DBuntil.java加载连接
package com.dhcode.Until;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUntil {
public Connection con = null;
public PreparedStatement pstmt = null;
public ResultSet rs = null;
public DBUntil() {
}
public static Connection getConnection() {
PropertiesUntils.loadFile("database.properties");
String url = PropertiesUntils.getPropertyValue("url");
String username = PropertiesUntils.getPropertyValue("user");
String passwd = PropertiesUntils.getPropertyValue("password");
String driver = PropertiesUntils.getPropertyValue("driver");
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,username,passwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
if(con==null) {
System.out.println("连接错误!");
}
return con;
}
public static void closeAll(ResultSet rs,PreparedStatement pstmt,
Connection con) {
try {
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public static void main(String args[]) {
//
// try {
// Connection con = DBUntil.getConnection();
// PreparedStatement pstmt = con.prepareStatement("Select * from dog");
// ResultSet rs = pstmt.executeQuery();
//
// while (rs.next()) {
// System.out.println(rs.getInt(1)+"\t");
// System.out.println(rs.getString(2)+"\t");
// System.out.println(rs.getInt(3)+"\t");
// System.out.println(rs.getInt(4)+"\t");
// System.out.println(rs.getString(5));
// }
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}
//外建propertiesUntil
package com.dhcode.Until;
import java.io.IOException;
import java.util.Properties;
public class PropertiesUntils {
static Properties property = new Properties();
public static String getPropertyValue(String key) {
return property.getProperty(key);
}
public static boolean loadFile(String fileName) {
// TODO Auto-generated method stub
try {
property.load(PropertiesUntils.class.getClassLoader().getResourceAsStream(fileName));
}catch(IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
//配置文件oracle
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
user=root
password=123456
//配置文件纯Javamysql
driver=com.mysql.jdbc.Driver
//#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root