Connect to MySQL using JDBC driver through a proxy

2019-05-31 07:17发布

In Java, I would like to make a connection to a MySQL server which is on the web from a client computer that is behind a http proxy. I have read few solutions some say http tunnelling might work and some suggest a very old link from oracle which is not available anymore. So the question is:

How can we connect to a MySQL server from a computer which is behind a http proxy?

1条回答
放我归山
2楼-- · 2019-05-31 07:51

You can try the following code and see if it works. It worked for me over TCP

package indika.jdbc.connectivity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectOverProxy {
    public static void main(String[] args) {
        new ConnectOverProxy();
    }

    public ConnectOverProxy() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = null;
            Properties info = new Properties();
            //info.put("proxy_type", "4"); // SSL Tunneling
            info.put("proxy_host", "[proxy host]");
            info.put("proxy_port", "[proxy port]");
            info.put("proxy_user", "[proxy user]");
            info.put("proxy_password", "[proxy password]");
            info.put("user", "[db user]");
            info.put("password", "[db pass word]");
            conn = DriverManager.getConnection("jdbc:mysql://[db host]/",info);


            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("Select NOW()");
            rs.next();
            System.out.println("Data- " + rs.getString(1));
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException er) {
            er.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}

Also look at "http://www.idssoftware.com/jdbchttps.html", However I have not used this personally.

查看更多
登录 后发表回答