您好,登錄后才能下訂單哦!
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,"男","zwj@123.com","武當山");
StudentsDAO sdao = new StudentsDAO();
if(sdao.addStudents(s))
{
System.out.println("添加成功!");
}
else
{
System.out.println("添加失敗!");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
StudentsDAO.query();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。