2016-12-21 237 views
0

我正在嘗試使用JDBC連接到一個新的數據庫,我正在處理這個項目,這將允許我輸出體育比賽的整個負載統計數據。但是我在嘗試連接數據庫時遇到了一些麻煩。我在下面收到以下錯誤。使用JDBC連接到MYSQL數據庫

com.mysql.jdbc.CommunicationsException: Communications link failure due  to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException 
MESSAGE: Connection refused 

STACKTRACE: 

java.net.ConnectException: Connection refused 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
at java.net.Socket.connect(Socket.java:589) 
at java.net.Socket.connect(Socket.java:538) 
at java.net.Socket.<init>(Socket.java:434) 
at java.net.Socket.<init>(Socket.java:244) 
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) 
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271) 
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) 
at com.mysql.jdbc.Connection.<init>(Connection.java:1555) 
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:247) 
at JDBC.main(JDBC.java:22) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 


** END NESTED EXCEPTION ** 



Last packet sent to the server was 0 ms ago. 
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847) 
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:247) 
at JDBC.main(JDBC.java:22) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

是從我的代碼作爲跟隨產生此錯誤:

//STEP 1. Import required packages 
import java.sql.*; 

public class JDBC { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost/"; 

    // Database credentials 
    static final String USER = "username"; 
    static final String PASS = "password"; 

    public static void main(String[] args) { 
     Connection conn = null; 
     Statement stmt = null; 
     try{ 
      //STEP 2: Register JDBC driver 
      Class.forName("com.mysql.jdbc.Driver"); 

      //STEP 3: Open a connection 
      System.out.println("Connecting to database..."); 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 

      //STEP 4: Execute a query 
      System.out.println("Creating database..."); 
      stmt = conn.createStatement(); 

      String sql = "CREATE DATABASE PREDICTOR"; 
      stmt.executeUpdate(sql); 
      System.out.println("Database created successfully..."); 
     }catch(SQLException se){ 
      //Handle errors for JDBC 
      se.printStackTrace(); 
     }catch(Exception e){ 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     }finally{ 
      //finally block used to close resources 
      try{ 
       if(stmt!=null) 
        stmt.close(); 
      }catch(SQLException se2){ 
      }// nothing we can do 
      try{ 
       if(conn!=null) 
        conn.close(); 
      }catch(SQLException se){ 
       se.printStackTrace(); 
      }//end finally try 
     }//end try 
     System.out.println("Goodbye!"); 
    }//end main 
} 

我有禁用我的防火牆,並期待在以前的帖子在堆棧溢出試圖修改我的問題,但很可惜的連接被拒絕。任何幫助讚賞。

+0

拒絕連接是非常強的自我解釋。服務器沒有運行,或者沒有在該主機上偵聽:port – e4c5

+2

[將Java連接到MySQL數據庫]的可能重複(http://stackoverflow.com/questions/2839321/connect-java-toa-a- MySQL的數據庫) –

回答

0

連接被拒絕

這意味着到服務器的TCP通信積極拒絕 - 這是在TCP層完成,連接從未到您的服務 - 你的代碼是不相關的,在這點。

這可以通過幾個不同的問題引起:

  • 的SQL服務器沒有實際運行
  • 沒有安裝防火牆阻斷連接
  • 您正在連接到不同的端口服務器正在監聽。
0

我認爲你的SQL服務器沒有運行。 請檢查服務器狀態。 或者您應該重新安裝服務器。它可以解決你的問題。

0

您的DB_URL不正確。應該

DB_URL = "jdbc:mysql://localhost/databaseName"; 

,或者您也可以使用端口號在您的網址,如果數據庫服務器不使用默認端口

DB_URL = "jdbc:mysql://localhost:portNumber/databaseName";  

有關詳細信息,請訪問this site