Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java连接到远程MySQL数据库?_Java_Mysql - Fatal编程技术网

如何使用Java连接到远程MySQL数据库?

如何使用Java连接到远程MySQL数据库?,java,mysql,Java,Mysql,有人能给我提供JavaSE编码来连接服务器中的MySQL数据库(而不是本地主机中的数据库)吗?我应该放什么来代替“localhost”和端口号 import java.sql.Connection; import java.sql.DriverManager; public class Main { public static void main(String[] argv) throws Exception { String driverName = "org.gjt.mm.my

有人能给我提供JavaSE编码来连接服务器中的MySQL数据库(而不是本地主机中的数据库)吗?我应该放什么来代替“localhost”和端口号

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);

    String serverName = "localhost";
    String mydatabase = "mydatabase";
    String url = "jdbc:mysql :// " + serverName + "/" + mydatabase; 

    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
  }
}
简单使用

String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
确保tou有权访问mysql中的远程计算机

将您的.database.domain改为ip

只需使用

String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
确保tou有权访问mysql中的远程计算机


将.database.domain改为ip

会出现什么错误?如果您能够在本地计算机上连接它,则只需将连接字符串中的
localhost
或您的ip地址替换为您要连接的远程计算机的ip地址或域名即可。在您的代码中,将
localhost
替换为
serverName
的值,使用远程计算机的ip。另外,您正在使用的用户名/密码在远程计算机的MySQL上应该有效。是否有任何代码可以在Java中查找服务器ip地址?为什么需要查找服务器ip地址?你不应该事先知道吗?你的
org.gjt.mm.mysql.Driver
驱动程序已经过时了,从现在开始使用
com.mysql.jdbc.Driver
驱动程序。另外,只需将您的
serverName
更改为您的服务器IP地址。就这样!:-)谢谢你@精英绅士你犯了什么错误?如果您能够在本地计算机上连接它,则只需将连接字符串中的
localhost
或您的ip地址替换为您要连接的远程计算机的ip地址或域名即可。在您的代码中,将
localhost
替换为
serverName
的值,使用远程计算机的ip。另外,您正在使用的用户名/密码在远程计算机的MySQL上应该有效。是否有任何代码可以在Java中查找服务器ip地址?为什么需要查找服务器ip地址?你不应该事先知道吗?你的
org.gjt.mm.mysql.Driver
驱动程序已经过时了,从现在开始使用
com.mysql.jdbc.Driver
驱动程序。另外,只需将您的
serverName
更改为您的服务器IP地址。就这样!:-)谢谢@Elite Gentlemanyes,但您需要更改dburl和驱动程序。i、 是的,但您需要更改dburl和驱动程序。i、 Class.forName中的e字符串