2015-10-24 47 views
-3
/* 
* 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; 
} 
} 

我該如何使用ip addre從InerAddress中找到通過JDBC連接MySQL?如何使用IP地址(通過InetAddress)通過JDBC與MySQL建立連接?

我要替換本地主機和加s(其捕獲IP地址)

回答

0

如果只更換它,你想要什麼,然後

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

只要改變連接URL採取的IP地址,該地址存儲在變量中s

1

你不能。您不需要使用帶有JDBC的InetAddress。您只需構建正確的JDBC URL。

您自2007年以來也不需要Class.forName()系列。