來源:旋律~學(xué) 發(fā)布時(shí)間:2018-11-24 11:13:54 閱讀量:1073
上一篇博客寫到如何連接數(shù)據(jù)庫和增刪查改,但是每一次操作都需要寫一個(gè)類,體現(xiàn)不到類的封裝性,這篇博客講詳細(xì)介紹到如何封裝類,并快速實(shí)現(xiàn)增刪查改。
前提準(zhǔn)備:
1.建包
com.tao.entity 用于盛放實(shí)體類
com.tao.dao 用于接口的實(shí)現(xiàn)類
com.tao.test 用于測(cè)試類
2.建表
通過mysql,在school數(shù)據(jù)庫下建立person表,
字段有id(int),name(varchar),pass(varchar),birthday(date)
1
2
3
4
5
6
7
BaseDao是一個(gè)基類,里面寫好了一個(gè)連接數(shù)據(jù)庫的方法connect()和一個(gè) 關(guān)閉資源的方法closeAll();其他接口的實(shí)現(xiàn)類繼承這個(gè)類,并調(diào)用他的方法即可。
BaseDao.java
package com.tao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class BaseDao {
protected Connection conn = null;
protected Statement stmt = null;
protected ResultSet rs = null;
protected String url = "jdbc:mysql://localhost:3306/school?characterEncoding=utf-8";
protected String name = "root";
protected String password = "root";
protected PreparedStatement pstmt=null;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "root");
stmt = conn.createStatement();
} catch(Exception e) {
e.printStackTrace();
}
}
public void closeAll(){
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
if(pstmt!=null){
pstmt.close();
}
}catch(Exception e) {
}
}
}
插入代碼片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
接下來,我們可以創(chuàng)建一個(gè)person類
Person.java
package com.tao.entity;
import java.util.Date;
public class Person {
private int id;
private String name;
private String pass;
private String birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", pass=" + pass
+ ", birthday=" + birthday + "]";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
然后寫PersonDao類,這個(gè)類繼承BaseDao,實(shí)現(xiàn)增刪查改的方法
package com.tao.dao;
import com.tao.entity.Person;
public class PersonDao extends BaseDao {
/*
* 登錄
*/
public Person dologin(String name, String pass) {
Person s = null;
try {
super.connect();
// String sql = "select * from person where name='" + name
// + "' and pass='" + pass + "'";
// rs = stmt.executeQuery(sql);
String sql="select * from person where name=? and pass=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, pass);
rs=pstmt.executeQuery();
while (rs.next()) {
s = new Person();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setPass(rs.getString(3));
s.setBirthday(rs.getString(4));
}
} catch (Exception e) {
// TODO: handle exception
}
return s;
}
/*
* 刪除
*/
public int delete(int id){
int row=0;
try {
super.connect();
String sql="delete from person where id="+id;
row=stmt.executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
}
return row;
}
/*
* 插入
*/
public int insert(Person p){
int row=0;
try {
super.connect();
// String sql="insert into person values("+p.getId()+",'"+p.getName()+"','"+p.getPass()+"','"+p.getBirthday()+"')";
// row=stmt.executeUpdate(sql);
String sql="insert into person(name,pass,birthday) values(?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, p.getName());
pstmt.setString(2, p.getPass());
pstmt.setString(3, p.getBirthday());
row=pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
/*
* 查詢一個(gè)人
*/
public Person get(int id) {
Person s = null;
try {
super.connect();
String sql = "select * from person where id="+id;
rs = stmt.executeQuery(sql);
while (rs.next()) {
s = new Person();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setPass(rs.getString(3));
s.setBirthday(rs.getString(4));
}
} catch (Exception e) {
// TODO: handle exception
}
return s;
}
/*
* 修改
*/
public int update(Person p){
int row=0;
try {
super.connect();
String sql="update person set name='"+p.getName()+"',pass='"+p.getPass()+"', birthday='"+p.getBirthday()+"' where id="+p.getId();
row=stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
最后寫測(cè)試類,測(cè)試結(jié)果
TestP.java
package com.tao.test;
import com.tao.dao.PersonDao;
import com.tao.entity.Person;
public class TestP {
public static void main(String[] args) {
/*
* 登錄測(cè)試
*/
PersonDao pdao = new PersonDao();
Person p1 = pdao.dologin("jack", "1' or'1'='1");
if (p1 == null) {
System.out.println("登錄失敗");
} else {
System.out.println("登陸成功");
System.out.println(p1);
}
/*
* 刪除測(cè)試
*/
int row = pdao.delete(5);
if (row > 0) {
System.out.println("del success..");
} else {
System.out.println("del failed");
}
/*
* 插入測(cè)試
*/
/*Person p2 = new Person();
p2.setName("xiaoliu");
p2.setPass("xiaoliu");
p2.setBirthday("2018-8-18");
int r=pdao.insert(p2);
if (r > 0) {
System.out.println("insert success..");
} else {
System.out.println("insert failed");
}*/
/*
* 查詢一個(gè)獲取學(xué)生并修改姓名
*
*/
Person p3 = new Person();
p3 = pdao.get(3);
p3.setName("tom1");
int rr = pdao.update(p3);
if (rr > 0) {
System.out.println("update success..");
} else {
System.out.println("update failed");
}
}
}
---------------------
在線
客服
服務(wù)時(shí)間:周一至周日 08:30-18:00
選擇下列產(chǎn)品馬上在線溝通:
客服
熱線
7*24小時(shí)客服服務(wù)熱線
關(guān)注
微信
關(guān)注官方微信