-1

I am trying to connect to mysql database and that is not done still because of the error,My code is as follows:

public static void main(String[] args) {
    try{
        String url = "jdbc.mysql://localhost:3306/db";

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url,"root","root");
        Statement stmt = conn.createStatement();
        String query = "insert into student values(1,abs)";
        stmt.executeQuery(query);
        System.out.println("Success...");
        conn.close();

    }catch(Exception e){
        e.printStackTrace();
    }

And the error is as follows:

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 Java_Database.main(Java_Database.java:11)

So anyone know well please help me.

11
  • this a servlet engine issue or not Commented Aug 26, 2015 at 14:12
  • no this is not servlet issue... Commented Aug 26, 2015 at 14:14
  • possible duplicate of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. Commented Aug 26, 2015 at 14:15
  • see this tutorial, you will find a link to the driver there! Commented Aug 26, 2015 at 14:17
  • the file name will be something like mysql-connector-java-5.1.35-bin.jar Commented Aug 26, 2015 at 14:19

3 Answers 3

2

1: download that msi you referred to @ http://dev.mysql.com/downloads/connector/j/

2: run it. after it runs the install app vanishes, not exactly a roadmap for success for an install routine

  1. from the root of c:\ I issue: dir mysql-connector-java-5.1.36-bin.jar /s

I let it run the whole way thru to confirm I didn't already have it installed elsewhere

C:\>dir mysql-connector-java-5.1.36-bin.jar /s

 Directory of C:\Program Files (x86)\MySQL\MySQL Connector J

06/19/2015  09:26 PM           972,009 mysql-connector-java-5.1.36-bin.jar
               1 File(s)        972,009 bytes

     Total Files Listed:
               1 File(s)        972,009 bytes

Seems consistent with my 5.1.35 filesize I was using before stumbling into your question

Btw, the above MySql Connect J folder date was just created so I am sure that was from the install

  1. I copy (not moving it) it to my c:\javadeps folder.

  2. I have a database called so_gibberish, and a table called thingws with 3 rows in it

  3. source code (myTest.java): I poached this stub off the internet as I am mostly a scala/jvm programmer. So please forgive. But it works.

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger;

    public class myTest {

    public static void main(String[] args) {
    
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
    
        String url = "jdbc:mysql://localhost:3306/so_gibberish"; // **** MODIFY db name @ end
        String user = "stan"; // **** MODIFY
        String password = "stan_password"; // **** MODIFY
    
        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            System.out.println("------------------------------");
            rs = st.executeQuery("select version()");
    
            if (rs.next()) {
                System.out.println(rs.getString(1));
                System.out.println("------------------------------");
           }
            rs = st.executeQuery("select id,myCode from thingws");
            while (rs.next()) {
                System.out.println(rs.getInt(1)+": "+rs.getString(2));
            }
            System.out.println("------------------------------");    
    
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(myTest.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
    
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }
    
            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(myTest.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
    

    }

I will save it in C:\dev\java8\quick_java_mysql_test

  1. compile and run (when you run it, it does a query for the mysql version, then a query on that table getting 3 rows)

c:\dev\java8\quick_java_mysql_test>javac myTest.java

c:\dev\java8\quick_java_mysql_test>java -cp .;c:\javadeps\mysql-connector-java-5.1.36-bin.jar myTest

output is:

------------------------------
5.6.24-log
------------------------------
1: C938CA
2: XYZ123
3: XYZPDQ
------------------------------

It is common to have a dependencies folder for jars at the project-level, such as a dep directory under the project folder.

Though I have that same jar file in it, it is not referenced, as seen in the -cp directive in step 7

that picks the jar up in c:\javadeps

plan your strategy according, and good luck

Sign up to request clarification or add additional context in comments.

Comments

1

The mysql driver jar is not in the classpath.

Download it and copy it to a location visible from the classpath.

Here the link to the official driver.

7 Comments

which type of driver is this ?
I downloaded that file and installed but still it does not resolve my problem.
Where do you put the .jar? You added it to a directory visible from the classpath? Or have you manually modified the classpath to read that jar?
I dont put any .jar file I had just download "Windows (x86, 32-bit), MSI Installer" and installed it but I didn't get what to do now.!
The .msi is a microsoft installer. When you run it it creates some directory and copy the jar in that directory. It is better to download the platform independent version. You will easily find the jar.
|
1

You are using JDBC driver/connector for MySQL in your code.

You may download from below mentioned link.

https://www.mysql.com/products/connector/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.