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

質問編集履歴

1

追記

2018/10/07 08:59

投稿

takx
takx

スコア18

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,47 @@
1
1
  java で MYSQLサーバーに接続できない
2
2
 
3
- 一般的な接続確認のソースコードを実行しても接続できません。
3
+ 一般的な接続確認のソースコードを実行しても接続できません。connector/jを使用しています。
4
4
 
5
- 理由:java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
5
+ 理由:java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
6
+
7
+ 以下、ソースコードです。
8
+ package mysql;
9
+
10
+ import java.sql.Connection;
11
+ import java.sql.DriverManager;
12
+ import java.sql.SQLException;
13
+
14
+ public class Example1 {
15
+
16
+ public static void main(String[] args) {
17
+ // TODO 自動生成されたメソッド・スタブ
18
+ String server = "127.0.0.1";
19
+ String user = "nyuser";
20
+ String pass = "nypass";
21
+ String db = "nyumon";
22
+ String url = "jdbc:mysql://" + server + "/" + db + "?autoReconnect=true&useSSL=false";
23
+
24
+ Connection con = null;
25
+ try {
26
+ Class.forName("com.mysql.jdbc.Driver").newInstance();
27
+ con = DriverManager.getConnection(url, user, pass);
28
+ System.out.println("接続成功です!");
29
+ con.close();
30
+ }catch(SQLException e) {
31
+ System.err.println("接続失敗です\n理由:" + e.toString());
32
+ }catch(Exception e){
33
+ e.printStackTrace();
34
+ }finally {
35
+ try {
36
+ if (con != null) {
37
+ con.close();
38
+ con = null;
39
+ }
40
+ }catch(Exception e) {
41
+ e.printStackTrace();
42
+ }
43
+ }
44
+
45
+ }
46
+
47
+ }