-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartmentDAO.java
More file actions
144 lines (138 loc) · 4 KB
/
DepartmentDAO.java
File metadata and controls
144 lines (138 loc) · 4 KB
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
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ConnectionDatabase.DatabaseConnect;
import beans.Department;
public class DepartmentDAO {
//データベース接続パスの取得
DatabaseConnect connect=new DatabaseConnect();
//部署リスト取得
private List<Department>selectDepartment(int id){
//返すリストの宣言
List<Department>list=new ArrayList<>();
//コネクション宣言
Connection con=null;
try {
Class.forName("org.h2.Driver");
//コネクションの確立
con=DriverManager.getConnection(connect.getJDBC_URL(),
connect.getJDBC_USER(), connect.getJDBC_PASS());
//sql文の設定
String sql="SELECT * FROM DEPART";
//編集の場合
if(id!=0) {
sql+=" WHERE ID='"+id+"'";
}
sql+=" ORDER BY ID ASC";
//データベースからリストを取得する
PreparedStatement ps=con.prepareStatement(sql);
//結果の取得
ResultSet rs=ps.executeQuery();
while(rs.next()) {
Department depart=new Department();
depart.setId(rs.getInt("ID"));
depart.setName(rs.getString("NAME"));
//リストに格納する
list.add(depart);
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}finally {
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
return list;
}
//すべての部署を返すメソッド
public List<Department>allDepartment(){
return this.selectDepartment(0);
}
//idで指定した部署を渡すメソッド
public Department getDepartment(int id) {
List<Department>list=this.selectDepartment(id);
return list.get(0);
}
//部署情報を新規登録、編集するメソッド
public boolean addEdit(Department depart,boolean addOrEdit) {
//返すboolean型
boolean trueFalse=false;
try {
//H2データベースの指定
Class.forName("org.h2.Driver");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
//SQL文の生成
String sql=null;
if(addOrEdit==true) {
sql="INSERT INTO DEPART (NAME,ID) VALUES (?,?)";
}else {
sql="UPDATE DEPART SET NAME=? WHERE ID=?";
}
try {
//コネクションを生成
Connection conn=DriverManager.getConnection(connect.getJDBC_URL(),
connect.getJDBC_USER(), connect.getJDBC_PASS());
//preparedStatementにSQL文をセット
PreparedStatement ps=conn.prepareStatement(sql);
//?に名前を代入する
ps.setString(1, depart.getName());
//?にid代入する
ps.setInt(2, depart.getId());
//実行
int trueOrFalse=ps.executeUpdate();
if(trueOrFalse!=0) {
trueFalse=true;
}
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return trueFalse;
}
//部署情報を削除するメソッド
public boolean delete(int id) {
//返すboolean型
boolean trueFalse=false;
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
}
//SQL文の生成
String sql="DELETE FROM DEPART WHERE ID=?";
try {
//コネクション確立
Connection conn=DriverManager.getConnection(connect.getJDBC_URL(),
connect.getJDBC_USER(), connect.getJDBC_PASS());
//sql文をセット
PreparedStatement ps=conn.prepareStatement(sql);
//?にidを代入する
ps.setInt(1, id);
//更新
int trueOrFalse=ps.executeUpdate();
if(trueOrFalse!=0) {
trueFalse=true;
}
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return trueFalse;
}
}