您好,登錄后才能下訂單哦!
java單例模式學(xué)習(xí)筆記
工具層:
import java.sql.Connection;
import java.sql.DriverManager;
//工具層
//DBHelper類,體現(xiàn)了單例模式的思想
public class DBHelper {
private static Connection conn; // 數(shù)據(jù)庫連接對象
// 驅(qū)動
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 數(shù)據(jù)庫的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";
// 在靜態(tài)代碼塊中,負(fù)責(zé)加載驅(qū)動
static {
try {
Class.forName(DRIVER);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 既然是單例模式,應(yīng)該讓構(gòu)造方法私有化
private DBHelper() {
}
// 外部調(diào)用的統(tǒng)一的方法
public static Connection getConnection() throws Exception {
if (conn == null) {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
return conn;
}
}
業(yè)務(wù)邏輯層:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//DAO層
//學(xué)生業(yè)務(wù)邏輯類
public class StudentsDAO {
public static Connection conn = null;// 連接對象
public static PreparedStatement stmt = null;
public static ResultSet rs = null;// 結(jié)果集
// 添加學(xué)生方法
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();
}
}
// 查詢所有的學(xué)生資料
public static void query() {
String sql = "select * from Students;";
try {
conn = DBHelper.getConnection();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("學(xué)號:" + 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;//學(xué)號
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;
}
//保留默認(rèn)的不帶參數(shù)的構(gòu)造方法
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","武當(dāng)山");
StudentsDAO sdao = new StudentsDAO();
if(sdao.addStudents(s))
{
System.out.println("添加成功!");
}
else
{
System.out.println("添加失?。?);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
StudentsDAO.query();
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。