Click here to Skip to main content
12,698,600 members (30,695 online)
Rate this:
Please Sign up or sign in to vote.
i have just installed mysql server 5 and NetBeans 7.1

i want to know how to use java to connect to database on mysql server

it is a code i found on net

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package database_con;
import java.sql.*;
 * @author engamir
public class Database_con {

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("MySQL Connect Example.");
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "test";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root"; 
        String password = "globalamirtarek";
            conn = DriverManager.getConnection(url+dbName,userName,password);
            System.out.println("Connected to the database");
            System.out.println("Disconnected from database");
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) 

this code enter the catch with ClassNotFoundException

i can find this Driver under Drivers list under services on Netbeans and i can connect using it to the mysql database

can any one tell me if i make something wrong?
Posted 4-Jul-12 2:11am
Updated 4-Jul-12 3:45am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

looks good spontaneous.

When you get a ClassNotFound Exception - which class is not found?

Please make sure that all parts are in the build:

Configuring the build in Netbeans[^]
amir tarek 5-Jul-12 5:59am
this is the class of the driver

i solved it

i added the jar file of com.mysql.jdbc.Driver to the libraries of the project and it works


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100