Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new in Netbeans. I am trying to making a new project on Store Management by Netbeans and mysql workbench as my DB. But I am facing problem in connecting database. Here my codes are given:

    enter code here
package MyDb.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;



public class DbConnection {
     private static Connection conn=null;
    static{
        Throwable e = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                System.out.println("Connecting Database!!");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/GanpatiDB","root","");
                System.out.println("");
            } catch (SQLException ex) {
                throw new RuntimeException("Cannot connect the database!", e);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, e);
            System.out.println("Terminating application...check database");
            System.exit(0);
        }
         finally {
    System.out.println("Closing the connection.");

}
    }
    private static Object connection;


public DbConnection(){

    }
    public static Connection getConnection(){
        return conn;
    }
    public static void closeConnection() throws Exception{

        conn.close();
    }


}

After debugging the project the console gives the result as

BUILD SUCCESSFUL (total time: 0 seconds) But still it is showing some errors as below:

    Jan 14, 2014 1:47:13 PM MyDb.utils.DbConnection <clinit>
Terminating application...check database
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'GanpatiDB[root on Default schema]'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:233)
    at MyDb.utils.DbConnection.<clinit>(DbConnection.java:20)
    at desktopapplication1.NewJFrame.<init>(NewJFrame.java:27)
    at desktopapplication1.NewJFrame$2.run(NewJFrame.java:199)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
share|improve this question
    
remember any connection with database is not the business of IDE, IDE is just a coding and compiling platform, which will not compile the code itself. –  Rugal Jan 14 at 8:31
    
Can you please guide me in getting the permission –  AM26 Jan 14 at 8:32
    
as the error prompted MySQLSyntaxErrorException, please check the syntax of connection –  Rugal Jan 14 at 8:32
    
try to use the following : dev.mysql.com/doc/refman/5.7/en/… –  Shaban Mousa Jan 14 at 8:38

1 Answer 1

up vote 0 down vote accepted

The build is showing up as successful for a good reason: you are catching the actual error and making the log statement yourself:

catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, e);
System.out.println("Terminating application...check database");
System.exit(0);
}

Because it is catching the error, the code executes as expected (if you want it to show up as failed, you should change the parameter for System.exit to -1).

The error you're catching is a ClassNotFoundException, Java seems unable to find the MySQL driver you're trying to find using Class.forName("com.mysql.jdbc.Driver");. If you look at the logs, there is no mention of the driver at all, which seems to support this theory.

share|improve this answer
    
Thank you , my database connection is done but now another error occurs: "Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError at desktopapplication1.NewJFrame.<init>(NewJFrame.java:27) at desktopapplication1.NewJFrame$2.run(NewJFrame.java:199)" –  AM26 Jan 14 at 8:52
    
Sorry, that's not enough information for me to work with. It looks to me like your JFrame has a faulty event listener or something in its init method. –  gpgekko Jan 14 at 8:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.