質問編集履歴

4

.

2019/05/09 01:12

投稿

kokok
kokok

スコア145

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- package sample1;
15
+ 'package sample1;
16
16
 
17
17
 
18
18
 
@@ -188,4 +188,4 @@
188
188
 
189
189
  }
190
190
 
191
- }
191
+ }'

3

ブラウザに表示はできましたが、改行がうまくいきません。

2019/05/09 01:12

投稿

kokok
kokok

スコア145

test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,16 @@
1
- サーブレットからmysql に接続する場合、どこに接続の記述をするのでしょうか?
1
+ ```java
2
2
 
3
- クラスを定義して書くのでしょうか?
3
+ コード
4
+
5
+ ```
4
6
 
5
7
 
6
8
 
7
- ```java
9
+
10
+
11
+
12
+
13
+
8
14
 
9
15
  package sample1;
10
16
 
@@ -46,10 +52,6 @@
46
52
 
47
53
 
48
54
 
49
-
50
-
51
-
52
-
53
55
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
54
56
 
55
57
  throws ServletException, IOException {
@@ -58,9 +60,23 @@
58
60
 
59
61
  PrintWriter out = response.getWriter();
60
62
 
63
+
64
+
65
+
66
+
61
67
  out.println("<html>");
62
68
 
69
+
70
+
71
+
72
+
73
+
74
+
63
75
  out.println("<body>");
76
+
77
+
78
+
79
+
64
80
 
65
81
 
66
82
 
@@ -70,7 +86,7 @@
70
86
 
71
87
  String url ="jdbc:mysql://localhost/training";
72
88
 
73
- String user = "ryota";
89
+ String user = "root";
74
90
 
75
91
  String password ="ryota";
76
92
 
@@ -80,7 +96,7 @@
80
96
 
81
97
  Class.forName("com.mysql.jdbc.Driver").newInstance();
82
98
 
83
- conn = DriverManager.getConnection(url,user,password);
99
+ conn = DriverManager.getConnection(url, user, password);
84
100
 
85
101
 
86
102
 
@@ -94,11 +110,25 @@
94
110
 
95
111
  while(rs.next()){
96
112
 
113
+ ````````````ここに言語を入力
114
+
97
- int id = rs.getInt("id");
115
+ int id = rs.getInt("id");
98
116
 
99
117
  String name = rs.getString("name");
100
118
 
101
119
  int age = rs.getInt("age");
120
+
121
+ out.println(id + name + age );
122
+
123
+
124
+
125
+ out.println("\n");
126
+
127
+
128
+
129
+
130
+
131
+
102
132
 
103
133
  }
104
134
 
@@ -140,19 +170,13 @@
140
170
 
141
171
  }
142
172
 
173
+ //out.println("</html>");
143
174
 
175
+ out.println("</body>");
144
176
 
145
- out.println("<html>");
177
+ out.println("</html>");
146
-
147
- out.println("<body>");
148
-
149
-
150
178
 
151
179
  }
152
-
153
-
154
-
155
-
156
180
 
157
181
 
158
182
 
@@ -165,5 +189,3 @@
165
189
  }
166
190
 
167
191
  }
168
-
169
- ```

2

マークダウン<code>

2019/05/09 00:48

投稿

kokok
kokok

スコア145

test CHANGED
File without changes
test CHANGED
@@ -3,6 +3,8 @@
3
3
  クラスを定義して書くのでしょうか?
4
4
 
5
5
 
6
+
7
+ ```java
6
8
 
7
9
  package sample1;
8
10
 
@@ -163,3 +165,5 @@
163
165
  }
164
166
 
165
167
  }
168
+
169
+ ```

1

コードを書いてみましたが SQLException:Access denied for user 'ryota'@'localhost' (using password: YES) とエラーがでます

2019/05/08 08:45

投稿

kokok
kokok

スコア145

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,165 @@
1
1
  サーブレットからmysql に接続する場合、どこに接続の記述をするのでしょうか?
2
2
 
3
3
  クラスを定義して書くのでしょうか?
4
+
5
+
6
+
7
+ package sample1;
8
+
9
+
10
+
11
+ import java.io.IOException;
12
+
13
+ import java.io.PrintWriter;
14
+
15
+ import java.sql.Connection;
16
+
17
+ import java.sql.DriverManager;
18
+
19
+ import java.sql.ResultSet;
20
+
21
+ import java.sql.SQLException;
22
+
23
+ import java.sql.Statement;
24
+
25
+
26
+
27
+ import javax.servlet.ServletException;
28
+
29
+ import javax.servlet.annotation.WebServlet;
30
+
31
+ import javax.servlet.http.HttpServlet;
32
+
33
+ import javax.servlet.http.HttpServletRequest;
34
+
35
+ import javax.servlet.http.HttpServletResponse;
36
+
37
+
38
+
39
+ @WebServlet("/Hello")
40
+
41
+ public class Hello extends HttpServlet {
42
+
43
+ private static final long serialVersionUID = 1L;
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
52
+
53
+ throws ServletException, IOException {
54
+
55
+ response.setContentType("text/html; charset=utf-8");
56
+
57
+ PrintWriter out = response.getWriter();
58
+
59
+ out.println("<html>");
60
+
61
+ out.println("<body>");
62
+
63
+
64
+
65
+
66
+
67
+ Connection conn = null;
68
+
69
+ String url ="jdbc:mysql://localhost/training";
70
+
71
+ String user = "ryota";
72
+
73
+ String password ="ryota";
74
+
75
+
76
+
77
+ try{
78
+
79
+ Class.forName("com.mysql.jdbc.Driver").newInstance();
80
+
81
+ conn = DriverManager.getConnection(url,user,password);
82
+
83
+
84
+
85
+ Statement stmt =conn.createStatement();
86
+
87
+ String sql ="SELECT * FROM user";
88
+
89
+ ResultSet rs = stmt.executeQuery(sql);
90
+
91
+
92
+
93
+ while(rs.next()){
94
+
95
+ int id = rs.getInt("id");
96
+
97
+ String name = rs.getString("name");
98
+
99
+ int age = rs.getInt("age");
100
+
101
+ }
102
+
103
+
104
+
105
+ rs.close();
106
+
107
+ stmt.close();
108
+
109
+ }catch(ClassNotFoundException e){
110
+
111
+ out.println("lassNotFoundException:"+ e.getMessage());
112
+
113
+ }catch(SQLException e){
114
+
115
+ out.println("SQLException:" + e.getMessage());
116
+
117
+ }catch(Exception e){
118
+
119
+ out.println("Exception:" + e.getMessage()); }
120
+
121
+ finally{
122
+
123
+
124
+
125
+ try{
126
+
127
+ if(conn !=null){
128
+
129
+ conn.close();}
130
+
131
+ }catch (SQLException e){
132
+
133
+ out.println("SQLException:" + e.getMessage());
134
+
135
+ }
136
+
137
+
138
+
139
+ }
140
+
141
+
142
+
143
+ out.println("<html>");
144
+
145
+ out.println("<body>");
146
+
147
+
148
+
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
158
+
159
+ throws ServletException, IOException {
160
+
161
+ doGet(request, response);
162
+
163
+ }
164
+
165
+ }