Write a java program establish a JDBC connection


Write a java program establish a JDBC connection

Week   12

Aim: To  Write a java program establish a JDBC connection, create a table student with properties name, register number, mark1,mark2, mark3. Insert the values into the table by using the java.

Description: Java Database Connectivity (JDBC) is an application program interface (API) specification for connecting programs written in Java to the data in popular databases. The application program interface lets you encode access request statements in Structured Query Language (SQL) that are then passed to the program that manages the database. It returns the results through a similar interface. JDBC is very similar to the SQL Access Group’s Open Database Connectivity (ODBC) and, with a small “bridge” program, you can use the JDBC interface to access databases through the ODBC interface. For example, you could write a program designed to access many popular database products on a number of operating system platforms. When accessing a database on a PC running Microsoft’s Windows 2000 and, for example, a Microsoft Access database, your program with JDBC statements would be able to access the Microsoft Access database.JDBC actually has two levels of interface. In addition to the main interface, there is also an API from a JDBC “manager” that in turn communicates with individual database product “drivers,” the JDBC-ODBC bridge if necessary, and a JDBC network driver when the Java program is running in a network environment (that is, accessing a remote database).

When accessing a remote database, JDBC takes advantage of the Internet’s file addressing scheme and a file name looks much like a Web page address (or Uniform Resource Locator). For example, a Java SQL statement might identify the database as:

Source code :

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/STUDENTS”;

   //  Database credentials

   static final String USER = “username”;

   static final String PASS = “password”;

   public static void main(String[] args) {

   Connection conn = null;

   Statement stmt = null;

   try{

      //STEP 2: Register JDBC driver

      Class.forName(“com.mysql.jdbc.Driver”);

      //STEP 3: Open a connection

      System.out.println(“Connecting to a selected database…”);

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

      System.out.println(“Connected database successfully…”);

      //STEP 4: Execute a query

      System.out.println(“Creating table in given database…”);

      stmt = conn.createStatement();

      String sql = “CREATE TABLE STUDENT ” +

                   “(RollNo INTEGER, ” +

                   ”  Name VARCHAR(255), ” +

                   ” Marks1 INTEGER ” +

                   ” Marks2 INTEGER, ” +

            ” Marks3 INTEGER, “)”;

      stmt.executeUpdate(sql);

      System.out.println(“Created table in given database…”);

String sql1=”INSERT INTO STUDENTS (RollNo,Name,Marks1,Marks2,Marks3) values (1,”a”,21,23,34)”;

stmt.executeUpdate(sql1);

      System.out.println(“data inserted into table”);

   }catch(SQLException se){

      //Handle errors for JDBC

      se.printStackTrace();

   }catch(Exception e){

      //Handle errors for Class.forName

      e.printStackTrace();

   }finally{

      //finally block used to close resources

      try{

         if(stmt!=null)

            conn.close();

      }catch(SQLException se){

      }// do nothing

      try{

         if(conn!=null)

            conn.close();

      }catch(SQLException se){

         se.printStackTrace();

      }//end finally try

   }//end try

   System.out.println(“Goodbye!”);

}//end main

}//end JDBCExample

Write a java program establish a JDBC connection

Write a java program establish a JDBC connection

https://en.wikipedia.org/wiki/Java_Database_Connectivity

https://en.wikipedia.org/wiki/JDBC_driver

https://en.wikipedia.org/wiki/Database_connection

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories