mysql无法添加数据信息,无法插入和检索mysql服务器数据库信息

i have created a database named test(table name demo) on wamp on my friend's pc.i am able to see this table from my browser using ip address of my friend's pc. but i want to insert and retrive data into this database(test) using java code from my pc. i try that but netbeans shows an error message.

here is my code :

package ashdemo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Ashdemo {

public static void main(String args[]) throws InstantiationException, IllegalAccessException

{

try{

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

Connection con = DriverManager.getConnection("jdbc:mysql://friend's_ipaddress:3306/test","username","password

");

Statement stmt=con.createStatement();

stmt.executeUpdate("Insert into demo values(1,'abc','nagpur')");

//ResultSet rs= stmt.executeQuery("Select name from demo where id=1");

//rs.next();

//String name= rs.getString("name");

//System.out.println(name);

System.out.println("DOne..");

//INSERT INTO `student`(`id`, `name`, `address`) VALUES (1,'amol','nagpur');

con.close();

}

catch(ClassNotFoundException | SQLException e){

System.out.println("error"+e);

}

}

}

error message is :

errorjava.sql.SQLException: Access denied for user

'username'@'myipaddress' (using password: YES)

解决方案

You need to set up mysql to allow remote connection for a particular user. The default syntax is:

grant on to @ identified by

So here you have to use-

grant all on test.* to 'username'@'your_ipaddress' identified by 'password'

Run this command in MySQL command prompt.

This will allow username to connect from your IP using that password and give all permissions on all tables in the database-test.

To allow any user to connect from any IP address and use all the tables in any database use the following syntax-

grant all on *.* to '%'@'%' identified by 'password'

And finally you have to use the following command-

FLUSH PRIVILEGES;

To reload all privileges.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值