質問編集履歴
1
Daoのコードを掲載しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,9 +9,89 @@
|
|
9
9
|
JavaBeans
|
10
10
|
→データを格納する
|
11
11
|
|
12
|
+
```java
|
13
|
+
package work;
|
12
14
|
|
15
|
+
import java.sql.Connection;
|
16
|
+
import java.sql.DriverManager;
|
17
|
+
import java.sql.ResultSet;
|
18
|
+
import java.sql.Statement;
|
13
19
|
|
20
|
+
public class Dao {
|
14
21
|
|
22
|
+
private String driver;
|
23
|
+
private String url;
|
24
|
+
private String user;
|
25
|
+
private String password;
|
26
|
+
private Connection connection;
|
27
|
+
private Statement statement;
|
28
|
+
private ResultSet resultset;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* コンストラクタ
|
32
|
+
* @param driver ドライバー
|
33
|
+
* @param url URL
|
34
|
+
* @param user ユーザー名
|
35
|
+
* @param password パスワード
|
36
|
+
*/
|
37
|
+
public Dao(String driver, String url, String user, String password) {
|
38
|
+
this.driver = driver;
|
39
|
+
this.url = url;
|
40
|
+
this.user = user;
|
41
|
+
this.password = password;
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* 引数なしのコンストラクタ
|
46
|
+
* 既定値を使用する
|
47
|
+
*/
|
48
|
+
public Dao() {
|
49
|
+
driver = "ドライバー";
|
50
|
+
url = "jdbc:mariadb://localhost:3306/DB名";
|
51
|
+
user = "ユーザ名";
|
52
|
+
password = "パスワード";
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* データベースへの接続を行う
|
57
|
+
*/
|
58
|
+
public synchronized void open() throws Exception {
|
59
|
+
Class.forName(driver);
|
60
|
+
connection = DriverManager.getConnection(url, user, password);
|
61
|
+
statement = connection.createStatement();
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* SQL 文を実行した結果の ResultSet を返す
|
66
|
+
* @param sql SQL 文
|
67
|
+
*/
|
68
|
+
public ResultSet getResultSet(String sql) throws Exception {
|
69
|
+
if ( statement.execute(sql) ) {
|
70
|
+
return statement.getResultSet();
|
71
|
+
}
|
72
|
+
return null;
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* SQL 文の実行
|
77
|
+
* @param sql SQL 文
|
78
|
+
*/
|
79
|
+
public void execute(String sql) throws Exception {
|
80
|
+
statement.execute(sql);
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* データベースへのコネクションのクローズ
|
85
|
+
*/
|
86
|
+
public synchronized void close() throws Exception {
|
87
|
+
if ( resultset != null ) resultset.close();
|
88
|
+
if ( statement != null ) statement.close();
|
89
|
+
if ( connection != null ) connection.close();
|
90
|
+
}
|
91
|
+
}
|
92
|
+
```
|
93
|
+
|
94
|
+
|
15
95
|
`SubDaoクラスの書き方について質問です。`
|
16
96
|
自分の中で、Daoはservletの中身をDB接続とそれ以外に切り離したものだと考えてます。
|
17
97
|
|