简体   繁体   English

如何使用 IP 地址(通过 InetAddress)通过 JDBC 建立与 MySQL 的连接?

[英]How can I use IP address (via InetAddress) to establish a connection with MySQL via JDBC?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inventory;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author Imantha
 */
public class dbcon {

  public static Connection createmyConnection() throws Exception{

    InetAddress ip=InetAddress.getLocalHost();
    String s=ip.getHostAddress();
    Class.forName("com.mysql.jdbc.Driver");
    Connection c = DriverManager.getConnection("jdbc:mysql://localHost:3306/inventory","root","123");   
    return c;
 }
}

How can i use ip addre which find from using InerAddress for connect with MySQL via JDBC?我如何使用通过 JDBC 与 MySQL 连接的 InerAddress 找到的 ip addre?

I want to replace local host and add s(which catch ip address)我想替换本地主机并添加 s(捕获 ip 地址)

You can't.你不能。 You don't need to use InetAddress with JDBC.您不需要将InetAddress与 JDBC 一起使用。 You just need to construct a correct JDBC URL.您只需要构建一个正确的 JDBC URL。

You also haven't needed the Class.forName() line since 2007.自 2007 年以来,您也不需要Class.forName()行。

If only replacing it what you want, then如果只替换它你想要的,那么

Connection c=DriverManager.getConnection("jdbc:mysql://"+s+"/inventory","root","123");

Just change the connection URL to take IP address which is stored in variable s只需更改连接 URL 以获取存储在变量s IP 地址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java程序可以通过代理服务器建立JDBC连接吗 - Can Java program establish JDBC Connection via Proxy Server 如何通过Clientport 80或8080建立与mysql服务器的连接 - How to establish a connection to mysql server via Clientport 80 or 8080 我可以为 JDBC 连接指定 *source* IP 地址吗? - Can I specify *source* IP address for a JDBC connection? 如何检查InetAddress是使用主机名还是IP地址创建的? - How do I check if InetAddress was created using a hostname or an IP address? 我可以在JDBC连接器中使用数据库IP地址代替主机名吗? - Can I use database IP address instead of hostname in JDBC connector? 如何通过 Amazon API 网关 (JAVA) 检索用户的公共 IP 地址 - How can I retrieve a user's public IP address via Amazon API Gateway (JAVA) 如何在JDBC中建立连接池? - How to establish a connection pool in JDBC? 当我可以 ping IP 地址时,为什么 InetAddress.isReachable 返回 false? - Why does InetAddress.isReachable return false, when I can ping the IP address? OkHttpClient 如何通过 http2 与不支持 ALPN 的服务器建立 TLS 连接? - How OkHttpClient can establish TLS connection via http2 with server that does not support ALPN? 通过IP地址访问Web应用程序以及通过计算机的主机名访问Web应用程序时,外观会有所不同吗? - Can there be a difference on how a web application looks when it is accessed via IP Address and when via the host name of the machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM