Monday, April 6, 2015

JDBC Code

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

public class JDBCExample {

public static void main(String arg[]) {

Connection connection = null;
//Register the driver class
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("MySQL JDBC Driver Registered!");
        //Creating connection
            //  driver     //service url   / DBName , username, pwd
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "test");
            System.out.println("SQL Connection to database established!");

       
        }
        catch (ClassNotFoundException e) {
            System.out.println("MySQL JDBC Driver not found !!");
            return;
        }
        catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            return;
        } finally {
            try
            {
                if(connection != null)
                    connection.close();
                System.out.println("Connection closed !!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
}

}

No comments:

Post a Comment