質問するログイン新規登録

回答編集履歴

2

インデントそろえてみた

2019/08/29 05:42

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -13,4 +13,144 @@
13
13
 
14
14
  インデントがごっちゃごちゃだと起こるミスです。
15
15
  インデントをしっかり意識してコーディングするようにしましょう。
16
- Eclipseを使っているなら自動フォーマットもありますし。
16
+ Eclipseを使っているなら自動フォーマットもありますし。
17
+
18
+
19
+ **インデントをそろえてみた。**
20
+
21
+ ```Java
22
+ package dateAll;
23
+
24
+ import java.io.IOException;
25
+ import java.sql.Connection;
26
+ import java.sql.PreparedStatement;
27
+ import java.sql.ResultSet;
28
+ import java.sql.SQLException;
29
+
30
+ import javax.servlet.ServletException;
31
+ import javax.servlet.annotation.WebServlet;
32
+ import javax.servlet.http.HttpServlet;
33
+ import javax.servlet.http.HttpServletRequest;
34
+ import javax.servlet.http.HttpServletResponse;
35
+ /**
36
+ * Servlet implementation class ProcessServ
37
+ */
38
+ @WebServlet("/ProcessServ")
39
+ public class ProcessServ extends HttpServlet {
40
+ private static final long serialVersionUID = 1L;
41
+
42
+ /**
43
+ * @see HttpServlet#HttpServlet()
44
+ */
45
+ public ProcessServ() {
46
+ super();
47
+ // TODO Auto-generated constructor stub
48
+ }
49
+
50
+ /**
51
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
52
+ */
53
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
54
+ // TODO Auto-generated method stub
55
+ System.out.println("doGetで処理をしてしまっている");
56
+ }
57
+
58
+ /**
59
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
60
+ */
61
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62
+
63
+ //データベースへ接続するためのコード
64
+ //PostgreSQLへの接続情報
65
+ Connection con ;
66
+ PreparedStatement st;
67
+ ResultSet rs;
68
+
69
+ String url = "jdbc:postgresql://localhost:5432/database";
70
+ String user = "postgres";
71
+ String password = "password";
72
+
73
+ //色々な変数を定義、初期化
74
+ int id ;
75
+ String questionanswer ;
76
+ int finalid ;
77
+
78
+ int ansid1;
79
+ int ansid2;
80
+ int ansid3;
81
+
82
+ String a1;
83
+ String a2;
84
+ String a3;
85
+
86
+ //JDBC driver setting
87
+ //Class.forName("org.postgresql.Driver");
88
+
89
+ //PostgreSQLへの接続
90
+
91
+
92
+ //SQLを発行
93
+ try {
94
+ st = con.prepareStatement("select id,questionanswer,finalid from question where parentid = ?");
95
+
96
+ //ProcessPagejspからpostされた値をStringからintに変換し、変数に代入
97
+ int post = Integer.parseInt(request.getParameter("postnum"));
98
+
99
+ //SQ発行文の?部分を決定し、抽出されたデータをrsに代入
100
+ st.setInt(1, post);
101
+ rs = st.executeQuery();
102
+
103
+ //rsに保存された次の質問文のデータを、それぞれidと文章に分けて変数に代入
104
+ while (rs.next()) {
105
+ id = rs.getInt("id");
106
+ questionanswer = rs.getString("questionanswer");
107
+ finalid = rs.getInt("finalid");
108
+ }
109
+
110
+ //finalidがnullの場合とそうでない場合に分岐させる
111
+ if(finalid != 0) {
112
+ st = con.prepareStatement("select meigentext,humanname,bookname from idconn left outer join meigen on meigenid");
113
+
114
+ }else {
115
+ //質問文に値をセットする
116
+ request.setAttribute("question", questionanswer);
117
+ request.setAttribute("id", id);
118
+
119
+ //対応する回答文およびidの取り出し、変数への代入
120
+ st.setInt(1, id);
121
+ rs = st.executeQuery();
122
+ while (rs.next()) {
123
+ ansid1 = rs.getInt("id");
124
+ a1 = rs.getString("questionanswer");
125
+
126
+ ansid2 = rs.getInt("id");
127
+ a2 = rs.getString("questionanswer");
128
+
129
+ ansid3 = rs.getInt("id");
130
+ a3 = rs.getString("questionanswer");
131
+ }
132
+
133
+ //回答選択肢自身のidをセットする
134
+ request.setAttribute("ans1", ansid1);
135
+ request.setAttribute("ans1", ansid2);
136
+ request.setAttribute("ans1", ansid3);
137
+
138
+ //回答選択肢自身の質問文をセットする
139
+ request.setAttribute("a1", a1);
140
+ request.setAttribute("a2", a2);
141
+ request.setAttribute("a3", a3);
142
+
143
+ }catch (SQLException e) {
144
+ // TODO 自動生成された catch ブロック
145
+ e.printStackTrace();
146
+ }finally {
147
+ con.close(); // クローズ処理
148
+ }
149
+ //if,elseを閉じる
150
+ }
151
+ //void doPostを閉じる
152
+ }
153
+ //classを閉じる
154
+ }
155
+
156
+ ```

1

2019/08/29 05:42

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -9,4 +9,8 @@
9
9
  }
10
10
  //if,elseを閉じる
11
11
  }
12
- ```
12
+ ```
13
+
14
+ インデントがごっちゃごちゃだと起こるミスです。
15
+ インデントをしっかり意識してコーディングするようにしましょう。
16
+ Eclipseを使っているなら自動フォーマットもありますし。