質問編集履歴
4
.
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
|
8
|
-
package sample1;
|
8
|
+
'package sample1;
|
9
9
|
|
10
10
|
import java.io.IOException;
|
11
11
|
import java.io.PrintWriter;
|
@@ -93,4 +93,4 @@
|
|
93
93
|
throws ServletException, IOException {
|
94
94
|
doGet(request, response);
|
95
95
|
}
|
96
|
-
}
|
96
|
+
}'
|
3
ブラウザに表示はできましたが、改行がうまくいきません。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
サーブレットからmysql に接続する場合、どこに接続の記述をするのでしょうか?
|
2
|
-
|
1
|
+
```java
|
2
|
+
コード
|
3
|
+
```
|
3
4
|
|
4
|
-
|
5
|
+
|
6
|
+
|
7
|
+
|
5
8
|
package sample1;
|
6
9
|
|
7
10
|
import java.io.IOException;
|
@@ -22,33 +25,45 @@
|
|
22
25
|
public class Hello extends HttpServlet {
|
23
26
|
private static final long serialVersionUID = 1L;
|
24
27
|
|
25
|
-
|
26
|
-
|
27
28
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
28
29
|
throws ServletException, IOException {
|
29
30
|
response.setContentType("text/html; charset=utf-8");
|
30
31
|
PrintWriter out = response.getWriter();
|
32
|
+
|
33
|
+
|
31
34
|
out.println("<html>");
|
35
|
+
|
36
|
+
|
37
|
+
|
32
38
|
out.println("<body>");
|
33
39
|
|
34
40
|
|
41
|
+
|
42
|
+
|
35
43
|
Connection conn = null;
|
36
44
|
String url ="jdbc:mysql://localhost/training";
|
37
|
-
String user = "
|
45
|
+
String user = "root";
|
38
46
|
String password ="ryota";
|
39
47
|
|
40
48
|
try{
|
41
49
|
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
42
|
-
conn = DriverManager.getConnection(url,user,password);
|
50
|
+
conn = DriverManager.getConnection(url, user, password);
|
43
51
|
|
44
52
|
Statement stmt =conn.createStatement();
|
45
53
|
String sql ="SELECT * FROM user";
|
46
54
|
ResultSet rs = stmt.executeQuery(sql);
|
47
55
|
|
48
56
|
while(rs.next()){
|
57
|
+
````````````ここに言語を入力
|
49
|
-
|
58
|
+
int id = rs.getInt("id");
|
50
59
|
String name = rs.getString("name");
|
51
60
|
int age = rs.getInt("age");
|
61
|
+
out.println(id + name + age );
|
62
|
+
|
63
|
+
out.println("\n");
|
64
|
+
|
65
|
+
|
66
|
+
|
52
67
|
}
|
53
68
|
|
54
69
|
rs.close();
|
@@ -69,17 +84,13 @@
|
|
69
84
|
}
|
70
85
|
|
71
86
|
}
|
72
|
-
|
87
|
+
//out.println("</html>");
|
88
|
+
out.println("</body>");
|
73
|
-
|
89
|
+
out.println("</html>");
|
74
|
-
out.println("<body>");
|
75
|
-
|
76
90
|
}
|
77
91
|
|
78
|
-
|
79
|
-
|
80
92
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
81
93
|
throws ServletException, IOException {
|
82
94
|
doGet(request, response);
|
83
95
|
}
|
84
|
-
}
|
96
|
+
}
|
85
|
-
```
|
2
マークダウン<code>
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
サーブレットからmysql に接続する場合、どこに接続の記述をするのでしょうか?
|
2
2
|
クラスを定義して書くのでしょうか?
|
3
3
|
|
4
|
+
```java
|
4
5
|
package sample1;
|
5
6
|
|
6
7
|
import java.io.IOException;
|
@@ -80,4 +81,5 @@
|
|
80
81
|
throws ServletException, IOException {
|
81
82
|
doGet(request, response);
|
82
83
|
}
|
83
|
-
}
|
84
|
+
}
|
85
|
+
```
|
1
コードを書いてみましたが SQLException:Access denied for user 'ryota'@'localhost' (using password: YES) とエラーがでます
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,2 +1,83 @@
|
|
1
1
|
サーブレットからmysql に接続する場合、どこに接続の記述をするのでしょうか?
|
2
|
-
クラスを定義して書くのでしょうか?
|
2
|
+
クラスを定義して書くのでしょうか?
|
3
|
+
|
4
|
+
package sample1;
|
5
|
+
|
6
|
+
import java.io.IOException;
|
7
|
+
import java.io.PrintWriter;
|
8
|
+
import java.sql.Connection;
|
9
|
+
import java.sql.DriverManager;
|
10
|
+
import java.sql.ResultSet;
|
11
|
+
import java.sql.SQLException;
|
12
|
+
import java.sql.Statement;
|
13
|
+
|
14
|
+
import javax.servlet.ServletException;
|
15
|
+
import javax.servlet.annotation.WebServlet;
|
16
|
+
import javax.servlet.http.HttpServlet;
|
17
|
+
import javax.servlet.http.HttpServletRequest;
|
18
|
+
import javax.servlet.http.HttpServletResponse;
|
19
|
+
|
20
|
+
@WebServlet("/Hello")
|
21
|
+
public class Hello extends HttpServlet {
|
22
|
+
private static final long serialVersionUID = 1L;
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
27
|
+
throws ServletException, IOException {
|
28
|
+
response.setContentType("text/html; charset=utf-8");
|
29
|
+
PrintWriter out = response.getWriter();
|
30
|
+
out.println("<html>");
|
31
|
+
out.println("<body>");
|
32
|
+
|
33
|
+
|
34
|
+
Connection conn = null;
|
35
|
+
String url ="jdbc:mysql://localhost/training";
|
36
|
+
String user = "ryota";
|
37
|
+
String password ="ryota";
|
38
|
+
|
39
|
+
try{
|
40
|
+
Class.forName("com.mysql.jdbc.Driver").newInstance();
|
41
|
+
conn = DriverManager.getConnection(url,user,password);
|
42
|
+
|
43
|
+
Statement stmt =conn.createStatement();
|
44
|
+
String sql ="SELECT * FROM user";
|
45
|
+
ResultSet rs = stmt.executeQuery(sql);
|
46
|
+
|
47
|
+
while(rs.next()){
|
48
|
+
int id = rs.getInt("id");
|
49
|
+
String name = rs.getString("name");
|
50
|
+
int age = rs.getInt("age");
|
51
|
+
}
|
52
|
+
|
53
|
+
rs.close();
|
54
|
+
stmt.close();
|
55
|
+
}catch(ClassNotFoundException e){
|
56
|
+
out.println("lassNotFoundException:"+ e.getMessage());
|
57
|
+
}catch(SQLException e){
|
58
|
+
out.println("SQLException:" + e.getMessage());
|
59
|
+
}catch(Exception e){
|
60
|
+
out.println("Exception:" + e.getMessage()); }
|
61
|
+
finally{
|
62
|
+
|
63
|
+
try{
|
64
|
+
if(conn !=null){
|
65
|
+
conn.close();}
|
66
|
+
}catch (SQLException e){
|
67
|
+
out.println("SQLException:" + e.getMessage());
|
68
|
+
}
|
69
|
+
|
70
|
+
}
|
71
|
+
|
72
|
+
out.println("<html>");
|
73
|
+
out.println("<body>");
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
80
|
+
throws ServletException, IOException {
|
81
|
+
doGet(request, response);
|
82
|
+
}
|
83
|
+
}
|