In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
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 ()
}
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.