質問編集履歴

3

コードを追加

2021/10/05 05:27

投稿

retantaro
retantaro

スコア15

test CHANGED
File without changes
test CHANGED
@@ -132,8 +132,4 @@
132
132
 
133
133
 
134
134
 
135
- connectionの文字列は一応伏せています。
136
-
137
-
138
-
139
135
  解決方法があればお願いします。

2

コードを追加

2021/10/05 05:27

投稿

retantaro
retantaro

スコア15

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,119 @@
1
+ ```Java
2
+
3
+ import java.sql.Connection;
4
+
5
+ import java.sql.DriverManager;
6
+
7
+ import java.sql.PreparedStatement;
8
+
9
+ import java.sql.ResultSet;
10
+
11
+ import java.sql.SQLException;
12
+
13
+ import java.util.ArrayList;
14
+
15
+ import java.io.IOException;
16
+
17
+ import java.sql.Statement;
18
+
19
+
20
+
21
+ public class DBExecQuery {
22
+
23
+ public static void main(String[] args) {
24
+
25
+
26
+
27
+ if (args.length!=0) {
28
+
29
+ int linePopulation = Integer.parseInt(args[0]);
30
+
31
+ System.out.println(linePopulation);
32
+
33
+
34
+
35
+ try(
36
+
37
+ // DB接続
38
+
39
+ Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//172.16.4.6:1521/infopdb", "uenoa", "uenoa");
40
+
41
+ // ユーザー名、パスワード
42
+
43
+
44
+
45
+ // SQL実行用オブジェクトの作成
46
+
47
+ Statement stmt = con.createStatement();
48
+
49
+ ){
50
+
51
+ // SQL実行と結果格納
52
+
53
+ ResultSet rs =
54
+
55
+ stmt.executeQuery("select 県名, 人口 from REVENGE where 人口 = 812056");
56
+
57
+
58
+
59
+ while(rs.next()) {
60
+
61
+ String name = rs.getString("県名");
62
+
63
+ int population = rs.getInt("人口");
64
+
65
+
66
+
67
+ System.out.println("県名 " + name + " : 人口 " + population);
68
+
69
+ }
70
+
71
+ //PreparedStatement pStmt = con.prepareStatement(sql);
72
+
73
+ //pStmt.setInt(1, userId);
74
+
75
+ //ResultSet rs = pStmt.executeQuery();
76
+
77
+
78
+
79
+ // コミット
80
+
81
+ try {
82
+
83
+ con.commit();
84
+
85
+ }
86
+
87
+ catch(SQLException ex) {
88
+
89
+ // コミットに失敗したら全作業をなかったことにする
90
+
91
+ con.rollback();
92
+
93
+ }
94
+
95
+ }
96
+
97
+ catch(SQLException ex) {
98
+
99
+ ex.printStackTrace();
100
+
101
+ }
102
+
103
+ }
104
+
105
+ else {
106
+
107
+ System.out.println("コマンドライン引数を指定してください");
108
+
109
+ }
110
+
111
+ }
112
+
113
+ }
114
+
115
+ ```
116
+
1
117
  sqlを使ったjavaプログラムを実行したいのですが、コマンドプロンプトで
2
118
 
3
119
  ①javac Main.java
@@ -16,27 +132,7 @@
16
132
 
17
133
 
18
134
 
19
- ### ソースコード
20
-
21
- public class Main {
22
-
23
- public static void main(String[] args) {
24
-
25
-
26
-
27
- if (args.length!=0) {
28
-
29
- int linePopulation = Integer.parseInt(args[0]);
30
-
31
- System.out.println(linePopulation);
135
+ connectionの文字列は一応伏せています。
32
-
33
-
34
-
35
- try(
36
-
37
- // DB接続
38
-
39
- Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//111.11.1.1:1111/---", "名前", "パス");
40
136
 
41
137
 
42
138
 

1

コードを記載し、エラー内容をきじゅつ

2021/10/05 05:26

投稿

retantaro
retantaro

スコア15

test CHANGED
File without changes
test CHANGED
@@ -12,4 +12,32 @@
12
12
 
13
13
 
14
14
 
15
+ エラーの内容はjava.sql.SQLException: No suitable driver foundです。
16
+
17
+
18
+
19
+ ### ソースコード
20
+
21
+ public class Main {
22
+
23
+ public static void main(String[] args) {
24
+
25
+
26
+
27
+ if (args.length!=0) {
28
+
29
+ int linePopulation = Integer.parseInt(args[0]);
30
+
31
+ System.out.println(linePopulation);
32
+
33
+
34
+
35
+ try(
36
+
37
+ // DB接続
38
+
39
+ Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//111.11.1.1:1111/---", "名前", "パス");
40
+
41
+
42
+
15
43
  解決方法があればお願いします。