George Marin
George Marin

Reputation: 395

Java database connection issue

I have the following issue with my code :

import java.sql.*;

public class App {

    public static void main(String[] args){

        String url = "jdbc:mysql://localhost:3306" ;
        try{ 
            Class.forName("com.mysql.jdbc.Driver"); 
            }
        catch(ClassNotFoundException e) 
                { System.out.println("Eroare incarcare driver!\n" + e);
                return; 
                }
        try{ 
            Connection con = DriverManager.getConnection(url);

        // Golim tabelul persoane 
                String sql = "DELETE FROM persoane"; 
                Statement stmt = con.createStatement(); 
                stmt.executeUpdate(sql);
                stmt.execute("CREATE DATABASE IF NOT EXISTS test");
                stmt.execute("USE test");

i get the exception...any idea how i can make this work? thx.

enter code here

Upvotes: 0

Views: 62

Answers (4)

Sarz
Sarz

Reputation: 1976

java.lang.ClassNotFoundException occured due to "class not found" in your project/war/ear. Exception is very self explanatory, How to solve it. In your case:

Add com.mysql.jdbc.Driver driver class/jar in your build/deployment/lib path you can download it HERE

Read here Offical

Upvotes: 1

Raju Guduri
Raju Guduri

Reputation: 1307

Change

   Connection con = DriverManager.getConnection(url);

to

 Connection con = DriverManager.getConnection(url,"username","password");

and replace it with yourusername and password

Upvotes: 1

jesse
jesse

Reputation: 28

Make sure you have the MySQL driver on your application classpath.

Upvotes: 1

Promination
Promination

Reputation: 156

You need to download and add the jdbc connector to your classpath.

http://dev.mysql.com/downloads/connector/j/

Upvotes: 2

Related Questions