Java Database Connectivity using MySQL server

 Create static connection using MySQL server

Select your project in figure

    Step: 1



Step: 2

    Your Project Right Click and choose New option



Step: 3

    Now Click Java Class...



Step: 4

    Choose class name

   example: dbconnnection

and Last click to finish



Step: 5

    Write Code in java create connection

    import java.sql.*; //import sql packages
public class dbconnection 
{
    private static Connection conn;
    static
    {
        try
        {
             //Connection Driver Name
            Class.forName("com.mysql.jdbc.Driver"); 
                //Connection url("jdbc:mysql://localhost:3306/your database name","your user name","Your password");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/postoffice","root","");
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    public static Connection getConnection()
    {
        return conn;
    }
}


Example
postoffice is my database name
and root my user name
and mypassword null

No comments:

Post a Comment