0

I am just a beginner in java and working on core java....not on any tool such as Netbeans or Eclipse. It is simply notepad based and running on cmd. I am providing a simple code for connecting with the database and performing basic operations. I am getting a continuous exception - ClassNotFoundException when I'm using com.mysql.jdbc.Connection and when I use java.sql.DriverManager instead of that..I get another error with No suitable driver found for jdbc:.... Please help me out guyz..I'm stuck with this...I have done the same with Netbeans..it worked perfectly there...but not with cmd.

     import java.sql.*;
     public class JDBCExample
     {
        static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
        static final String DB_URL="jdbc:mysql://localhost/company";
        static final String USER="root";
        static final String PASSWORD="root";
            public static void main(String args[])
            {
              Connection conn=null;
              Statement stmt=null;
             try
             {
               Class.forName("com.mysql.jdbc.Driver");  //tried java.sql.DriverManager as well
        System.out.println("Connecting to Database...");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","root");
        System.out.println("Creating Statement...");
        stmt=conn.createStatement();
        String sql="Update login set password=123 where id=1";
        Boolean ret=stmt.execute(sql);
        System.out.println("Return value is: "+ret.toString());
        int rows=stmt.executeUpdate(sql);
        System.out.println("Rows impacted: "+rows);
        sql="Select *from login;";
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next())
        {
            String id=rs.getString("id");
            String user=rs.getString("username");
            String pass=rs.getString("password");
            System.out.println("ID: "+id);
            System.out.println("username: "+user);
            System.out.println("password: "+pass);
        }
        rs.close();
        stmt.close();
        conn.close();
    }
    catch(SQLException se)
    {
        se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            if(stmt!=null)
                stmt.close();
        }
        catch(SQLException se2)
        {

        }
        try
        {
            if(conn!=null)
                conn.close();

        }
        catch(SQLException se)
        {
            se.printStackTrace();
        }
    }
    System.out.println("End");
}

}

  • 1
    Is `jdbc.jar` on your classpath? Why don't you just use Netbeans if it works? – Karl Reid Apr 21 '17 at 19:01
  • 1
    May be the jar files are missing in the classpath. Are you setting the classpath while running through command. Could you provide the cmd that you are running? – PKR Apr 21 '17 at 19:01
  • @KarlReid I want to learn Java from the basics...that's why I'm trying to work with Core Java and Netbeans doesn't let user know about its deep functionalities. – Shweta Gupta Apr 21 '17 at 19:06
  • @PKR I have set the classpath to the mysql-connector5.1.*.jar......and please tell me how can I provide you the cmd that i am running... – Shweta Gupta Apr 21 '17 at 19:07
  • @DanielBickler I've gone through you link...may be the question is same...but the answers are not solving my problem – Shweta Gupta Apr 21 '17 at 19:08
  • You need to understand what a classpath is. Consider the oracle tutorial. – Thorbjørn Ravn Andersen Apr 21 '17 at 19:08
  • Just copy and paste the command you are entering into the command prompt, the command that gives you the `ClassNotFoundException`. – Karl Reid Apr 21 '17 at 19:11
  • @AlanHay i've tried that as well... I am getting another error in that case..."Could not find or load Main Class" – Shweta Gupta Apr 21 '17 at 19:13
  • @KarlReid ...Sorry I didn't understand how can I run a particular command directly on cmd.... – Shweta Gupta Apr 21 '17 at 19:15

0 Answers0