2014-04-06 362 views
0

我收到以下錯誤消息。檢查很多解決方案,但沒有得到結果。無法連接到SOCKS代理:連接被拒絕:連接

 
Not Connected to the database - networkcoding 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
ERROR: java.lang.NullPointerException 
The last packet sent successfully to the server was 0 milliseconds ago. 
The driver has not received any packets from the server.

以下是數據庫連接代碼。

public class DBConnect { 
    // System.out.println("MySQL Connect Example."); 

    public static Connection getConnection() { 
     Connection conn = null; 
     String url = "jdbc:mysql://127.0.0.1:3306/"; 
     String dbName = "networkcoding"; 
     String driver = "com.mysql.jdbc.Driver"; 
     String userName = "root"; 
     String password = "xampp123"; 
     try { 
      Class.forName(driver).newInstance(); 
      conn = DriverManager.getConnection(url + dbName, userName, password); 
      System.out.println("Connected to the database "+dbName); 
      //conn.close(); 
      //System.out.println("Disconnected from database"); 
     } catch (Exception e) { 
      System.out.println("Not Connected to the database - "+dbName); 
      e.printStackTrace(); 
     } 
     return conn; 
    } 

    public static void main(String arg[]){ 
     DBConnect.getConnection(); 
    } 
} 

錯誤是代碼的以下部分中:

package privacysensor; 

import java.io.File; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.Random; 
import javax.swing.JOptionPane; 

public class Sensors extends javax.swing.JFrame { 

    /** 
    * Creates new form Sensors 
    */ 
    String s1 = "", s2 = "", s3 = "", s4 = "", s5 = "", s6 = "", s7 = "", s8 = "", s9 = ""; 
    DBConnect c = new DBConnect(); 
    DateFormat df = new SimpleDateFormat("hh:mm a"); 
    Calendar cal1 = Calendar.getInstance(); 
    org.jdesktop.application.ResourceMap resourceMap = 
     org.jdesktop.application.Application.getInstance(
      privacysensor.PrivacySensorApp.class).getContext().getResourceMap(Sensors.class); 
    Class1 x = new Class1(); 
    int id, id1; 

    public Sensors() { 
     initComponents(); 
     try { 
      id = x.idgeneration(); 
      id1 = x.Tidgeneration(); 
      Connection c1 = c.getConnection(); 
      Connection c2 = c.getConnection(); 
      PreparedStatement pst = c1.prepareStatement("update sensors set status='Offline' where id=" + id); 
      int s = pst.executeUpdate(); 
      if (s == 0) { 
       System.out.println("Not updated"); 
      } else { 
       System.out.println("Updated"); 
      } 
      PreparedStatement pst1 = c2.prepareStatement("update timing set status='Offline' where id=" + id1); 

      s = pst1.executeUpdate(); 
      if (s == 0) { 
       System.out.println("Timer updated"); 
      } else { 
       System.out.println("TImer not updated"); 
      } 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     ... // End of variables declaration     
    } 
} 

任何建議都歡迎。謝謝你,

+0

http://stackoverflow.com/questions/2121829/mysql-jdbc-communications-link-failure – fdsa

+0

你能提供完整的堆棧跟蹤嗎? –

+0

這是哪一個?拒絕連接?或空指針異常? SOCKS代理在哪裏進入潮流?問題是什麼? – EJP

回答

1

我使用的是Mac OS X,我得到了錯誤:
Can't connect to SOCKS proxy:Connection refused: connect
我開了:
系統偏好設置 - >網絡 - >高級 - >代理 - >選中SOCKS代理

這解決了這個問題。

相關問題