如何使用javasql连接器连接到另一台计算机上的数据库

mbyulnm0  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(394)

我有一个简单的数据库在我的电脑上测试的目的,我正试图从数据库检索一些信息,从我的笔记本电脑。所以我想让我的笔记本电脑请求查看我的电脑mysql数据库中的信息。下面显示了我试图在笔记本电脑上运行的java代码,以收集students表中的第一个条目,该表位于我的计算机上。
我的笔记本电脑和电脑上都安装了mysql workbench,如果电脑存储数据而笔记本电脑只提取数据,那么这两台电脑上都必须安装mysql workbench吗。
到目前为止,我从研究中学到的是,应该在url中使用公共ip,而不是计算机的ip,所以我在中添加了这个,但在堆栈跟踪中收到了一个communicationexception和“connection timed out”。我已经通读了这个答案和一个类似问题的答案,但是我很难理解这两个解决方案,有人能给我介绍一下使用mysql从数据库远程访问数据的初学者指南吗。

  1. public class TestRemote{
  2. //JDBC variables
  3. Connection connection;
  4. Statement statement;
  5. ResultSet resultSet;
  6. //String variables
  7. String url;
  8. String user;
  9. String password;
  10. public static void main(String[] args) {
  11. TestRemote sql = new TestRemote();
  12. ArrayList<String> firstnames = sql.getColumn("students", "firstname", "studentid=4");
  13. System.out.println(firstnames.get(0));
  14. }
  15. // Constructor
  16. public TestRemote()
  17. {
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. } catch (ClassNotFoundException e) {
  21. System.out.println("couldnt find class");
  22. }
  23. url = "jdbc:mysql://81.159.3.167:3306/test"; //?autoReconnect=true&useSSL=false";
  24. user = "user";
  25. password = "pass123";
  26. connection = null;
  27. statement = null;
  28. resultSet = null;
  29. }
  30. private void closeConnection(){
  31. try{
  32. if(connection != null)
  33. connection.close();
  34. if(statement != null)
  35. statement.close();
  36. if(resultSet != null)
  37. resultSet.close();
  38. connection=null; resultSet=null; statement=null;
  39. }catch(Exception e){
  40. e.printStackTrace();
  41. }
  42. }
  43. public ArrayList<String> getColumn(String table, String column, String where) {
  44. ArrayList<String> resultsArray = new ArrayList<String>();
  45. try {
  46. connection = DriverManager.getConnection(url, user, password);
  47. statement = connection.createStatement();
  48. if(!where.equals(""))
  49. resultSet = statement.executeQuery("SELECT "+column+" FROM "+table + " WHERE "+where);
  50. else
  51. resultSet = statement.executeQuery("SELECT "+column+" FROM "+table);
  52. while(resultSet.next()) {
  53. String val = resultSet.getString(1);
  54. if(val==null)
  55. resultsArray.add("");
  56. else
  57. resultsArray.add(val);
  58. }
  59. //resultsArray = (ArrayList<String>) resultSet.getArray(column);
  60. } catch (SQLException ex) {
  61. Logger lgr = Logger.getLogger(Model.class.getName());
  62. lgr.log(Level.SEVERE, ex.getMessage(), ex);
  63. }
  64. closeConnection();
  65. return resultsArray;
  66. }
  67. }
b1uwtaje

b1uwtaje1#

您的java代码可能还不错。但问题是,在另一台机器上,mysql服务器是否在公共ip上的3306端口上运行和侦听?默认情况下,它应该只在localhost上侦听,因此您需要更改mysql安装,以便它侦听公共ip。还要确保没有防火墙阻止访问。尝试连接笔记本电脑上的工作台,以到达另一个盒子上的mysql服务器。如果运行了这个程序,请再次尝试java代码。

4dc9hkyq

4dc9hkyq2#

我的笔记本电脑和电脑上都安装了mysql workbench,如果电脑存储数据而笔记本电脑只提取数据,那么这两台电脑上都必须安装mysql workbench吗。
不,你所说的“电脑”是你的服务器。它不需要mysql工作台。它只需要mysql服务器
应该在url中使用公共ip,而不是计算机的ip
数据库几乎不应该暴露在公共ip地址上。如果两台计算机都在局域网上,则专用网络ip是服务器应该监听的,这也是您应该在连接字符串上使用的。
通信异常以及堆栈跟踪中的“连接超时”
因为服务器没有运行,所以没有监听ip:端口或防火墙来丢弃数据包。

相关问题