Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
Free Online JDBC API Tutorials in JDBC,JDBC API in JDBC
GeekMantra
Search Site

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
J2SE [Java 2 Standard Edition]
Core Java Introduction
Exception Handling
Collection & Util Classes
Multithreading
Input & Output Streams
Applet
Adwance Windows Toolkit (AWT)
Swings (Adwance GUI)
Event Handling
Java Networking
Remote Method Invocation (RMI)
Java Database Connectivity (JDBC)
J2EE [Java 2 Enterprise Edition]
Java 2 Enterprise Edition Intoduction (J2EE)
Server Side Programming (Servlet)
Java Server Pages (JSP)
Enterprise Java Bean
MVC Framework (Struts)
Advance Framework (Springs)
Object-relational mapping (Hibernate)
eXtensible Markup Language (XML)
Asynchronous JavaScript and XML (AJAX)
Miscellneous
Monthly News Letter
Free SCJP Samples
Interviews Questions For Freshers
Interviews Questions For Experinced
Most Visited Links
Most Freaky Links
Advertisements

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2

JDBC Standard APIs

1. Registering a Driver
    -statically load driver
         Class.forName(“foo.bar.MyDriver”);
         Connection c = DriverManager.getConnection(...);

    -or use the jdbc.drivers system property

2. JDBC URLs
    -jdbc:subprotocol:source
    each driver has its own subprotocol and each subprotocol has its own syntax

JDBC URL

3. DriverManager
    Connects to given JDBC URL with given user name and password
    Throws java.sql.SQLException
    returns a Connection object

JDBC Connection API

Connection
1. A Connection represents a session with a specific database.
2. Within the context of a Connection, SQL statements are executed and results are returned.
3. Can have multiple connections to a database
    NB: Some drivers don’t support serialized connections
    Fortunately, most do (now)
4. Also provides “metadata” -- information about the database, tables, and fields
5. Also methods to deal with transactions

Obtaining a Connection
String url   = "jdbc:odbc:mydsn";
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection(url);
}
catch (ClassNotFoundException e)
    { e.printStackTrace(); }
catch (SQLException e)
    { e.printStackTrace(); }

Connection Methods
  -Statement createStatement()
    returns a new Statement object
  -PreparedStatement prepareStatement(String sql)
   returns a new PreparedStatement object
  -CallableStatement prepareCall(String sql)
    returns a new CallableStatement object

JDBC Statement APIs

1. Statement
A Statement object is used for executing a static SQL statement and obtaining the results produced by it.

Statement Methods

ResultSet executeQuery(String)
Execute a SQL statement that returns a single ResultSet.
int executeUpdate(String)
Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.
boolean execute(String)
Execute a SQL statement that may return multiple results.

2. The PreparedStatement Interface

• The prepareStatement() method of Connection interface returns a PreparedStatement object
• Useful for frequently executed SQL statements
• An SQL statement is pre-compiled and gets executed more efficiently than a plain statement

   For example: the statement “INSERT INTO Table1 VALUES (?, ?)” can be used to add multiple records, with a different values each time. Where ‘?’ acts as a placeholder

JDBC Prepared Statement

3. The CallableStatement Interface

• A CallableStatement object is created by calling the prepareCall() method on a Connection object
• The object provides a way to call stored procedures in a standard way for all DBMS
• CallableStatement inherits Statement methods, which deal with SQL statements in general, and it also inherits PreparedStatement methods, which deal with IN parameters
• All methods defined in the CallableStatement deal with OUT parameters or the output aspect of INOUT parameters
• Syntax for a stored procedure without parameters is:
   {call procedure_name}
• Syntax for procedure call with two parameters:
   {call procedure_name[(?, ?)] }