0

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
3
  • it`s the exception. Eroare incarcare driver! java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Commented Mar 16, 2015 at 12:57
  • Use url as jdbc:mysql://localhost:3306/YOUR_DB Commented Mar 16, 2015 at 12:59
  • add mysql-connector-java-5.1.4.jar into the classpath. Commented Mar 16, 2015 at 13:01

4 Answers 4

2

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

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

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

Comments

1

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

Comments

1

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

Comments

1

Change

   Connection con = DriverManager.getConnection(url);

to

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

and replace it with yourusername and password

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.