-1

I'm relatively new to java and have just started learning chow to connect mysql to java. This is my code to add details entered in java to a table in mysql:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
    String Name = t1.getText();     
    String Mobile = t2.getText();
    String Email = t3.getText();
    try
    { 
        Class.forName("java.sql.DriverManager");
        Connection con;
        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cbse", "Mahima" , "mahima");

        Statement stmt = (Statement) con.createStatement();
        String query = "INSERT INTO contact VALUES ('"+Name+"','"+Mobile+"', '"+Email+"');";

        stmt.executeUpdate(query);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

How to fix the error??

khakiout
  • 2,372
  • 25
  • 32
  • possible duplicate of [No suitable driver found for 'jdbc:mysql://localhost:3306/mysql](http://stackoverflow.com/questions/8146793/no-suitable-driver-found-for-jdbcmysql-localhost3306-mysql) - at least the answers there contain useful information how to solve the problem – mschenk74 Jul 29 '14 at 09:25
  • @mschenk74 no, it's not. The JDBC URI looks OK. – Stefano Sanfilippo Jul 29 '14 at 09:25
  • What error exactly? Please post the exact message. – Stefano Sanfilippo Jul 29 '14 at 09:26
  • 2
    `Class.forName("java.sql.DriverManager");` doesn't make any sense. Are you sure the MySQL JDBC driver is in the classpath? –  Jul 29 '14 at 09:26
  • @a_horse_with_no_name I know it's wrong, but `DriverManager` should be on the classpath nevertheless. So the error must be somewhere else (probably no connector in classpath). – Stefano Sanfilippo Jul 29 '14 at 09:27
  • 2
    The `DriverManager` is part of the JRE, it does not make any sense to load it dynamically - especially if you have a (hard) reference to it in your Java class. –  Jul 29 '14 at 09:29
  • 1
    possible duplicate of [Java connectivity with MySQL](http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql) – Mark Rotteveel Jul 29 '14 at 09:30
  • @a_horse_with_no_name read again: the `Class.forName` part is not likely the source of the error, because you certainly have that class in classpath. Since Java 1.6 you don't need to to explictly load JDBC drivers, so getting the wrong (but existent) class will produce no harm. I repeat, _I know it's wrong_, but that's not the source of the error. – Stefano Sanfilippo Jul 29 '14 at 09:34
  • BTW you are not closing any of your resources (connection, statement). Make sure your use try..finally < Java 7 or try-with-resources for Java => 7. A second note: you are vulnerable to sql-injection on this swing app. Use prepared statement (better yet use a SQL framework such as MyBatis or Spring JDBC) – raphaëλ Jul 29 '14 at 09:39
  • @StefanoSanfilippo: I am aware of that, but it still doesn't make sense to load that class. –  Jul 29 '14 at 09:41

3 Answers3

1

You probably need following:

  1. If you are working with MySQL ... you need MySQL's connectorJ and do following:

    Class.forName("com.mysql.jdbc.Driver");
    
  2. When you run the above code make sure that the MySQL's connectorJ (which can be downloaded from MySQL website) is available on classpath so that it can actually be loaded otherwise you will hit class not found exception

jsshah
  • 1,741
  • 1
  • 10
  • 18
  • 3
    Loading the driver with `Class.forName` is no longer necessary if the driver is JDBC 4.0 or higher. More likely the driver simply isn't on the classpath. – Mark Rotteveel Jul 29 '14 at 09:29
  • didnt know about the auto scanning in JDBC 4.0 ... I agree ... probably the driver is not found in classpath – jsshah Jul 29 '14 at 09:32
0

Change that line to that: Class.forName("com.mysql.jdbc.Driver");
And be sure that your jar is on the class path

-1

use com.mysql.jdbc.Driver in your Class.forName

CHANGE THIS

Class.forName("java.mysql.DriverManager");

TO THIS

Class.forName("com.mysql.jdbc.Driver");

nobalG
  • 4,544
  • 3
  • 34
  • 72