简体   繁体   中英

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

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

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

Change

   Connection con = DriverManager.getConnection(url);

to

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

and replace it with your username and password

确保您的应用程序类路径上具有MySQL驱动程序

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM