简体   繁体   中英

How can I share my database using IP Address?

 import java.sql.Connection; 
 import java.sql.DriverManager; 
 import javax.swing.JOptionPane;  
  public class Database {      
      public static Connection con = null;      
      public static Connection connectDB() {         
       try {             
         Class.forName("com.mysql.jdbc.Driver");            
         Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/registerdb",                     "root", "");  
     //JOptionPane.showMessageDialog(null, "Connection Successful");             
     return con;         
    } catch (Exception e) {             
     JOptionPane.showMessageDialog(null, e);        
     return null;   
  }    
  }  
 }

How I make my finish program share to others locally or using the internet and how to use the IP address for it? I'm using NetBeans for my java programming and XAMPP for my database. Please help me.. THanks

在任何计算机上安装数据库服务器,获取服务器IP地址和端口,并将该信息传递给DriverManager.getConnection("jdbc:mysql://IP:port/dbname")最好使用属性文件存储IP和端口并读取它创建连接时,可以轻松更改它而无需更改代码。

Your program should work as it is on multiple machines as long as they are in the same local network (as you are using local IP address).

If they are not able to connect to DB, following can be the possible reasons:

  1. DB is not configured to allow connection from another machine. You can grant it using following syntax: grant all on db-name.* to username@'%' identified by 'password'

  2. Your firewall is blocking income 3306 port requests. Open the port for incoming requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM