Addman
Addman

Reputation: 341

Connecting to MySQL Database from java in IDE Netbeans

So this is my thing. I am trying to connect do MySQL Database from java. I have downloaded connector driver from MySQL ( Its called "mysql-connector-java-5.0.8-bin.jar" ) and I added to my libraries (Im using NetBeans). I tried to do simple connection like this:

package datacon;

public class Datacon {

    public static void main(String[] args) {

        try {

            java.sql.Connection con = (java.sql.Connection) java.sql.DriverManager
                .getConnection("jdbc:mysql://localhost:3306/test"
                /*, "root"
                  , "root" */ );

        } catch ( Exception e ) {
            System.out.println( e.toString() );
        }
    }
}

But this hapened:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

I made my research on the internet and this pretty common thing, but none of the answers helped me. I was able connect to database through NetBeans/services, so that URL jdbc:mysql://localhost:3306/test should be correct.

I am using:

java:   Oracle java SDK 1.7.0 _ 45
IDE:    NetBeans 7.4
OS:     Debian 3.2.51-1 x86_64
Driver: MySQL Connector/J 5.0.8

I am afraid this is gonna have a very trivial answer, but I am stuck here for a while now and i need to move. So what am I missing?

Upvotes: 0

Views: 30929

Answers (5)

Tiago NET
Tiago NET

Reputation: 153

My class for connection:

package connection;

import java.sql.Connection;
import java.sql.DriverManager;   
import java.sql.SQLException;

public class ConnectionFactory {  
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/<database>", "<user>", "<password>");
    }        
}   

Don't need add ClassForName. This is necessary in older versions of java.

Upvotes: 0

user3441712
user3441712

Reputation: 1

try { class.forname("com.mysql.jdbc.Driver");

     driver.getconnection("connenctionurl","name","password");

            Statement s =goodrecive.asif().createStatement();
            s.executeUpdate("INSERT INTO product(productno,productname,quantity,ammount)values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"')");

    } catch (Exception e) {
        System.out.println(e);
    }
}                         

Upvotes: 0

KhAn SaAb
KhAn SaAb

Reputation: 5366

I thing you forget to load driver .

Class.forName("com.mysql.jdbc.Driver");

and make sure you have mysql-connector-java-5.1.22-bin in your class path.

Upvotes: 1

Tatarao voleti
Tatarao voleti

Reputation: 513

try {
      Class.forName("org.gjt.mm.mysql.Driver");
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    } catch(ClassNotFoundException e) {
        e.printStackTrace();
    }

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42010

Add:

static {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (final ClassNotFoundException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions