从Java到MySQL的Acces拒绝错误 [英] Acces denied error from Java to MySQL

查看:102
本文介绍了从Java到MySQL的Acces拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GUI中遇到错误,因为我将JGrasp用作该项目的IDE和错误状态; <​​/p>

拒绝访问用户'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net'(使用密码:是)

这是该应用程序的唯一类;

package net.roseindia.jtableExample;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.sql.*;
import java.awt.event.*;

public class SearchResult implements ActionListener{
JFrame frame, frame1;
JTextField textbox;
JLabel label;
JButton button;
JPanel panel;
static JTable table;

String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://buiud.com:3306/buiud458_androidhive";
String userName = "XXXX";
String password = "XXXX";
String[] columnNames = {"Roll No", "Name", "Class", "Section"};

public void createUI()
{
    frame = new JFrame("Database Search Result");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    textbox = new JTextField();
    textbox.setBounds(120,30,150,20); 
    label = new JLabel("Enter your roll no");
    label.setBounds(10, 30, 100, 20);
    button = new JButton("search");
    button.setBounds(120,130,150,20);
    button.addActionListener(this);

    frame.add(textbox);
    frame.add(label);
    frame.add(button);
    frame.setVisible(true);
    frame.setSize(500, 400);        
}   

public void actionPerformed(ActionEvent ae)
{
    button = (JButton)ae.getSource();
    System.out.println("Showing Table Data.......");
        showTableData();            
}   

public void showTableData()
{

    frame1 = new JFrame("Database Search Result");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setLayout(new BorderLayout());       
    //TableModel tm = new TableModel();
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columnNames);
    //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());      
    //table = new JTable(model);
    table = new JTable();
    table.setModel(model);      
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setFillsViewportHeight(true);
    JScrollPane scroll = new JScrollPane(table);
    scroll.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);      
    String textvalue = textbox.getText();
    String roll= "";
    String name= "";
    String cl = "";
    String sec = "";
    try
    {           
        Class.forName(driverName);      
        Connection con = DriverManager.getConnection(url, userName, password);
        String sql = "select * from products where pid = "+textvalue;
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        int i =0;
        if(rs.next())
        {
            roll = rs.getString("pid");
            name = rs.getString("name");
            cl = rs.getString("price");
            sec = rs.getString("phone");                    
            model.addRow(new Object[]{roll, name, cl, sec});
            i++;                
        }
        if(i <1)
        {
            JOptionPane.showMessageDialog(null, "No Record Found","Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        if(i ==1)
        {
        System.out.println(i+" Record Found");
        }
        else
        {
            System.out.println(i+" Records Found");
        }
    }
    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
                JOptionPane.ERROR_MESSAGE);
    }
    frame1.add(scroll);
    frame1.setVisible(true);
    frame1.setSize(400,300);
}

public static void main(String args[])
{
    SearchResult sr = new SearchResult();
            sr.createUI();              
}
}

解决方案

最可能的解释是用户未在MySQL中定义.

请注意,在MySQL中,用户是通过用户连接的主机名(或IP地址)以及用户名来标识的. (注意:'%'可以用作主机名的通配符,以允许从任何主机进行连接,但是MySQL会首先查找完全匹配的内容,如果找不到匹配项,则退回到通配符.)

SELECT u.Host,
     , u.User
     , u.Password
  FROM mysql.user u
 WHERE u.User = 'buiud458_raklar'

我们希望在那里有主机值'd24-36-33-148.home1.cgocable.net'的行,或主机值通配符'%'的行.

您可以使用PASSWORD功能将您使用的密码与表中存储的值进行比较...

SELECT PASSWORD('mysecret') 

可以为每个user @ host授予在单个数据库上的特权.

SELECT d.Host
     , d.User
     , d.Db
  FROM mysql.db d
 WHERE d.User = 'buiud458_raklar'


使用数据库特权设置用户的最简单方法:

CREATE USER 'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net' IDENTIFIED BY 'secret' ;
GRANT ALL PRIVILEGES ON mydb.* TO  'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net' ;

(注意:向用户授予所有特权通常会违反最小特权"的最佳实践原则.该用户实际上并不需要所有特权,例如DROP TABLE特权.不必要地授予特权会带来额外的安全风险. )

http://dev.mysql.com/doc/refman/5.5/en/adding-users.html

I'm getting an error in a GUI, because I'm using JGrasp as IDE for this project and the error states;

Access denied for user'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net'(using password: YES)

And here is the only class for the application;

package net.roseindia.jtableExample;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.sql.*;
import java.awt.event.*;

public class SearchResult implements ActionListener{
JFrame frame, frame1;
JTextField textbox;
JLabel label;
JButton button;
JPanel panel;
static JTable table;

String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://buiud.com:3306/buiud458_androidhive";
String userName = "XXXX";
String password = "XXXX";
String[] columnNames = {"Roll No", "Name", "Class", "Section"};

public void createUI()
{
    frame = new JFrame("Database Search Result");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    textbox = new JTextField();
    textbox.setBounds(120,30,150,20); 
    label = new JLabel("Enter your roll no");
    label.setBounds(10, 30, 100, 20);
    button = new JButton("search");
    button.setBounds(120,130,150,20);
    button.addActionListener(this);

    frame.add(textbox);
    frame.add(label);
    frame.add(button);
    frame.setVisible(true);
    frame.setSize(500, 400);        
}   

public void actionPerformed(ActionEvent ae)
{
    button = (JButton)ae.getSource();
    System.out.println("Showing Table Data.......");
        showTableData();            
}   

public void showTableData()
{

    frame1 = new JFrame("Database Search Result");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setLayout(new BorderLayout());       
    //TableModel tm = new TableModel();
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columnNames);
    //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());      
    //table = new JTable(model);
    table = new JTable();
    table.setModel(model);      
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setFillsViewportHeight(true);
    JScrollPane scroll = new JScrollPane(table);
    scroll.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);      
    String textvalue = textbox.getText();
    String roll= "";
    String name= "";
    String cl = "";
    String sec = "";
    try
    {           
        Class.forName(driverName);      
        Connection con = DriverManager.getConnection(url, userName, password);
        String sql = "select * from products where pid = "+textvalue;
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        int i =0;
        if(rs.next())
        {
            roll = rs.getString("pid");
            name = rs.getString("name");
            cl = rs.getString("price");
            sec = rs.getString("phone");                    
            model.addRow(new Object[]{roll, name, cl, sec});
            i++;                
        }
        if(i <1)
        {
            JOptionPane.showMessageDialog(null, "No Record Found","Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        if(i ==1)
        {
        System.out.println(i+" Record Found");
        }
        else
        {
            System.out.println(i+" Records Found");
        }
    }
    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
                JOptionPane.ERROR_MESSAGE);
    }
    frame1.add(scroll);
    frame1.setVisible(true);
    frame1.setSize(400,300);
}

public static void main(String args[])
{
    SearchResult sr = new SearchResult();
            sr.createUI();              
}
}

解决方案

The most likely explanation is that the user is not defined in MySQL.

Note that in MySQL a user is identified by BOTH the hostname (or IP address) the user connects from, AND the username. (NOTE: a '%' can be used as a wildcard for the hostname, to allow connection from any host, but MySQL will first look for an exact match, and then fall back to the wildcard if no match is found.)

SELECT u.Host,
     , u.User
     , u.Password
  FROM mysql.user u
 WHERE u.User = 'buiud458_raklar'

We'd expect there to be row there with Host value of 'd24-36-33-148.home1.cgocable.net', or a row with a host value wildcard of '%'.

You can compare the password you are using to the value stored in the table, with the PASSWORD function...

SELECT PASSWORD('mysecret') 

Each user@host can be granted privileges on individual databases.

SELECT d.Host
     , d.User
     , d.Db
  FROM mysql.db d
 WHERE d.User = 'buiud458_raklar'


The easiest way to setup a user with privileges on a database:

CREATE USER 'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net' IDENTIFIED BY 'secret' ;
GRANT ALL PRIVILEGES ON mydb.* TO  'buiud458_raklar'@'d24-36-33-148.home1.cgocable.net' ;

(NOTE: Granting all privileges to a user typically violates the best-practice principle of "least privilege". This user may not actually require every privilege, for example, DROP TABLE privilege. Unnecessarily granting privileges is an additional security risk.)

http://dev.mysql.com/doc/refman/5.5/en/adding-users.html

这篇关于从Java到MySQL的Acces拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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