-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.

Abhijit Kumbhar
  • 923
  • 3
  • 23
  • 49

3 Answers3

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

Drew
  • 24,851
  • 10
  • 43
  • 78
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.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • which type of driver is this ? – Abhijit Kumbhar Aug 26 '15 at 14:13
  • I downloaded that file and installed but still it does not resolve my problem. – Abhijit Kumbhar Aug 26 '15 at 15:16
  • 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? – Davide Lorenzo MARINO Aug 26 '15 at 15:22
  • 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.! – Abhijit Kumbhar Aug 26 '15 at 15:28
  • 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. – Davide Lorenzo MARINO Aug 26 '15 at 15:40
  • Oh !!! After your suggestion I once again visited this thread [link](http://dev.mysql.com/downloads/connector/j/) and from their I download platform independent version **Platform Independent (Architecture Independent), ZIP Archive** and then I added **mysql-connector-java-5.1.36-bin.jar** file to eclipse and after correcting some errors my project works fine :) thank you @Davide Lorenzo MARINO – Abhijit Kumbhar Aug 26 '15 at 15:46
  • Generally if you need to download something related to java consider to download always the independent version instead of the microsoft version. The independent is always a .zip and you can decide where to copy it. The automatic microsoft installer generally is not always clear about where the data are copied. Additionally the MSI sometimes generates icons and startup shortcuts but leave you less flessibility. – Davide Lorenzo MARINO Aug 26 '15 at 15:49
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/

seahawk
  • 1,872
  • 12
  • 18