0

I am trying to connect to data base.Earlier it was working fine. but now some jars has got deleted or something I don't know (red cross were coming on them). But I downloaded all jars again (mysql-connector,commons-io-2.4)still I am getting classnotfound exception at Class.forName("com.mysql.jdbc.Driver"); Here is my code

    package abc;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

     public class JDBC_oracle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //step1 load the driver class  
    Class.forName("com.mysql.jdbc.Driver");
    //step2 create  the connection object  
    Connection  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
    //step3 create the statement object  
    Statement statement = connection.createStatement();
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query");       
    while(rs.next())  
        System.out.println("I am triyng to print data");
//step5 close the connection object 
    connection.close();  
}
  }

This is error

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at deiniteguide.JDBC_example.main(JDBC_example.java:24)

I have tried using try catch also it is not working for me.

monil
  • 47
  • 2
  • 14
  • 1
    The mysql-connector.jar file is not on the classpath. Unrelated, but `Class.forName()` is not needed any more. The JDBC drivers auto-register, as long as they are on the classpath. – Andreas May 21 '16 at 07:01

1 Answers1

1

If you are using netbeans, you can go to your library folder in your project. Right click and add library and select mysql driver. Sorry couldn't comment due to less reputation and it would be easier if you can post with what error message you get. Also use try catch in your program so that it will be easier to locate the error. Your code can be like this:

  public class JDBC_oracle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //step1 load the driver class  
     try{
    Class.forName("com.mysql.jdbc.Driver");
       }catch(Exception e){
         System.out.println("Class not found" +e);
    //step2 create  the connection object  
     try{
    Connection  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
     }catch(Exception es){
     System.out.println("connection couldn't be made "+es);
      }
    //step3 create the statement object  
     try{
    Statement statement = connection.createStatement();
    statement.executeUpdate("sql_query"); 
    ResultSet rs=statement.executeQuery("sql_query");   }
     catch(Exception er){
      System.out.println("Query wasnot executed "+er);    
     }
    while(rs.next())  
        System.out.println("I am triyng to print data");
//step5 close the connection object 
    connection.close();  
}
  }
Bishal Gautam
  • 380
  • 3
  • 16