1.
Associating JDBC/ODBC Bridge with
Database
This process connects a Java program to a database using ODBC.
Steps:
1. Open Control Panel → ODBC Data Source Administrator
2. Select ODBC (32-bit)
3. Click Add to create a new data source
4. Choose the database driver (e.g., MS Access, MySQL)
5. Enter Data Source Name (DSN) → used in Java connection
6. Add description (optional)
7. Click Select → choose database file path
8. Set username & password (optional via Advanced settings)
9. Click OK to save
10.Close ODBC Administrator
👉 This DSN is used in Java like:
jdbc:odbc:DataSourceName
2. Transaction Processing
What is a Transaction?
● A group of SQL statements executed together
● Either all succeed OR all fail
Real-life Example:
● Like a supermarket bill → if one step fails, whole process cancels
Key Points:
● If one query fails → rollback() is used
● If all succeed → commit() is called
● Ensures data consistency
commit() and AutoCommit
commit()
● Saves all changes permanently
● After commit → changes cannot be undone
rollback()
● Cancels all changes before commit
AutoCommit:
● Default = true
● Every query is automatically saved
For Transactions:
● Disable AutoCommit
setAutoCommit(false);
● After completion:
setAutoCommit(true);
Program:
Import [Link].*;
Public class Example{
Public static void main(String[] args){
String url = "jdbc:odbc:CustomerInformation";
String userID = "jim";
String password = "keogh";
Statement DataRequest1, DataRequest2 ;
Connection Database;
try {
[Link]( "[Link]");
Database = [Link](url,userID,password);
catch (ClassNotFoundException error) {
[Link]("Unable to load the JDBC/ODBC bridge." +
error);
[Link](1);
catch (SQLException error) {
[Link]("Cannot connect to the database." + error);
[Link](2);
try {
Database .setAutoCommit(false)
String query1 = "UPDATE Customers SET Street = '5 Main Street' "
"WHERE FirstName = 'Bob'";
String query2 = "UPDATE Customers SET Street = '10 Main Street' " +
"WHERE FirstName = 'Tim'";
DataRequest1= [Link]();
DataRequest2= [Link]();
[Link] (query1 );
[Link] (query2 );
[Link]();
[Link]();
[Link]();
[Link]();
catch(SQLException ex) {
[Link]("SQLException: " + [Link]());
if (con != null) {
try {
[Link]("Transaction is being rolled back ");
[Link]();
catch(SQLException excep) {
[Link]("SQLException: ");
[Link]([Link]());
4. Transaction Execution Flow
1. Connect to database
2. Disable AutoCommit
3. Execute SQL queries
4. If success → commit()
5. If error → rollback()
6. Close connection
5. Metadata (DatabaseMetaData)
What is Metadata?
● Data about data
Used for:
● Getting database details like:
○ Tables
○ Columns
○ Schemas
○ Keys
How to get:
DatabaseMetaData db = [Link]();
Important Methods:
● getDatabaseProductName() → DB name
● getUserName() → user
● getURL() → database URL
● getTables() → table names
● getPrimaryKeys() → primary keys
● getSchemas() → schema names
🔹 6. ResultSet Metadata
Purpose:
● Gives info about query result (table output)
How to get:
ResultSetMetaData rm = [Link]();
Important Methods:
● getColumnCount() → number of columns
● getColumnName(int) → column name
● getColumnType(int) → data type
7. Data Types (SQL → Java)
SQL Type Java Type
CHAR String
VARCHAR String
NUMERIC BigDecimal
DECIMAL BigDecimal
BIT Boolean
TINYINT Byte
SMALLINT Short
INTEGER Integer
BIGINT Long
REAL float
👉 Used in:
● setXXX() → set values
● getXXX() → retrieve values
Example:
setInt(), getString()
🔹 8. JDBC Exceptions
Types of Exceptions:
1. SQLException
● Most common
● Causes:
○ SQL syntax errors
○ Connection issues
○ Closed objects
● Methods:
○ getMessage()
○ getErrorCode()
○ getNextException()
2. SQLWarning
● Gives warnings (not errors)
● Retrieved using:
getWarnings()
getNextWarning()
3. DataTruncation
● Occurs when data is cut/shortened
● Example:
○ Inserting long string into small column