What is JDCB?
Ans:
JDCB (Java Database Connectivity) is a Java API for connecting programs written in Java to the data in relational databases. It consists of a set of classes and interfaces written in the Java programming language. JDBC provides a standard API for tool/database developers and makes it possible to write database applications using a pure Java API.
Simply put, JDBC makes it possible to do three things:
Establish a connection with a database
Send SQL statements
Process the results.
Advantages of JDCB
The main advantage of JDBC is that it is platform independent (as Java is platform independent) as well as database independent. It means that any software developed on platforms such as Linux can be used on platforms such as Windows, with very minor changes.
ODBC:-
ODBC (Open Database Connectively) is a standard or open application programming interface (API) for accessing a database. By Using ODBC statements in a program, you can access files in a number of different databases including Access, dBase, DB2, Excel and Text. It allows programs to use SQL requests that will access database without having to known the proprietary interfaces to the databases.
Comparison of JDCB with ODBC [2060,2062,2064]
Ans:
1. ODBC is for Microsoft and JDBC is for java applications.
2. ODBC mixes simple and advanced features together and has complex options for simple queries but JDBC is designed to keep things simple while allowing advanced capabilities when required.
3. ODBC can't be directly used with java because it uses a c interface.
4. ODBC is sued between application but JDBC is used by Java programmer to connect to database.
5. JDBC API is a natural Java Interface and is built on ODBC. JDBC retains some of the basic feature of ODBC.
6. ODBC makes use of pointers which have been removed totally from java.
The java sql package
Ans:
This package provides the APIs for accessing and processing data which is stored in the database especially relational database by using the java programming language. This java.sql package contains API for the following :
1. Making a connection with a database with the help of DriverManager class
2. Sending SQL Parameters to a database
3. Updating and retrieving the results of a query
4. Providing Standard mappings for SQL types to classes and interfaces in Java Programming language.
5. Metadata
6. Exceptions
7. Custom mapping an SQL user- defined type (UDT) to a class in the java programming language.
Steps for using JDBC [2064, 2065]
Ans:
Steps for JDBC
There are seven standard steps in querying databases:
a. Load the JDBC driver.
b. Define the connection URL.
c. Establish the connection.
d. Create a statement object.
e. Execute a query or update.
f. Process the results.
g. Close the connection.
Sample program
import java.sql.* ;
class JDBCQuery
{
public static void main( String args[] )
{
try
{
// Load the database driver
Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver” ) ;
// Get a connection to the database
Connection conn = DriverManager.getConnection( “jdbc:odbc:Database” ) ;
//DriverManager.getConnection(“jdbc:mysql://localhost/test?user=root&password=mypass”);
// Print all warnings
for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
{
System.out.println( “SQL Warning:” ) ;
System.out.println( “State : ” + warn.getSQLState() ) ;
System.out.println( “Message: ” + warn.getMessage() ) ;
System.out.println( “Error : ” + warn.getErrorCode() ) ;}
// Get a statement from the connection
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( “SELECT * FROM Cust” ) ;
// Loop through the result set
while( rs.next() )
System.out.println( rs.getString(1) ) ;
// Close the result set, statement and the connection
rs.close() ;
stmt.close() ;
conn.close() ;}
catch( SQLException se )
{
System.out.println( “SQL Exception:” ) ;
// Loop through the SQL Exceptions
while( se != null )
{
System.out.println( “State : ” + se.getSQLState() ) ;
System.out.println( “Message: ” + se.getMessage() ) ;
System.out.println( “Error : ” + se.getErrorCode() ) ;
se = se.getNextException() ; }}
catch( Exception e )
{
System.out.println( e ) ;}}}
What are the necessary steps needed for connecting a database with a java program through JDBC? [2062]
Ans:
Steps to connect to JDBC.
1. Import java.sql package
2. Load the driver, using Class.forName(DriverName);
3. Get the connection object, Connection con = Driver.getConnection(loaded driver name);
4. Create a SQL statement, Statement s = con.createStatement();
5. Execute a query or update ResultSet rs = s.executeQuery("sql statement");
ResultSet rs = s.executeUpdate("sql statement");
6. Retrive the result
while(rs.next())
System.out.println(“Name=”+rs.getstring(“Column Name”)
7. Close statement & Connection
s.close(); con.close();