Professional Documents
Culture Documents
Java Database
Connectivity
Driver Types
Type
Type
Type
Type
1:
2:
3:
4:
In a Type 2 driver, JDBC API calls are converted into native C/C++
API calls which are unique to the database. These drivers typically
provided by the database vendors and used in the same manner
as the JDBC-ODBC Bridge, the vendor-specific driver must be
installed on each client machine.
The native API as it is specific to a database so still it had to be
installedon each machine
some speed increase with a Type 2 driver, because it eliminates
ODBC's overhead.
Ex. OCI
Connection to the DB
After you've loaded the driver, you can establish a
connection using the
DriverManager.getConnection()method. The three
overloaded DriverManager.getConnection() methods:
getConnection(String url)
getConnection(String url, Properties prop)
getConnection(String url, String user, String password)
Here each form requires a databaseURL. A database URL
is an address that points to your database.
Formulating a database URL is where most of the
problems associated with establishing a connection occur.
URL
Any
Database
RDBMS
URL format
MySQL
com.mysql.jdbc.Driver
jdbc:mysql://hostname/
databaseName
ORACLE
oracle.jdbc.driver.OracleDriv jdbc:oracle:thin:@hostname:p
er
ort Number:databaseName
DB2
COM.ibm.db2.jdbc.net.DB2D jdbc:db2:hostname:port
river
Number/databaseName
Sybase
com.sybase.jdbc.SybDriver
jdbc:sybase:Tds:hostname:
port Number/databaseName
Boolean First()
Boolean Last()
Boolean Next()
Type getType(int columnIndex)
Type getType(String ColumnName)
Example of PreparedStatement
import java.sql.*;
public class jdbcConn {
public static void main(String[] args)
throws Except{
int id = 23;
String name = Roshan;
String desg = CEO;
Class.forName("
sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection
(" jdbc:odbc:NishaMysql10
","name,pass");
PreparedStatement pstmt
=con.prepareStatement
("insert into emp values(?,?,?)");
pstmt setInt(1,id);
pstmt.setString(2,name);
pstmt setString(3, desg);
Example of Callable
Statement
import java.sql.*;
//Bind IN parameter first, then bind
public class JDBCExample {
static final String JDBC_DRIVER = "
sun.jdbc.odbc.JdbcOdbcDriver";
static final String DB_URL = "
jdbc:odbc:NishaMysql10";
static final String USER =
"username";
static final String PASS = "password";
public static void main(String[] args)
{
Connection conn = null;
CallableStatement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driv
er");
conn = DriverManager.
getConnection(DB_URL,USER,PAS
S);
OUT parameter
int empID = 102;
stmt.setInt(1, empID); // This
would set ID as 102, Because second
parameter is OUT so register it
stmt.registerOutParameter(2,
java.sql.Types.VARCHAR);
//Use execute method to run stored
proc
stmt.execute();
//Retrieve employee name with
getXXX meth
String empName =
stmt.getString(2);
System.out.println("Emp Name
with ID:" +
empID + " is " +
empName);
stmt.close();
rs.moveToInsertRow();
rs.updateString("Title", title);
rs.updateString("ISBN", isbn);
rs.updateDouble("Price", price);
rs.insertRow();
rs.moveToCurrentRow();
Finally, you can delete the row under the cursor.
rs.deleteRow();
The deleteRow method immediately removes the row
from both the result set and the database.
The updateRow, insertRow, and deleteRow
methods of the ResultSet class give you the same
power as executing UPDATE, INSERT, and DELETE
SQL commands. However, programmers who are used
to the Java programming language will find it more
natural to manipulate the database contents through
result sets than by constructing SQL statements.
Metadata
D atab aseM etad ata - JDBC can give you additional
information about the structure of a database and its
tables. For example, you can get a list of the tables
in a particular database or the column names and
types of a table.
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getTables(null, null, null, new String[]
{ "TABLE" });
Transaction
The major reason for grouping commands into
transactions is database integrity. For example,
suppose we want to add a new book to our book
database. Then, it is important that we simultaneously
update the Books, Authors, and BooksAuthors table. If
the update were to add new rows into the first two
tables but not into the third, then the books and
authors would not be properly matched up.
If you group updates to a transaction, then the
transaction either succeeds in its entirety and it can be
committed, or it fails somewhere in the middle. In that
case, you can carry out a rollback and the database
automatically undoes the effect of all updates that
occurred since the last committed transaction.
By default, a database connection is in autocommit
mode, and each SQL command is committed to the
database as soon as it is executed. Once a command is
committed, you cannot roll it back.
Transaction
You can group a set of statements to form a transaction. Thetransaction can be committed when
all hasgone well. Ot if an error occurred in one of them, it can be rolled back as if none of them has
been issued. The major reason for grouping statements into transaction is database intigrity.
By default a database connection is in autocommit mode, and each SQL statement is committed to
the database as soon as it is executed. Once a statement is committed you cant roll it back. Turn
off this default when you use transaction;
Con.setAutoCommit(false);
Create a statement in a normal way
stmt = con.createStatement();
Call executeUpdate() or Insert or delete
Stmt.executeUpdate(command1);
Stmt.executeUpdate(command2);
Stmt.executeUpdate(command3);
Stmt.executeUpdate(command4);
If statements have been excuted successfully call
Con.commit()
Else if an error occure in the exception block use
Con.rollback();
The all the statements until the last commit are automatically reserved. You generally isssue a
rollback statement when your transaction was interrupted by SQLException