为什么我无法通过Internet连接到我的数据库

时间:2023-01-07 22:45:42

I have an sql database called roottesting and I manage it at the ip 192.168.1.121. I also have a simple java application that connects to it (or rather, is supposed to connect to it) over the Internet with a user named 'user' that has a host of '%', which I know will make it usable for any computer that uses it. Here is the method in which my program connects to the database:

我有一个名为roottesting的sql数据库,我在ip 192.168.1.121管理它。我还有一个简单的java应用程序,通过互联网连接到它(或者更确切地说,应该连接到它),用户名为'user',主机为'%',我知道这将使其可用于任何使用它的电脑。这是我的程序连接到数据库的方法:

public void addTestEntry(TestEntry testEntry)
        throws ClassNotFoundException, SQLException {

    tests.add(testEntry);

    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://192.168.1.121/roottesting";

    con = DriverManager.getConnection(url, "user", "S@cajaw3a");

    PreparedStatement statement = con
            .prepareStatement("insert into tests values(?, ?, ?)");

    statement.setString(1, testEntry.getName());
    statement.setInt(2, testEntry.getCorrect());
    statement.setInt(3, testEntry.getIncorrect());

    statement.executeUpdate();

    con.close();
}

This will work every time I download the program on another computer on my home network, Beigecedar, but when I try to do it on another network, like my school's network, it does not work and gives that standard message that signals that the database could not be found. Can anyone give me a suggestion about how to connect to my database over the Internet, no matter what network the computer running the program is in?

每次我在家庭网络Beigecedar上的另一台计算机上下载程序时都会这样,但是当我尝试在另一个网络上进行操作时,例如我学校的网络,它不起作用并提供标识消息,表明数据库可以找不到。任何人都可以给我一个关于如何通过Internet连接到我的数据库的建议,无论运行该程序的计算机在哪个网络中?

3 个解决方案

#1


0  

The IP Address you are connecting to is what is called a Reserved IP Address

您要连接的IP地址是所谓的保留IP地址

It is local to your network only ( 192.168.0.0/16 ). With that aside let's address 2 things.

它仅适用于您的网络(192.168.0.0/16)。除此之外,让我们解决两件事。

If you are allowed to connect to your own computer from the internet (IE: Firewall and ports open) then you can connect using your public IP Address

如果允许您从Internet连接到您自己的计算机(IE:防火墙和端口打开),那么您可以使用您的公共IP地址连接

You also have to consider what ports your school allow to be open. They may very well block the ports required for you to make this connection.

您还必须考虑学校允许开放的港口。它们可以很好地阻止您建立此连接所需的端口。

#2


0  

192.168.1.121 is a local IP, you need a Public IP. If you have internet you can use something like www.whatismyip.com to know your public ip. The problem with that IP is may change with the time so your app also may broke in the future.

192.168.1.121是本地IP,需要公共IP。如果你有互联网,你可以使用像www.whatismyip.com这样的东西来了解你的公共IP。该IP的问题可能会随着时间而改变,因此您的应用程序将来也可能会崩溃。

Or use a firewall to reruote public IP to your pc in case you are behind a router

或者使用防火墙为您的电脑重新启动公共IP,以防您在路由器后面

#3


0  

192.x.x.x is a private address range. It will only be available locally, and not beyond the internet router. Look at setting up NAT (network address translation) if you wish to access this host from other sites.

192.x.x.x是私有地址范围。它只能在本地使用,而不能在互联网路由器之外。如果您希望从其他站点访问此主机,请查看设置NAT(网络地址转换)。

#1


0  

The IP Address you are connecting to is what is called a Reserved IP Address

您要连接的IP地址是所谓的保留IP地址

It is local to your network only ( 192.168.0.0/16 ). With that aside let's address 2 things.

它仅适用于您的网络(192.168.0.0/16)。除此之外,让我们解决两件事。

If you are allowed to connect to your own computer from the internet (IE: Firewall and ports open) then you can connect using your public IP Address

如果允许您从Internet连接到您自己的计算机(IE:防火墙和端口打开),那么您可以使用您的公共IP地址连接

You also have to consider what ports your school allow to be open. They may very well block the ports required for you to make this connection.

您还必须考虑学校允许开放的港口。它们可以很好地阻止您建立此连接所需的端口。

#2


0  

192.168.1.121 is a local IP, you need a Public IP. If you have internet you can use something like www.whatismyip.com to know your public ip. The problem with that IP is may change with the time so your app also may broke in the future.

192.168.1.121是本地IP,需要公共IP。如果你有互联网,你可以使用像www.whatismyip.com这样的东西来了解你的公共IP。该IP的问题可能会随着时间而改变,因此您的应用程序将来也可能会崩溃。

Or use a firewall to reruote public IP to your pc in case you are behind a router

或者使用防火墙为您的电脑重新启动公共IP,以防您在路由器后面

#3


0  

192.x.x.x is a private address range. It will only be available locally, and not beyond the internet router. Look at setting up NAT (network address translation) if you wish to access this host from other sites.

192.x.x.x是私有地址范围。它只能在本地使用,而不能在互联网路由器之外。如果您希望从其他站点访问此主机,请查看设置NAT(网络地址转换)。