title: Java Netbeans DB Connection error
body: <p>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:</p>
  
  <pre><code>    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();
      }
  
  
  }
  </code></pre>
  
  <p>After debugging the project the console gives the result as </p>
  
  <blockquote>
    <p>BUILD SUCCESSFUL (total time: 0 seconds) 
    But still it is showing some errors as below:</p>
  </blockquote>
  
  <pre><code>    Jan 14, 2014 1:47:13 PM MyDb.utils.DbConnection &lt;clinit&gt;
  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.&lt;init&gt;(ConnectionImpl.java:774)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(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.&lt;clinit&gt;(DbConnection.java:20)
      at desktopapplication1.NewJFrame.&lt;init&gt;(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)
  </code></pre>