Reading:  

Getting started with JDBC


Example

JDBC – Create Database

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 = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");

      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);

      stmtmnt = conn1.createStatement();
      
      String sql = "CREATE DATABASE COMPANY";
      stmtmnt.executeUpdate(sql);
      System.out.println("successfully Database created...");
   }catch(SQLException se){
      // JDBC Handle errors 
      se.printStackTrace();
   }catch(Exception e){
      // Class.forName Handle errors 
      e.printStackTrace();
   }finally{
      //finally
      try{
         if(stmtmnt!=null)
            stmtmnt.close();
      }catch(SQLException se2){
      } 
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

 Select Database

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/COMPANY ";

   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");

      
      conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("database successfully Connected...");
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
} 

Drop Database

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 = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");
      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("database successfully Connected...");
      stmtmnt = conn.createStatement();
      
      String sql = "DROP DATABASE COMPANY ";
      stmtmnt.executeUpdate(sql);
      System.out.println("successfully Database deleted...");
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      
      e.printStackTrace();
   }finally{
            try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
   } 
} 

 Create Table

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/COMPANY";

   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      
      Class.forName("com.mysql.jdbc.Driver");     
      
      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
       stmtmnt = conn1.createStatement();
      
      String sql = "CREATE TABLE PROFILE_REG " +
                   "(C_id INTEGER not NULL, " +
                   " C_first VARCHAR(255), " + 
                   " C_last VARCHAR(255), " + 
                   " C_age INTEGER, " + 
                   " PRIMARY KEY (C_ id ))"; 

      stmt.executeUpdate(sql);
      
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      
      e.printStackTrace();
   }finally{
      
      try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

 Drop Table

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/ COMPANY ";
   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      
      Class.forName("com.mysql.jdbc.Driver");
      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      stmtmnt = conn.createStatement();
      
      String sql = "DROP TABLE PROFILE_REG ";
 
      stmtmnt.executeUpdate(sql);
      
   }catch(SQLException se){
      
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      //finally 
      try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }// do 
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }finally 
   }
}
}

Insert Record

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/ COMPANY ";

   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");

        Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      
      stmtmnt = conn.createStatement();
      
      String sql = "INSERT INTO PROFILE_REG " +
                   "VALUES (100, 'David', 'A', 18)";
      stmtmnt.executeUpdate(sql);
      sql = "INSERT INTO PROFILE_REG " +
                   "VALUES (101, 'Chan', 'F', 25)";
      stmtmnt.executeUpdate(sql);
      

   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }// do nothing
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
   
}
}

Select Record

import java.sql.*;

public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/ COMPANY ";

   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");
      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      
      stmtmnt = conn1.createStatement();

      String sql1 = "SELECT id, age ,first, last  FROM PROFILE_REG ";
      ResultSet rs1 = stmt.executeQuery(sql);
      
   while(rs1.next()){
         //Retrieve by column name
         int eid  = rs1.getInt("id");
         int eage = rs1.getInt("age");
         String efirst = rs1.getString("first");
         String elast = rs1.getString("last");

         //Display values
         System.out.print("ID: " + eid);
         System.out.print(", Age: " + eage);
         System.out.print(", First: " + efirst);
         System.out.println(", Last: " + elast);
      }
      Rs1.close();
   }catch(SQLException se){
            se.printStackTrace();
   }catch(Exception e){
            e.printStackTrace();
   }finally{
      //finally      try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }// do 
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end 
   }//end 
}//end 
}//end 

 Delete Record

import java.sql.*;

public class JDBCExample {
   
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/ COMPANY ";
   
   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");

      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      stmtmnt = conn1.createStatement();
      String sql = "DELETE FROM PROFILE_REG " +
                   "WHERE id = 101";
      stmtmnt.executeUpdate(sql);

      sql = "SELECT id, first, last, age FROM PROFILE_REG ";
      ResultSet rs1 = stmt.executeQuery(sql);

      while(rs1.next()){
         int eid  = rs1.getInt("id");
         int eage = rs1.getInt("age");
         String efirst = rs1.getString("first");
         String elast = rs.1getString("last");

         System.out.print("ID: " + eid);
         System.out.print(", Age: " + eage);
         System.out.print(", First: " + efirst);
         System.out.println(", Last: " + elast);
      }
      Rs1.close();
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
      try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      }// do 
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

 

Using Where Clause

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/COMPANY";

   static final String USER = "username1";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   Statement stmtmnt = null;
   try{
      
      Class.forName("com.mysql.jdbc.Driver");
      Conn1 = DriverManager.getConnection(DB_URL, USER, PASS);
      stmtmnt = conn.createStatement();
      String sql = "SELECT id, first, last, age FROM PROFILE_REG ";
      ResultSet rs1 = stmt.executeQuery(sql);

      while(rs1.next()){
         Int eid  = rs.getInt("id");
         int eage = rs.getInt("age");
         String efirst = rs.getString("first");
         String elast = rs.getString("last");

         System.out.print("ID: " +eid);
         System.out.print(", Age: " + eage);
         System.out.print(", First: " + efirst);
         System.out.println(", Last: " + elast);
      }

      sql = "SELECT id, first, last, age FROM PROFILE_REG " +
                   " WHERE id = 101 ";
      Rs1 = stmt.executeQuery(sql);

      while(rs1.next()){
         int eid  = rs1.getInt("id");
         int eage = rs1.getInt("age");
         String efirst = rs1.getString("first");
         String elast = rs1.getString("last");

         System.out.print("ID: " + eid);
         System.out.print(", Age: " + eage);
         System.out.print(", First: " + efirst);
         System.out.println(", Last: " + elast);
      }
      Rs1.close();
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
try{
         if(stmtmnt!=null)
            conn1.close();
      }catch(SQLException se){
      } 
     try{
         if(conn!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
} } }

Where to go from here?

Go read on JDBC documentation from Oracle.com  and if you have any specific questions, don't be afraid to ask on Stackoverflow

Description

This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts

  1. Introduction
  2. SQL Syntax
  3. Setting up Environment
  4. Getting started with some samples
  5. Connecting to database
  6. Statements
  7. Result Sets
  8. Data Types
  9. Transactions
  10. Exception handling
  11. Batch Processing
  12. Streaming Data
  13. More JDBC Examples

Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected



Prerequisites

Understanding of Java language a must to understand various things in this tutorial

Audience

Absolute beginners who wants to get started with JDBC

Author: Subject Coach
Added on: 8th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!