Saturday, 22 October 2016

JDBC Program using Java & MySQL DB

import java.sql.*;

public class JDBCExample
{


   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost/";


   static final String USER = "username";
   static final String PASS = "password";
 
   public static void main(String[] args)
   {
   Connection conn = null;
   Statement stmt = null;

   try
   {

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

      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
   
       con.setAutoCommit(false);

      System.out.println("Creating database...");
      stmt = conn.createStatement();
     
      String sql = "CREATE DATABASE STUDENTS";
      stmt.executeUpdate(sql);
      System.out.println("Database created successfully...");

    }
     catch(SQLException se)
    {
      se.printStackTrace();
     con.rollback();
   }
   
     catch(Exception e)
     {
      e.printStackTrace();
    }

    finally
      {
      try
        {
         if(stmt!=null)
            stmt.close();
      }
      catch(SQLException se2)
      {
      }

      try
      {
         if(conn!=null)
            conn.close();
      }
        catch(SQLException se)
       {
         se.printStackTrace();
      }
   }
}
}

No comments:

Post a Comment