java过去localhost出错,错误:java.sql.SQLException:用户'root @ localhost'@'localhost'的访问被拒绝(使用密码:是)...

I am USING mysql (XAMPP) on my netbeans.

I am trying the query

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;

but cant't connect:

ERROR: java.sql.SQLException:Access denied for user 'root'@'localhost'

Here the whole code:

public class NextHandler implements ActionListener{

@Override

public void actionPerformed (java.awt.event.ActionEvent evt){

try{

String url="jdbc:mysql://localhost:3306/database2";

String username= "root";

String password= " ";

Connection con=DriverManager.getConnection(url, username,password);

Statement stat=con.createStatement();

String Query=

"INSERT INTO donor_info(donor_id,donor_lastname,donor_firstname,donor_middlename,donor_age,donor_weight,donor_height,donor_gender,donor_contact_number,"

+ "medical_condition)VALUES ('"+txtdonorid.getText()+"','"+txtlname.getText()+"','"+txtfname.getText()+"','"+txtmname.getText()+"',"

+ "'"+txtage.getText()+"','"+txtweight.getText()+"','"+txtheight.getText()+"')";

stat.execute(Query);

JOptionPane.showMessageDialog(null,"Donor Added to Database");

txtdonorid.setText(null);

txtlname.setText(null);

txtfname.setText(null);

txtmname.setText(null);

txtage.setText(null);

txtweight.setText(null);

txtheight.setText(null);

}

catch (SQLException ex){

JOptionPane.showMessageDialog(null,ex.toString());

}

bloodtype f=new bloodtype();

f.setVisible(true);

dispose();

f.run();

}

}

public void run(){

//setUndecorated(true);

setSize(1920, 1200);

setLocationRelativeTo(null);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) throws Exception{

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

donorinfo f = new donorinfo();

f.run();

}

}

解决方案

After you run your query you have to run

FLUSH PRIVILEGES

Your code is taking a space as password. Please change password =" " to password=""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值