Friday, September 09, 2011

Java MYSQL Connection....

MyConnection .java

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

public class MyConnection
{
   
    public static Connection getConnection()
    {
        Connection con=null;
        String driver = "com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/test";
        //String url="jdbc:mysql://10.50.4.5:3306/test";
        String userName="root";
        String password="piyu";
       
       
        /*String driver = "oracle.jdbc.driver.OracleDriver";
        String url="jdbc:oracle:thin:@localhost:1521:orcl"; Or
        String url="jdbc:oracle:thin:@10.50.4.5:1521:orcl";
        String userName="system";
        String password="user";*/
       
       
        try
        {
            Class.forName(driver);
            con=DriverManager.getConnection(url, userName, password);
            if(con!=null)
            {
                System.out.println("Connection Successfulll...");
            }
            else
            {
                System.out.println("Connection Failed....");
            }
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        return con;
    }

}
Note : This File Required mysql-connector-java-5.1.7-bin.jar File So Download it and enjoying MYSQL Database Connection.

No comments:

Post a Comment