Learning notes of java singleton pattern
Learning notes of java singleton pattern
Tool layer:
Import java.sql.Connection
Import java.sql.DriverManager
/ / tool layer
/ / DBHelper class, which embodies the idea of singleton pattern
Public class DBHelper {
Private static Connection conn; / / Database connection object
/ / driver
Private static final String DRIVER = "com.mysql.jdbc.Driver"
/ / URL address of the database
Private static final String URL = "jdbc:mysql://192.168.47.196:3306/school?useUnicode=true&characterEncoding=UTF-8"
/ / user name
Private static final String USERNAME = "root"
/ / password
Private static final String PASSWORD = "123456"
/ / in the static code block, responsible for loading the driver
Static {
Try {
Class.forName (DRIVER)
} catch (Exception ex) {
Ex.printStackTrace ()
}
}
/ / since it is a singleton pattern, the constructor should be privatized.
Private DBHelper () {
}
/ / A unified method for external calls
Public static Connection getConnection () throws Exception {
If (conn = = null) {
Conn = DriverManager.getConnection (URL, USERNAME, PASSWORD)
}
Return conn
}
}
Business logic layer:
Import java.sql.Connection
Import java.sql.PreparedStatement
Import java.sql.ResultSet
/ / DAO layer
/ / Student business logic class
Public class StudentsDAO {
Public static Connection conn = null;// connection object
Public static PreparedStatement stmt = null
Public static ResultSet rs = null;// result set
/ / add student method
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 ()
}
}
/ / query all student information
Public static void query () {
String sql = "select * from Students;"
Try {
Conn = DBHelper.getConnection ()
Rs = stmt.executeQuery (sql)
While (rs.next ()) {
System.out.println ("student number:" + rs.getInt ("sid") + ", name:" + rs.getString ("sname") + ", age:" + rs.getInt ("age")
+ ", gender:" + rs.getString ("gender")
}
} catch (Exception ex) {
Ex.printStackTrace ()
} finally {
DestoryResource ()
}
}
/ / method of releasing resources
Public static void destoryResource () {
Try {
/ / if (rswatches null)
/ / {
/ / rs.close ()
/ / rs=null
/ /}
If (stmt! = null) {
Stmt.close ()
Stmt = null
}
/ *
* do not release Connection resource if (connexion null) {conn.close (); conn=null;} for the time being
, /
} catch (Exception ex) {
Ex.printStackTrace ()
}
}
}
Physical layer:
Public class Students {
Private int sid;// student number
Private String sname;// name
Private int age;// age
Private String gender;// gender
Private String email;// email address
Private String address;// 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
}
/ / keep the default construction method without parameters
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
}
}
Test layer:
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 ()
/ / Test whether it is in singleton mode
System.out.println (conn1==conn2)
Students s = new Students (55, "Zhang Wuji", 3, "male", "zwj@123.com", "Wudang Mountain")
StudentsDAO sdao = new StudentsDAO ()
If (sdao.addStudents (s)
{
System.out.println ("added successfully!")
}
Else
{
System.out.println ("add failed!")
}
}
Catch (Exception ex)
{
Ex.printStackTrace ()
}
StudentsDAO.query ()
}
}