來源:ChuckMa 發(fā)布時間:2018-11-21 14:15:04 閱讀量:1092
這是數(shù)據(jù)庫增刪改查的Java代碼,隨即寫下來!Java部分的代碼全部在這里了
第一個實體類Emp
public class Emp {
private String empName;
private int empId;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
第二個類BaseDao(公共部分),代碼如下:
public class BaseDao {
/**
*
* @param conn 連接對象
* @param sql sql 語句
* @param param 此數(shù)組,裝的是sql語句中的問題的替代數(shù)據(jù)
* @throws Exception
*/
public void exeSql(Connection conn, String sql, Object[] param)
throws Exception {
PreparedStatement pst = null;
pst = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
}
pst.executeUpdate();
}
/**
* 得到查詢結(jié)果集
* @param conn
* @param sql
* @param param
* @return 返回結(jié)果集rs
* @throws Exception
*/
public ResultSet getRs(Connection conn, String sql, Object[] param)
throws Exception {
PreparedStatement pst = null;
pst = conn.prepareStatement(sql);
ResultSet rs = null;
if (param != null) {
for (int i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
}
rs = pst.executeQuery();
return rs;
}
}
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
第三個類EmpDao ,代碼如下:
public class EmpDao extends BaseDao{
/**
* 建表
* @param conn
* @throws Exception
*/
public void createTable(Connection conn)throws Exception{
String sql = "create table emp (empId tinyint primary key,empName varchar(20))";
Object [] param = null;
this.exeSql(conn,sql,param);//???
}
/**
* 數(shù)據(jù)添加
* @param conn
* @param emp
* @throws Exception
*/
public void addEmp(Connection conn,Emp emp) throws Exception{
String sql = "insert into emp(empId,empName) values(?,?)";
Object [] param = {emp.getEmpId(),emp.getEmpName()};
this.exeSql(conn,sql,param);
}
/**
* 數(shù)據(jù)刪除
* @param conn
* @param empId
* @throws Exception
*/
public void deleteEmpById(Connection conn,int empId)throws Exception{
String sql= "delete from emp where empId=?";
Object [] param = {empId};
this.exeSql(conn,sql,param);
}
/**
* 數(shù)據(jù)更新
* @param conn
* @param emp
* @throws Exception
*/
public void updateEmp(Connection conn,Emp emp)throws Exception{
String sql ="update emp set empName=? where empId=?";
Object []param ={emp.getEmpName(),emp.getEmpId()};
this.exeSql(conn,sql,param);
}
/**
* 數(shù)據(jù)查詢
* @param conn
* @return
* @throws Exception
*/
public ArrayList<Emp> findAllEmps(Connection conn)throws Exception{
String sql ="select *from emp";
Object [] param = null;
ResultSet rs = this.getRs(conn,sql,param);
ArrayList<Emp> empList = new ArrayList<Emp>();
while(rs.next()){
Emp emp = new Emp();
emp.setEmpId(rs.getInt("empId"));
emp.setEmpName(rs.getString("empName"));
empList.add(emp);
}
return empList;
}
}
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
第四個類 工具類DbHelper ,代碼如下:
/**
* 連接和關(guān)閉
* @author Administrator
* static關(guān)鍵字,標(biāo)識為可以用類名直接調(diào)用類的屬性或方法。
*/
public class DbHelper {
public static Connection getConn()throws Exception{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","1234567");
return conn;
}
public static void closeConn(Connection conn)throws Exception{
if(conn!=null){
conn.close();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
第五個類 Service 服務(wù)層,代碼如下:
public class EmpService {
/**
* 建表服務(wù)
*/
public void create() {
Connection conn = null;
try {
conn = DbHelper.getConn();
conn.setAutoCommit(false); //設(shè)置手動提交
EmpDao empDao = new EmpDao();
empDao.createTable(conn); //???
conn.commit(); //提交
DbHelper.closeConn(conn);
System.out.println("表創(chuàng)建成功");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 添加
* @param emp
*/
public void add(Emp emp) {
Connection conn = null;
try {
conn = DbHelper.getConn();
conn.setAutoCommit(false);
EmpDao empDao = new EmpDao();
empDao.addEmp(conn, emp);
conn.commit();
DbHelper.closeConn(conn);
System.out.println("數(shù)據(jù)添加成功");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 刪除
* @param empId
*/
public void delete(int empId) {
Connection conn = null;
try {
conn = DbHelper.getConn();
conn.setAutoCommit(false);
EmpDao empDao = new EmpDao();
empDao.deleteEmpById(conn, empId);
conn.commit();
DbHelper.closeConn(conn);
System.out.println("數(shù)據(jù)刪除成功");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 修改
* @param emp
*/
public void update(Emp emp) {
Connection conn = null;
try {
conn = DbHelper.getConn();
conn.setAutoCommit(false);
EmpDao empDao = new EmpDao();
empDao.updateEmp(conn, emp);
conn.commit();
DbHelper.closeConn(conn);
System.out.println("數(shù)據(jù)更新成功");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 查詢
* @return
*/
public ArrayList<Emp> findAllEmps() {
Connection conn = null;
ArrayList<Emp> empList = null;
try {
conn = DbHelper.getConn();
conn.setAutoCommit(false);
EmpDao empDao = new EmpDao();
empList = empDao.findAllEmps(conn);
conn.commit();
DbHelper.closeConn(conn);
System.out.println("數(shù)據(jù)查詢完畢!");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return empList;
}
}
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
第六個類 測試類 Test ,代碼如下:
public class EmpTest {
public static void main(String[] args) {
Emp emp = new Emp();
emp.setEmpId(1);
emp.setEmpName("阿童木");
EmpService es = new EmpService();
// es.create(); //???
// es.add(emp);
// es.delete(66);
es.update(emp);
ArrayList<Emp> empList = es.findAllEmps();
for (int i = 0; i < empList.size(); i++) {
Emp emp2 = empList.get(i);
System.out.println("姓名:" + emp2.getEmpName() + " 序號:"
+ emp2.getEmpId());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
本次使用的數(shù)據(jù)庫是mysql數(shù)據(jù)庫!不足之處希望各位指出,我們共同學(xué)習(xí),總結(jié),進(jìn)步!
---------------------