JDBC
JDBC
Introduction
JDBC stands for Java Database Connectivity.
JDBC is a Java API to connect and execute the query with the
database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the database. There
are four types of JDBC drivers:
Why Should We Use JDBC
1. Connect to the database
2. Execute queries and update statements to the
database
3. Retrieve the result received from the database.
Java Database Connectivity with 5 Steps
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","
password");
3) Create the Statement object
The createStatement() method of Connection interface is used to create statement.
The object of statement is responsible to execute queries with the database.
public Statement createStatement()throws SQLException
Syntax:
Statement stmt=con.createStatement();
4) Execute the query
The executeQuery() method of Statement interface is used to execute queries to
the database. This method returns the object of ResultSet that can be used to get
all the records of a table.
Syntax:
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5) Close the connection object
By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close the
connection.
Syntax:
public void close()throws SQLException
con.close();
CREATE DATABASE
Syntax;