0

So I want to retrieve one row of the table but first I want to make it works and then I will do the rest, so below is the code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import java.util.*;
import java.sql.*;


public class ExampleForDB {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        try{  
            Class.forName("com.mysql.jdbc.Driver");  
            Connection con=DriverManager.getConnection(  
            "jdbc:mysql://localhost:3306/******","*****","******"); 

                System.out.print("Successful connection with MySQL!");
            //here sonoo is database name, root is username and password  
            Statement stmt=con.createStatement();  
            ResultSet rs=stmt.executeQuery("select Number1 from lottery1955-00 where id=1");  
            System.out.println(rs.getInt(1)+"  "+rs.getString(1));  
            con.close();  
        }catch (SQLException e ) {
              e.printStackTrace();
          } 
            }  


    }

And here you can see my error I tried so many different ways that I found here in StackOverflow but none of them worked for me, can you share your experience? see the error below

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    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 ExampleForDB.main(ExampleForDB.java:16)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Patrahi
  • 3
  • 1
  • 4
  • You don't have the MySQL Connector/J JDBC driver on the class path. Add it, see linked duplicate for more information. – Mark Rotteveel May 12 '18 at 12:19
  • Man if the existing answer in StackOverflow does not help me then can you explain me, how it is the same case? – Patrahi May 12 '18 at 13:17
  • Have you followed the steps in [this answer](https://stackoverflow.com/a/2840358/466862)? It exactly addresses your problem (specifically step 2). In any case, I have added two other questions that address the same problem. – Mark Rotteveel May 12 '18 at 15:15
  • I solved my problem and it was not the same as it is written in the examples you gave me, and because you closed my question now no one will understand how to solve easily the problem that I got. Sometimes the moderators are too fast with their decisions to cancel some question. – Patrahi May 12 '18 at 15:24
  • 1
    Well, could you describe/explain how you solved your problem, if it is really a different solution I'll reopen it so you can add your own answer. Also, I'm not a moderator, moderators have a diamond next to their name. – Mark Rotteveel May 12 '18 at 15:42
  • Basically I copied a file Referenced Libraries with msql-connector.jar inside of this project rebuild it and it worked. – Patrahi May 13 '18 at 12:51
  • And that means that you didn't have the driver on classpath, which is what the linked duplicates also specify. – Mark Rotteveel May 13 '18 at 12:59

0 Answers0