Share with others


Assignment No 05


AIM:


Write a program in Java that enters student details (Roll No, Name etc) and retrieves information. Use Access as a database and write the application in JDBC. (AWT or JFame)


THEORY:


JDBC stands for Java Database connectivity’s. It is a software layer that allows developers to write real client-server projects in Java. JDBC is based on the X/OPEN call level interface (CLI) for SQL. JDBC was designed to be a very compact, simple interface focusing on the execution of raw SQL statements and retrieving the results. The components of JDBC are Application, Driver manager and Driver.


JDBC   Vs ODBC


  • ODBC cannot be directly used with Java because it uses a C interface.

  • ODBC makes use of pointers which have been totally removed from Java.

  • ODBC mixes simple and advanced features together and has complex options for simple queries.

  • ODBC requires manual installation of the ODBC driver manager and driver on all client machines. JDBC drivers are written in Java and JDBC code is automatically installable, secure and portable on all java platforms from network computers to mainframes.


The JDBC API defines a set of interfaces and classes to be used for communications with a database. These interfaces and classes are found in the java.sql package. The results of the SQL statements are stored in ResultSet object and getXXX method used to retrieve the data from ResultSet. A transaction is set of one or more statements that are executed together as a unit, so either all of the statements are executed or none of the statement is executed.


DSN: Types of DSN


  • File DSN  Accessibility is very low
  • System DSN – Easy to access and by all users.
  • User DSN  Specific to a user but not portable.



Steps for using JDBC:


//step 1- import the java.sql package

import java.sql.*;

Class Customer {

public static void main(String arg[ ]) throws SQLException {


//step 2-register the driver

class.forName (”sun.jdbc.odbc.JdbcOdbcDriver”);


//step 3-connect to a database

Connection c= DriverManager.getConnection(“Jdbc.odbc:DSN”,”username”,”password”);


//step 4- create a statement

Statement s = c.createStatement( );


//step 5-execute the statement

Resultset rs = s.executeQuery (“ SQL statement”);


s.executeUpdate( ): This is used for all DDL command present in a database (ALTER, DROP, INSERT and CREATE ). This does not returns anything but executes the query and update the database.


s.execute( ): This method is used to execute an SQL statement that may return multiple returns The return value is a Boolean. Which is true if the next result is a Resultset and false if it is an update count or there are no more results.


//step 6- for displaying the column name

ResultSetMetaData rsmd =rs.getMetaData( );

int i =rsmd.getColumnCount( );

for(int j=i; j<=i; j++)

{

System.out.println( rsmd.getColumnName( j)+”\t”);

System.out.println(” ”);

}


//step 7-retrieve the results

while(rs.next( ) );

{

for(int j=i; j<=i; j++)

{

System.out.println( rs.getString( j)+”\t”);

}

System.out.println(” ”);

}


//step 8-close the statement and connection

s.close( );

c.close( );

}

}

A batch update is a set of multiple update statements that is a submitted to the database for processing as a batch. AddBatch, clearBatch and executeBatch are some of the methods that are used for batch updates. Two types of exceptions are thrown during batch updates and they include SQLException and BatchUpdateException.


 CONCLUSION


JDBC is successfully implemented. 


Share with others