Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Can't connect with mysql database using java program

Zulfi KhanJun 15 2024

Hi,

I have written the following code on Apache Netbeans IDE 22. I am working on ubuntu 18.04

class Dbconnect {
   boolean connect(){
        Connection con = null;
        System.out.println("Starts1...");
       try {
           System.out.println("Starts2...");
           Class.forName("com.mysql.cj.jdbc.Driver");
           con = DriverManager.getConnection("jdbc:mysql://localhost/ebookshop", "root", "");
           
           System.out.println("Starts3...");
           //if (!con.isClosed()){
           boolean reachable = con.isValid(10);// 10 sec
           if(con==null) {
               System.out.println("Cant connect with to MySQL server?????? ERRROR...");
               return false;
           }
           else{
               Statement stmt = con.createStatement();
               String sql = "select * from books";
               ResultSet rs = stmt.executeQuery(sql);
               String name;
               String author;
               double price;
               int qty;
               while(rs.next()){
                  name = rs.getString("title");
                  author = rs.getString("author");
                  price  = rs.getInt("price");
                  System.out.println("title ="+name+"author="+author+"price="+price);
                  
                                 
               }
               
               
           }
       } catch(Exception e) {
           //System.err.println("Exception: " + e.getMessage());
           e.printStackTrace(System.err);
       } finally {
           try {
               if (con != null){
                   con.close();
                   return true;
               }
                    
           } catch(SQLException e) {//System.err.println("Exception2: " + e.getMessage());
                                     e.printStackTrace(System.err);
           }
   }
   return false;
   }   
}

When I try to connect with the database, I am getting the following error:

Hello World!
Starts1...
Starts2...
java.sql.SQLException: Access denied for user 'root'@'localhost'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:819)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:239)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:188)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
    at com.mycompany.db1mav.Dbconnect.connect(DB1ConnectMav.java:23)
    at com.mycompany.db1mav.DB1mav.main(DB1mav.java:16)
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  2.764 s
Finished at: 2024-06-15T10:37:38-05:00

My MySQL DB information is:

ysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| ebookshop          |
| menagerie          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.01 sec)
mysql> use ebookshop;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> show tables;
+---------------------+
| Tables_in_ebookshop |
+---------------------+
| books               |
+---------------------+
1 row in set (0.00 sec)

Somebody please guide me.

Zulfi.

Comments
Post Details
Added on Jun 15 2024
1 comment
208 views