如何使用 java SQL 连接器连接到不同计算机上的数据库
How can the java SQL connector be used to connect to a database on a different computer
我的计算机上有一个用于测试目的的简单数据库,我正在尝试从笔记本电脑的数据库中检索一些信息。所以我希望我的笔记本电脑发出请求以查看我的计算机 MySQL 数据库中的信息。下面显示了我试图在我的笔记本电脑上 运行 收集学生 table 中的第一个条目的 java 代码,它位于我的计算机上。
我的笔记本电脑和电脑上都安装了MySQL workbench,如果电脑要存储数据而笔记本电脑只提取数据,是否有必要在两台机器上都安装?
到目前为止,我从研究中了解到 public ip 应该用于 url 而不是计算机的 ip,所以我添加了它,但我收到了CommunicationsException 以及堆栈跟踪中的 "Connection timed out"。我已经通读了 this answer and this 对类似问题的回答,但我很难理解这两种解决方案,有人可以向我介绍使用 MySQL.[= 从数据库远程访问数据的初学者指南吗? 14=]
public class TestRemote{
//JDBC variables
Connection connection;
Statement statement;
ResultSet resultSet;
//String variables
String url;
String user;
String password;
public static void main(String[] args) {
TestRemote sql = new TestRemote();
ArrayList<String> firstnames = sql.getColumn("students", "firstname", "studentid=4");
System.out.println(firstnames.get(0));
}
// Constructor
public TestRemote()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("couldnt find class");
}
url = "jdbc:mysql://81.159.3.167:3306/test"; //?autoReconnect=true&useSSL=false";
user = "user";
password = "pass123";
connection = null;
statement = null;
resultSet = null;
}
private void closeConnection(){
try{
if(connection != null)
connection.close();
if(statement != null)
statement.close();
if(resultSet != null)
resultSet.close();
connection=null; resultSet=null; statement=null;
}catch(Exception e){
e.printStackTrace();
}
}
public ArrayList<String> getColumn(String table, String column, String where) {
ArrayList<String> resultsArray = new ArrayList<String>();
try {
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
if(!where.equals(""))
resultSet = statement.executeQuery("SELECT "+column+" FROM "+table + " WHERE "+where);
else
resultSet = statement.executeQuery("SELECT "+column+" FROM "+table);
while(resultSet.next()) {
String val = resultSet.getString(1);
if(val==null)
resultsArray.add("");
else
resultsArray.add(val);
}
//resultsArray = (ArrayList<String>) resultSet.getArray(column);
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Model.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
closeConnection();
return resultsArray;
}
}
I have MySQL workbench installed on both my laptop and computer, is it
necessary to be on both machines if the computer will store the data
and the laptop only extracts data.
不,你所说的 "Computer" 是你的服务器。它不需要 mysql workbench。它只需要 mysql 服务器
the public ip should be used in the url instead of the ip for the
computer
数据库几乎不应该暴露在 public IP 地址上。如果两台计算机都在 LAN 上,则专用网络 IP 是服务器应该侦听的内容,也是您应该在连接字符串上使用的内容。
CommunicationsException along with "Connection timed out" in the stack
trace
因为服务器不是 运行,所以没有监听 ip:port 或防火墙丢弃数据包。
您的 Java 代码可能没问题。但问题是,另一台机器上是否有 MySQL 服务器 运行 并在 public IP 上侦听端口 3306?默认情况下,它应该只监听本地主机,所以你需要更改你的 MySQL 安装,以便它监听 public IP。还要确保没有防火墙阻止访问。尝试连接笔记本电脑上的 Workbench 以连接到另一台机器上的 MySQL 服务器。如果您收到此 运行,请再次尝试您的 Java 代码。
我的计算机上有一个用于测试目的的简单数据库,我正在尝试从笔记本电脑的数据库中检索一些信息。所以我希望我的笔记本电脑发出请求以查看我的计算机 MySQL 数据库中的信息。下面显示了我试图在我的笔记本电脑上 运行 收集学生 table 中的第一个条目的 java 代码,它位于我的计算机上。
我的笔记本电脑和电脑上都安装了MySQL workbench,如果电脑要存储数据而笔记本电脑只提取数据,是否有必要在两台机器上都安装?
到目前为止,我从研究中了解到 public ip 应该用于 url 而不是计算机的 ip,所以我添加了它,但我收到了CommunicationsException 以及堆栈跟踪中的 "Connection timed out"。我已经通读了 this answer and this 对类似问题的回答,但我很难理解这两种解决方案,有人可以向我介绍使用 MySQL.[= 从数据库远程访问数据的初学者指南吗? 14=]
public class TestRemote{
//JDBC variables
Connection connection;
Statement statement;
ResultSet resultSet;
//String variables
String url;
String user;
String password;
public static void main(String[] args) {
TestRemote sql = new TestRemote();
ArrayList<String> firstnames = sql.getColumn("students", "firstname", "studentid=4");
System.out.println(firstnames.get(0));
}
// Constructor
public TestRemote()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("couldnt find class");
}
url = "jdbc:mysql://81.159.3.167:3306/test"; //?autoReconnect=true&useSSL=false";
user = "user";
password = "pass123";
connection = null;
statement = null;
resultSet = null;
}
private void closeConnection(){
try{
if(connection != null)
connection.close();
if(statement != null)
statement.close();
if(resultSet != null)
resultSet.close();
connection=null; resultSet=null; statement=null;
}catch(Exception e){
e.printStackTrace();
}
}
public ArrayList<String> getColumn(String table, String column, String where) {
ArrayList<String> resultsArray = new ArrayList<String>();
try {
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
if(!where.equals(""))
resultSet = statement.executeQuery("SELECT "+column+" FROM "+table + " WHERE "+where);
else
resultSet = statement.executeQuery("SELECT "+column+" FROM "+table);
while(resultSet.next()) {
String val = resultSet.getString(1);
if(val==null)
resultsArray.add("");
else
resultsArray.add(val);
}
//resultsArray = (ArrayList<String>) resultSet.getArray(column);
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Model.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
closeConnection();
return resultsArray;
}
}
I have MySQL workbench installed on both my laptop and computer, is it necessary to be on both machines if the computer will store the data and the laptop only extracts data.
不,你所说的 "Computer" 是你的服务器。它不需要 mysql workbench。它只需要 mysql 服务器
the public ip should be used in the url instead of the ip for the computer
数据库几乎不应该暴露在 public IP 地址上。如果两台计算机都在 LAN 上,则专用网络 IP 是服务器应该侦听的内容,也是您应该在连接字符串上使用的内容。
CommunicationsException along with "Connection timed out" in the stack trace
因为服务器不是 运行,所以没有监听 ip:port 或防火墙丢弃数据包。
您的 Java 代码可能没问题。但问题是,另一台机器上是否有 MySQL 服务器 运行 并在 public IP 上侦听端口 3306?默认情况下,它应该只监听本地主机,所以你需要更改你的 MySQL 安装,以便它监听 public IP。还要确保没有防火墙阻止访问。尝试连接笔记本电脑上的 Workbench 以连接到另一台机器上的 MySQL 服务器。如果您收到此 运行,请再次尝试您的 Java 代码。