DAO是Data Access Object數(shù)據(jù)訪問接口,數(shù)據(jù)訪問:顧名思義就是與數(shù)據(jù)庫打交道。夾在業(yè)務(wù)邏輯與數(shù)據(jù)庫資源中間。
在連接數(shù)據(jù)庫過程當(dāng)中,可以定義一個DAO接口,然后編寫一個類來擴展這個DAO類來實現(xiàn)DAO接口中的方法。如:有一個用戶表t_user:id(int) , username(varchar(255)) , password(varchar(255))
定義一個簡單的JAVA類(VO):User
定義一個接口UserDAO
擴展這個接口,實現(xiàn)基本方法
在連接數(shù)據(jù)庫過程當(dāng)中,可以定義一個DAO接口,然后編寫一個類來擴展這個DAO類來實現(xiàn)DAO接口中的方法。如:有一個用戶表t_user:id(int) , username(varchar(255)) , password(varchar(255))
定義一個簡單的JAVA類(VO):User
1
package cn.zhang.org.vo;
2
3
public class User {
4
private String username;
5
private String password;
6
public String getUsername() {
7
return username;
8
}
9
public void setUsername(String username) {
10
this.username = username;
11
}
12
public String getPassword() {
13
return password;
14
}
15
public void setPassword(String password) {
16
this.password = password;
17
}
18
19
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

定義一個接口UserDAO
1
package cn.zhang.org.dao;
2
3
import cn.zhang.org.vo.User;
4
5
public interface UserDAO {
6
boolean isLogin(User user);
7
8
boolean RegUser(User user);
9
}
10

2

3

4

5

6

7

8

9

10

擴展這個接口,實現(xiàn)基本方法
1
ackage cn.zhang.org.imp;
2
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
8
import cn.zhang.org.dao.UserDAO;
9
import cn.zhang.org.exception.MyRuntimeException;
10
import cn.zhang.org.factory.Factory;
11
import cn.zhang.org.vo.User;
12
13
public class UserDAOIMP implements UserDAO {
14
private static final String ISLOGINSQL = "select count(*) from t_user where username = ? and password = ?";
15
private static final String INSERTUSER = "insert into t_user(user , password) values(? , ?)";
16
public boolean isLogin(User user) {
17
boolean islogin = false;
18
ResultSet rs = null;
19
PreparedStatement pstm = null;
20
Connection conn = Factory.getMySQLCONN().getConnection();
21
try {
22
pstm = conn.prepareStatement(ISLOGINSQL);
23
pstm.setString(1, user.getUsername());
24
pstm.setString(2, user.getPassword());
25
rs = pstm.executeQuery();
26
if(rs.next()){
27
int i = rs.getInt(1);
28
if(i>0){
29
islogin = true;
30
}
31
}
32
} catch (SQLException e) {
33
throw new MyRuntimeException(e.getMessage(),e);
34
}finally{
35
close(rs, pstm, conn);
36
}
37
return islogin;
38
}
39
public void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
40
try {
41
if(rs!=null){
42
rs.close();
43
}
44
} catch (SQLException e) {
45
throw new MyRuntimeException(e.getMessage(),e);
46
}
47
48
try {
49
if(pstm!=null){
50
pstm.close();
51
}
52
} catch (SQLException e) {
53
throw new MyRuntimeException(e.getMessage(),e);
54
}
55
56
try {
57
if(conn!=null){
58
conn.close();
59
}
60
} catch (SQLException e) {
61
throw new MyRuntimeException(e.getMessage(),e);
62
}
63
}
64
public boolean RegUser(User user) {
65
Connection conn = Factory.getMySQLCONN().getConnection();
66
boolean isreg = false;
67
PreparedStatement pstm = null;
68
ResultSet rs = null;
69
try {
70
pstm = conn.prepareStatement(INSERTUSER);
71
pstm.setString(1, user.getUsername());
72
pstm.setString(2, user.getPassword());
73
int i = pstm.executeUpdate();
74
if(i > 0){
75
isreg = true;
76
}
77
} catch (SQLException e) {
78
throw new MyRuntimeException(e.getMessage(),e);
79
}finally{
80
close(rs, pstm, conn);
81
}
82
83
return isreg;
84
}
85
86
}

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
