質問編集履歴
1
サーブレットのコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,33 +18,139 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
|
21
|
+
```java
|
22
22
|
|
23
|
-
|
23
|
+
package xxxx;
|
24
24
|
|
25
|
-
```xmlコード
|
26
25
|
|
27
|
-
<Context reloadable="true">
|
28
26
|
|
29
|
-
|
27
|
+
import java.io.*;
|
30
28
|
|
31
|
-
|
29
|
+
import javax.servlet.*;
|
32
30
|
|
33
|
-
|
31
|
+
import javax.servlet.http.*;
|
34
32
|
|
35
|
-
|
33
|
+
import java.sql.*;
|
36
34
|
|
37
|
-
|
35
|
+
import java.io.IOException;
|
38
36
|
|
39
|
-
|
37
|
+
import java.io.PrintWriter;
|
40
38
|
|
41
|
-
|
39
|
+
import javax.servlet.ServletException;
|
42
40
|
|
43
|
-
|
41
|
+
import javax.servlet.http.HttpServlet;
|
44
42
|
|
45
|
-
|
43
|
+
import javax.servlet.http.HttpServletRequest;
|
46
44
|
|
45
|
+
import javax.servlet.http.HttpServletResponse;
|
46
|
+
|
47
|
+
import javax.servlet.annotation.WebServlet;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
@WebServlet(urlPatterns= {"/xxxx/hello2"})
|
52
|
+
|
53
|
+
public class Hello2 extends HttpServlet {
|
54
|
+
|
55
|
+
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
56
|
+
|
57
|
+
throws IOException, ServletException{
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
response.setContentType("text/html; charset=UTF-8");
|
62
|
+
|
63
|
+
PrintWriter out = response.getWriter();
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
out.println("<html>");
|
68
|
+
|
69
|
+
out.println("<head>");
|
70
|
+
|
71
|
+
out.println("<title>データベーステスト</title>");
|
72
|
+
|
73
|
+
out.println("</head>");
|
74
|
+
|
75
|
+
out.println("<body>");
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
out.println("<p>");
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
Connection conn = null;
|
84
|
+
|
85
|
+
String url = "jdbc:mysql://[サーバーのアドレス]/test";
|
86
|
+
|
87
|
+
String user = "***";
|
88
|
+
|
89
|
+
String password = "***";
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
try {
|
94
|
+
|
95
|
+
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
96
|
+
|
97
|
+
out.println("ドライバのロードに成功しました<br>");
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
conn = DriverManager.getConnection(url, user, password);
|
102
|
+
|
103
|
+
out.println("データベース接続に成功しました<br>");
|
104
|
+
|
105
|
+
}catch (ClassNotFoundException e){
|
106
|
+
|
107
|
+
out.println("ClassNotFoundException:" + e.getMessage());
|
108
|
+
|
109
|
+
}catch (SQLException e){
|
110
|
+
|
111
|
+
out.println("SQLException:" + e.getMessage());
|
112
|
+
|
113
|
+
}catch (Exception e){
|
114
|
+
|
115
|
+
out.println("Exception:" + e.getMessage());
|
116
|
+
|
117
|
+
}finally{
|
118
|
+
|
119
|
+
try{
|
120
|
+
|
121
|
+
if (conn != null){
|
122
|
+
|
123
|
+
conn.close();
|
124
|
+
|
125
|
+
out.println("データベース切断に成功しました");
|
126
|
+
|
127
|
+
}else{
|
128
|
+
|
129
|
+
out.println("コネクションがありません");
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}catch (SQLException e){
|
134
|
+
|
135
|
+
out.println("SQLException:" + e.getMessage());
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
out.println("</p>");
|
144
|
+
|
145
|
+
|
146
|
+
|
47
|
-
</
|
147
|
+
out.println("</body>");
|
148
|
+
|
149
|
+
out.println("</html>");
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
}
|
48
154
|
|
49
155
|
```
|
50
156
|
|