java访问其他机器上的mysql,运行连接到另一台计算机上的本地主机数据库的Java程序...

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.

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 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.

Try granting privileges to users from other IP addresses.

Read documentation GRANT Syntax.

Example:

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.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值