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

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

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

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;
     }
}
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:端口或防火墙来丢弃数据包。

相关问题