jdbc connectivity in java
DESCRIPTION
how to connect to db using jdbcTRANSCRIPT
JDBC
SYNOPSIS
• What is JDBC?
• Architecture of JDBC
• Types of JDBC drivers
• Steps to connect database
What is JDBC
• JDBC – Java Database Connectivity
• JDBC is a standard java API for independent database connection between a java program and wide range of relational database
• It is present in the “java.sql” package
Architecture of JDBC
Types of JDBC driver
• Type 1: jdbc-odbc bridge driver
• Type 2: net protocols all java driver
• Type 3: native API partly java driver
• Type 4: native protocols all java driver
How to connect ?
• Define the connection URL
• Established the connection
• Create the statement object
• Execute a query
• Process the results
• Close the connection
Connection URL
• The connection URL is Class.forName();
• For jdbc-odbc bridge driver:
class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
• For oracle driver
class.forName(“oracle.jdbc.driver.OracleDriver”);
• For MySql driver
class.forName(“com.mysql.jdbc.Driver”);
Establishing the connection• Connection con =
Driver.Manager.getConnection(“url”,”user_name”,”password”);
• Statement stmt = con.createStatement();
Executing queries
• For select queries
Stmt.executeQuery(select * from tab_name);
• For insert queries
Stmt.executeQuery(insert into tab_name values(1,”hello”));
Process results
ResultSet rs = stmt.executeQuery(some query);
While(rs.next())
{
System.out.println(rs.getInt(id));
System.out.println(rs.getString(name));
}
Close connection
• Close the connection using
Stmt.close();
Con.close();
Sample program