java单例模式学习笔记
工具层:
import java.sql.Connection;
import java.sql.DriverManager;
//工具层
//DBHelper类,体现了单例模式的思想
public class DBHelper {
private static Connection conn; // 数据库连接对象
// 驱动
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 数据库的URL地址
private static final String URL = "jdbc:mysql://192.168.47.196:3306/school?useUnicode=true&characterEncoding=UTF-8";
// 用户名
private static final String USERNAME = "root";
// 密码
private static final String PASSWORD = "123456";
// 在静态代码块中,负责加载驱动
static {
try {
Class.forName(DRIVER);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 既然是单例模式,应该让构造方法私有化
private DBHelper() {
}
// 外部调用的统一的方法
public static Connection getConnection() throws Exception {
if (conn == null) {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
return conn;
}
}
业务逻辑层:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//DAO层
//学生业务逻辑类
public class StudentsDAO {
public static Connection conn = null;// 连接对象
public static PreparedStatement stmt = null;
public static ResultSet rs = null;// 结果集
// 添加学生方法
public boolean addStudents(Students s) {
String sql = "insert into Students values (?,?,?,?,?,?);";
try {
conn = DBHelper.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, s.getSid());
stmt.setString(2, s.getSname());
stmt.setInt(3, s.getAge());
stmt.setString(4, s.getGender());
stmt.setString(5, s.getEmail());
stmt.setString(6, s.getAddress());
int result = stmt.executeUpdate();
if (result > 0) {
return true;
} else {
return false;
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
destoryResource();
}
}
// 查询所有的学生资料
public static void query() {
String sql = "select * from Students;";
try {
conn = DBHelper.getConnection();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("学号:" + rs.getInt("sid") + ",姓名:" + rs.getString("sname") + ",年龄:" + rs.getInt("age")
+ ",性别:" + rs.getString("gender"));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
destoryResource();
}
}
// 释放资源的方法
public static void destoryResource() {
try {
// if(rs!=null)
// {
// rs.close();
// rs=null;
// }
if (stmt != null) {
stmt.close();
stmt = null;
}
/*
* 暂时不释放Connection资源 if(conn!=null) { conn.close(); conn=null; }
*/
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
实体层:
public class Students {
private int sid;//学号
private String sname;//姓名
private int age;//年龄
private String gender;//性别
private String email;//电子邮箱
private String address;//地址
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//保留默认的不带参数的构造方法
public Students()
{
}
public Students(int sid,String sname,int age,String gender,String email,String address)
{
this.sid = sid;
this.sname = sname;
this.age = age;
this.gender = gender;
this.email = email;
this.address = address;
}
}
测试层:
import java.sql.Connection;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Connection conn1 = DBHelper.getConnection();
Connection conn2 = DBHelper.getConnection();
//测试是否是单例模式
System.out.println(conn1==conn2);
Students s = new Students(55,"张无忌",3,"男","[email protected]","武当山");
StudentsDAO sdao = new StudentsDAO();
if(sdao.addStudents(s))
{
System.out.println("添加成功!");
}
else
{
System.out.println("添加失败!");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
StudentsDAO.query();
}
}