Wednesday, June 23

Connect MySql Server using JDBC

mysql-connector-java-3.1.10 is JDBC connector for MYSQL database. Place the mysql-connector-java-3.1.10-bin.jar file in your CLASSPATH variable or Application folder.

Example below makes connection to mySQL using com.mysql.jdbc.Driver.

import java.sql.*;

public class getmySQLConnection 
{
    public static void main(String[] args
    {
      DB db = new DB();
      Connection conn=db.dbConnect(
      "jdbc:mysql://localhost:3306/test""root""");
    }

}

class DB
{
    public DB() {}

    public Connection dbConnect(String db_connect_string,
    String db_userid, String db_password)
    {
      try
      {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         Connection conn = DriverManager.getConnection(
         db_connect_string, db_userid, db_password);
      
         System.out.println("connected");
         return conn;
                        
      }
      catch (Exception e)
      {
        e.printStackTrace();
        return null;
      }
   }
};
-----------------------------------------------------------------------------------------------
Again for more reference and detailed info by video... Click Here 

Note: Read Below Only if you have seen the Video ....

You may create database by java console also....but it is better for first time that you create 
database in mysql first.


 Later,
Aditya

No comments:

Post a Comment