单例模式和DAO模式
一、单例模式(设计模式的一种)
####################################懒汉式#########################################
public class TestSingle {
private static TestSingle t =null;
public synchronized static TestSingle newInstance(){
if(t==null){
return t=new TestSingle();
}
return t;
}
private TestSingle(){
}
public static void main(String[] args) {
TestSingle ts= newInstance();
System.out.println(ts);
TestSingle ts1=newInstance();
System.out.println(ts1);
}
############################运行结果##############################################
cn.kgc.kb11.TestSingle@74a14482
cn.kgc.kb11.TestSingle@74a14482
###############################饿汉式##############################################
public class TestSingle2 {
private static TestSingle2 ts=null;
private TestSingle2(){
}
static{
ts=new TestSingle2();
}
public static TestSingle2 getInstance(){
return ts;
}
public static void main(String[] args) {
TestSingle2 ts1=getInstance();
System.out.println(ts1);
TestSingle2 ts2=getInstance();
System.out.println(ts2);
}
}
###############################运行结果###########################################
cn.kgc.kb11.TestSingle2@74a14482
cn.kgc.kb11.TestSingle2@74a14482
##########################代理模式##############################################
public class Pet {
public static final String TYPE_ONE="Dog";
public static final String TYPE_TWO="Cat";
public static Pet getInstance(String type){
if(null==type)
return null;
if(type.equals(TYPE_ONE)){
return new Dog();
}
if(type.equals(TYPE_TWO)){
return new Cat();
}
return null;
}
}
class Dog extends Pet{
}
class Cat extends Pet{
}
二、DAO模式
package cn.kgc.kb11.dao;
import cn.kgc.kb11.dao.impl.BaseDAOImpl;
import cn.kgc.kb11.entity.Student;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StudentDao extends BaseDAOImpl {
private final String DRIVER="com.mysql.jdbc.Driver";
private final String URL="jdbc:mysql://192.168.186.100:3306/test";
//根据学生id查询所有信息
public Student getStudentById(String id) {
String sql = "select * from student where stu_id=?";
getConn(DRIVER, URL, "root", "ok");
query(sql,id);
ResultSet rs = getRs();
//Data Access object
Student s = new Student();
try {
if (rs.next()) {
s.setStu_id(rs.getInt("stu_id"));
s.setStu_name(rs.getString("stu_name"));
s.setGrade_id(rs.getInt("grade_id"));
s.setGender(rs.getString("gender"));
s.setAddress(rs.getString("address"));
s.setPhone(rs.getString("phone"));
s.setIdCard(rs.getString("idCard"));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
close();
}
return s;
}
public boolean insertIntoStudent(Student s){
String sql="insert into student values(?,?,?,?,?,?,?)";
getConn(DRIVER,URL,"root","ok");
boolean update = update(sql, s.getStu_id() + "", s.getStu_name(), s.getGrade_id() + "", s.getGender(),null,null,null);
if(update) return true;
else return false;
}
public static void main(String[] args) {
StudentDao dao=new StudentDao();
Student s = new Student(18,"白眉鹰王",3,"男",null,null,null);
// Student student =dao.getStudentById("7");
// System.out.println(student);
boolean b = dao.insertIntoStudent(s);
System.out.println(b ? "插入成功" : "插入失败");
}
}
三、配置文件加载数据库访问参数
如果在java代码里添加数据库的访问信息,那么每次访问的数据库放生变化时,java里都需要重启服务,很麻烦,所以才有加载数据库访问参数的配置文件出现,修改起来比较方便。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.186.100:3306/test
user=root
pwd=ok
public class Prop {
static Properties p = new Properties();
static {
try {
p.load(new FileInputStream("resources/db.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return p.getProperty(key);
}
}