首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Netbeans与mysql通信

Netbeans与mysql通信
EN

Stack Overflow用户
提问于 2015-04-22 03:52:23
回答 1查看 1.9K关注 0票数 1

我为我们公司创建了一个应用程序。一开始,我尝试在MySQL-xampp数据库上创建没有密码的内容,它运行得很好。现在我的问题是,我在数据库中输入了一个密码,每当我登录时,我的应用程序就会抛出这个错误:

SQL java.sql.SQLException:拒绝用户‘root’@‘CITSP 7’的访问(使用密码: YES)

连接数据库时也会出现一个错误:

无法连接。无法使用com.mysql.jdbc.Driver建立到jdbc:mysql//localhost:3306/mydb的连接(用户'root'@'CITSP-MOB7‘被拒绝访问(使用密码: YES))。

下面是我的数据库连接代码:

代码语言:javascript
运行
复制
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://192.168.1.112:3306/citsp";
static final String USER = "root";
static final String PASS = "mypassword";

try{
   Class.forName("com.mysql.jdbc.Driver");
  }catch(ClassNotFoundException ex){
            }
  //STEP 3: Open a connection
  System.out.println("Connecting to database...");
  conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //STEP 4: Execute a query
  System.out.println("Creating statement...");
  stmt = conn.createStatement();

有人能帮我解决我的连接问题吗?您的回答将不胜感激。谢谢!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-22 06:25:45

如果这对您有用,请测试在mysql中创建一个新用户。

首先,在mysql中创建一个新用户:以root用户身份从mysql命令行登录。然后逐一运行以下命令。

MySQL

  1. 创建由‘asdf’标识的用户'mydbuser'@'localhost‘;
  2. 授予所有特权。“mydbuser”@“localhost”;
  3. 冲洗特权;

将上述java代码更改为:我假定数据库名为'citsp'

JAVA

代码语言:javascript
运行
复制
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DB_URL = "jdbc:mysql://localhost/citsp";
        String USER = "mydbuser";
        String PASS = "asdf";

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            //JOptionPane.showMessageDialog(null, "There's an error connecting with the database. Please contact your administrator.");
        }
        //STEP 3: Open a connection
        System.out.println("Connecting to database...");
        java.sql.Connection conn = null;
        try {
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        //STEP 4: Execute a query
        System.out.println("Creating statement...");
        try {
            Statement stmt = conn.createStatement();
            String query = "";
            ResultSet rs = stmt.executeQuery(query);
            while(rs.next()){
                // Iterate through the result
            }
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29787434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档