运行连接到另一台计算机上的本地主机数据库的Java程序 [英] Running a Java program that connects to a localhost database on a different computer

查看:348
本文介绍了运行连接到另一台计算机上的本地主机数据库的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下代码将我的基本Java应用程序连接到计算机上的Wamp服务器

I've connected my really basic Java application to the Wamp server on my computer using the following code

try {  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection connection = DriverManager.getConnection(  
                "jdbc:mysql://localhost:3306/project", "root", "password");

很明显,此程序在我的计算机上运行,​​但没有其他人在运行,我需要将其作为项目提交.

Obviously, this program runs on my computer, but no one else's, and I need to submit this as a project.

无论如何,我是否可以修改此部分,以便我的伴侣和老师可以从其计算机上运行代码,而不必下载任何特殊软件,也可以访问本地主机上的数据库?可能像用//my IP address:8080替换//localhost:3306一样?

Is there anyway I can modify this part so that my partner and teacher can run the code from their computers, without having to download any special software, and access the database on my localhost? Possibly like replacing //localhost:3306 with //my IP address:8080?

我得到的所有建议都涉及到网络,我对此一无所知,所以除非我真的很确定,否则我不想尝试然后弄乱我的计算机

All suggestions I've gotten involve networking, which I don't know anything about so I don't want to try and then mess up my computer unless I'm really sure

我已经研究过但不确定的事情:

Things I've already looked in to, but aren't sure about:

  • C:/wamp/alias/phpmyadmin.conf并输入ALLOW (other person's IP)
  • 团队查看器(要求另一台计算机使用我的程序以外的软件)
  • 打开我的路由器端口以接收传入请求
  • C:/wamp/alias/phpmyadmin.conf and entering the line ALLOW (other person's IP)
  • team viewer (requires the other computer to use software other than my program)
  • opening my router port to take incoming requests

对任何人都可以帮助,甚至只是确认如果没有一些网络就无法完成. 如果这是没有经验的人可以做的,我会接受的.

Anything'll help guys, even just confirming it can't be done without some networking. If it's something an inexperienced person can do, I'll take it.

推荐答案

除了尝试的方法,还需要授予访问数据库的权限.

Apart from what you have tried, you also need granting privileges to access your database.

尝试从其他IP地址向用户授予特权.

Try granting privileges to users from other IP addresses.

阅读文档 GRANT语法.

示例:

GRANT ALL ON test.* TO '%'@'localhost' ...
GRANT ALL ON test.* TO '%'@'%' ...
GRANT ALL ON test.* TO 'ravinder'@'192.168.1.105' ...
etc...

还有其他形式可以授予从特定计算机或全部连接的特定或所有用户对特定数据库对象的访问权限.
我建议您选择一种适合您的需求.

There are other forms of granting access to specific database object to specific or all users connecting from a specific computer or all.
I suggest you chose the one which is appropriate for your need.

但是请记住,'%'@'%'允许 all 系统的 all 用户.

But, keep in mind that '%'@'%' will allow all users from all systems.

在授予特权之后,用IP地址更改连接字符串的dbURL值.

After granting the privileges, change your dbURL value for connection string with your IP address.

Connection connection = 
DriverManager.getConnection(  
                "jdbc:mysql://your_system_ip_or_name_here:3306/project",
                "root", "password");

在所需的系统上部署重新生成的应用程序,它应该可以正常工作.

Deploy the regenerated app on desired systems and it should be working.

这篇关于运行连接到另一台计算机上的本地主机数据库的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆