title: Java-Mysql - Access denied for user 'root'@'localhost' (using password: YES)
body: <p>I am using the fllowing code and get this error:</p>
  
  <pre><code>Access denied for user 'root'@'localhost' (using password: YES)
  java.lang.NullPointerException
  </code></pre>
  
  <p>I used several solutions from the stackoverflow. But the problem is not resolved! Please provide your solution.</p>
  
  <p><strong>Inserts only once, but then I came across the above error.</strong></p>
  
  <p><strong>My Java code:</strong></p>
  
  <pre><code>package DBConnection;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import com.mysql.jdbc.PreparedStatement;
  
  public class Main {
  
      public static void main(String[] args) throws Exception {
          insert();
      }
  
      public static void insert() throws Exception{
          final String var1 = "test1";
          final String var2 = "تست2";
          final String var3 = "test.com";
          try{
              Connection conn = getConnection();
              PreparedStatement inserted = (PreparedStatement) conn.prepareStatement("INSERT INTO wpsjim_target_sites (site_name_en,site_name_fa,site_address) VALUES('"+var1+"','"+var2+"','"+var3+"')");
              inserted.execute();
          }catch(Exception e){
              System.out.println(e);
          }finally{
              System.out.println("Insert Completed!");
          }
      }
  
      public static Connection getConnection() throws Exception{
          try{
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost:3306/simjin";
              String username = "root";
              String password = "rootpass";
              Class.forName(driver); 
              Connection conn = DriverManager.getConnection(url,username,password);
              System.out.println("Connected");
              return conn;
          } catch(Exception e){
              System.out.println(e.getMessage());
          } 
          return null;
      }
  }
  </code></pre>
  
  <p><em>Update:</em> 
  <strong>When i use <code>e.printStackTrace(System.out);</code> display the fllowing messages on console:</strong></p>
  
  <pre><code>    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
      at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
      at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:795)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:44)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at DBConnection.Main.getConnection(Main.java:34)
      at DBConnection.Main.insert(Main.java:17)
      at DBConnection.Main.main(Main.java:9)
  java.lang.NullPointerException
  </code></pre>