1

I am making a standalone database application in which I use netbeans for java and a mysql database as my server.

Inserting basic values into my database tables fails with an exception "no jdbc driver found for jdbc:mysql:\localhost\basicinfo" where in basicinfo is my database name with "info" as my database table. My code:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class class1 
{
 public static void main(String[] args )
 {
     try
     {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con=DriverManager.getConnection("jdbc:mysql:\\localhost\basicinfo","root","root");
         Statement stmt=(Statement)con.createStatement();

         String name="Jerome Dcruz";
         String contactno="9773523568";

       String insert="INSERT INTO info VALUES('"+name+"','"+contactno+"');";
       stmt.executeUpdate(insert);



     }
     catch(Exception e)
     {
         JOptionPane.showMessageDialog(null, e.getMessage() ,"Error", 1);

     }
 }
}          
nikoshr
  • 32,926
  • 33
  • 91
  • 105
DJerome
  • 19
  • 1
  • 1
  • 3

2 Answers2

0

you should have the mysql-connector-java driver int C:\Program Files\Java\jdk1.7.0_25\jre\lib\ext

and also place the port number of your database like the following

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/basicinfo", "root", "root");

you can download the driver from here

Muhammad
  • 6,725
  • 5
  • 47
  • 54
  • 2
    Whilst this would work, placing jars in the ext folder is not a great solution as it would restrict you from using a different mysql driver in another project should you need to. There are other ways of adding the jar to the classpath that are more flexible, and this would depend on how the project is setup. For example adding dependencies to a Maven or gradle project, or by configuring external libraries in the Netbeans project. – Romski Oct 31 '14 at 05:46
  • @Romski Thanks for your suggestion, I will like to know the other way you want to suggest :) – Muhammad Nov 01 '14 at 13:29
  • 1
    I have mentioned 3 possibilities, what specifically are you asking? – Romski Nov 03 '14 at 03:17
0

Error Found at (line #15) :

Connection con = DriverManager.getConnection("jdbc:mysql:\\localhost\basicinfo", "root", "root");

Correct it to :

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/basicinfo", "root", "root");

Conclusion : There was an error('\' instead of '/') in path and as such, it was impossible to find the specified schema and table.

KNU
  • 2,560
  • 5
  • 26
  • 39