java - 无法连接到 SOCKS 代理 :Connection refused: connect

标签 java mysql swing netbeans

我收到以下错误。检查了很多解决方案,但没有得到结果。

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.

The following is the database connection code.

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                   
    }
}

欢迎提出任何建议。谢谢,

最佳答案

我使用的是 mac OS X,但出现错误:
无法连接到 SOCKS 代理:连接被拒绝:连接
我打开了:
系统偏好设置 -> 网络 -> 高级 -> 代理 -> 未选中的 SOCKS 代理

这解决了问题。

关于java - 无法连接到 SOCKS 代理 :Connection refused: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890243/

相关文章:

java - Android - 如何从原始文件中获取 Uri?

java - 将父类(super class)的对象转换为子类

mysql - 选择 MAX(ID) mysql

mysql - 为什么我的 JOIN 显示双重结果?

java - Java(和 SwingUtilities)中的 GUI 线程

java - PriorityQueue 返回自定义比较器的错误排序?

java - Spring 对 <context :component-scan/> vs <mvc:annotation-driven> 给出的@Controller 的支持

mysql - 我需要删除最后一个整数之后的所有内容

java 。如何绘制 JTable 中的特定单元格?

java - CardLayout 管理器问题/困惑