You are on page 1of 3

Java JDBC Tutorial

Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc
drivers to connect with the database.

Why use JDBC

Before JDBC, ODBC API was the database API to connect and execute query with the database.
But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and
unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers
(written in Java language).

/*
Import JDBC core packages.
Following statement imports the java.sql package,
which contains the JDBC core API.
*/
import java.sql.*;

public class RetriveAllEmployees{


public static void main(String[] args) {
System.out.println("Getting All Rows from employee table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "jdbc";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM employee");
System.out.println("Employee Name: " );
while (res.next()) {
String employeeName = res.getString("employee_name");
System.out.println(employeeName );
}
con.close();
}
catch (ClassNotFoundException e){
System.err.println("Could not load JDBC driver");
System.out.println("Exception: " + e);
e.printStackTrace();
}
catch(SQLException ex){
System.err.println("SQLException information");
while(ex!=null) {
System.err.println ("Error msg: " + ex.getMessage());
System.err.println ("SQLSTATE: " + ex.getSQLState());
System.err.println ("Error code: " + ex.getErrorCode());
ex.printStackTrace();
ex = ex.getNextException();
// For drivers that support chained exceptions
}
}
}
}

Prerequisite – Functional Dependencies


The term Armstrong axioms refers to the sound and complete set of inference rules or axioms,
introduced by William W. Armstrong, that is used to test logical implication of functional
dependencies. If F is a set of functional dependencies then the closure of F, denoted as , is
the set of all functional dependencies logically implied by F. Armstrong’s Axioms are a set of
rules, that when applied repeatedly, generates a closure of functional dependencies.

Axioms –

1. Axiom of reflexivity – If is a set of attributes and is subset of , then holds . If


then This property is trivial property.
2. Axiom of augmentation – If holds and is attribute set, then also holds.
That is adding attributes in dependencies, does not change the basic dependencies. If ,
then for any .
3. Axiom of transitivity – Same as the transitive rule in algebra, if holds and
holds, then also holds. is called as functionally that determines . If
and , then

Secondary Rules –

These rules can be derived from the above axioms.

Union – If holds and holds, then holds. If and then

Composition – If and holds, then holds.


Decomposition – If holds then and hold. If then
and

Pseudo Transitivity – If holds and holds, then holds. If


and then .

You might also like