Selasa, 07 Februari 2012

Database Connection With Java

 Database Connection With Java

guys, the following script in java to connect to the Database; please try:

Here's how:

copy the following script

package blog;
import java.sql.*;
/**
 *
 * @author mzherri
 */
public class database {

    public static void main(String args[]){

        String url = "jdbc:mysql://localhost:3306/test";

        // url address match with the name of your Database
        // jdbc:mysql://localhost:3306/name of your database
        String user = "root";
       // user name of your database
       String pass = "root";
      // password of your database

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

            // connection with your MySQL
            Connection con = DriverManager.getConnection(url, user, pass);

           // connection with your database
            System.out.println("Connection was succesfull");

           // connection succes
        }catch(ClassNotFoundException ex){
            System.out.println("Driver not found "+ex);

           // dirver not found in your project
        }catch(SQLException ex){
            System.out.println("Connection failed "+ex);

           // connection to your database failed
        }


    }

}


please add library for your project : 



and choose MySQL JDBC DRIVER, next clik add library


Try to "run" another source code or you can type SHIFT+F6. Should now connect and appears the information
"run:
Connection was succesfull
BUILD SUCCESSFUL (total time: 1 second)"

Tidak ada komentar:

Posting Komentar