[英]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.