来源:旋律~学 发布时间:2018-11-24 11:13:54 阅读量:1210
上一篇博客写到如何连接数据库和增删查改,但是每一次操作都需要写一个类,体现不到类的封装性,这篇博客讲详细介绍到如何封装类,并快速实现增删查改。
前提准备:
1.建包
com.tao.entity 用于盛放实体类
com.tao.dao 用于接口的实现类
com.tao.test 用于测试类
2.建表
通过mysql,在school数据库下建立person表,
字段有id(int),name(varchar),pass(varchar),birthday(date)
1
2
3
4
5
6
7
BaseDao是一个基类,里面写好了一个连接数据库的方法connect()和一个 关闭资源的方法closeAll();其他接口的实现类继承这个类,并调用他的方法即可。
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
接下来,我们可以创建一个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类,这个类继承BaseDao,实现增删查改的方法
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;
}
/*
* 查询一个人
*/
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
最后写测试类,测试结果
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) {
/*
* 登录测试
*/
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);
}
/*
* 删除测试
*/
int row = pdao.delete(5);
if (row > 0) {
System.out.println("del success..");
} else {
System.out.println("del failed");
}
/*
* 插入测试
*/
/*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");
}*/
/*
* 查询一个获取学生并修改姓名
*
*/
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");
}
}
}
---------------------