JDBC
JDBC
JDBC
Step 5 :Now, start the tomcat server Executable programs and scripts are kept in the "bin" sub-directory
of the Tomcat installed directory, e.g., "E:\myserver\tomcat7.0.40\bin".
Step 5(a) Start Server Launch a command shell. Set the current directory to "\bin" like
E:\myserver\tomcat7.0.40\bin, and run "startup.bat" as follows:
After that a new Tomcat console window appears. Read the messages on the console. Look out for the
Tomcat's port number (double check that Tomcat is running on port 9999)....... We saw a figure like:
JDBC(JAVA DATABASE CONNECTIVITY):
What is API?
API (Application programming interface) is a document that contains description of all the features of
a product or software. It represents classes and interfaces that software programs can follow to
communicate with each other. An API can be created for applications, libraries, operating systems, etc.
JDBC – Architecture:
The JDBC Architecture consists of two layers: the JDBC API, which provides the application-to-
JDBC Manager connection, and the JDBC Driver API, which supports the JDBC Manager-to-Driver
Connection.
Common JDBC Components
The JDBC API provides the following interfaces and classes −
DriverManager: This class manages a list of database drivers. Matches connection requests from the
java application with the proper database driver using communication sub protocol. The first driver that
recognizes a certain sub protocol under JDBC will be used to establish a database Connection.
Driver: This interface handles the communications with the database server. You will interact directly
with Driver objects very rarely. Instead, you use DriverManager objects, which manages objects of this
type. It also abstracts the details associated with working with Driver objects.
Connection: This interface with all methods for contacting a database. The connection object
represents communication context, i.e., all communication with database is through connection object
only.
Statement: You use objects created from this interface to submit the SQL statements to the database.
Some derived interfaces accept parameters in addition to executing stored procedures.
There are 3 types of Statements, as given below:
• Statement: It can be used for general-purpose access to the database. It is useful when you are
using static SQL statements at runtime.
• PreparedStatement: It can be used when you plan to use the same SQL statement many times.
The PreparedStatement interface accepts input parameters at runtime.
• Callable Statement: Callable Statement can be used when you want to access database stored
procedures.
ResultSet: These objects hold data retrieved from a database after you execute an SQL query using
Statement objects. It acts as an iterator to allow you to move through its data.
SQLException: This class handles any errors that occur in a database application.
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
• JDBC-ODBC bridge driver
• Native-API driver (partially java driver)
• Network Protocol driver (fully java driver)
• Thin driver (fully java driver)
Class.forName("oracle.jdbc.driver.OracleDriver");
The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
Statement stmt=con.createStatement();
Step 5: Execute a query and Returning a result
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.
next():Used to move the cursor to the one row next from the current position.
getInt(column index):Used to return the data of specified column index of the current row as int.
while(rs.next())
{
int id = rs.getInt(1); // index 1 is the "id" column
String name = rs.getString(2); // index 2 is the "name" column
System.out.println(id);
System.out.println(name)
}
By closing connection object statement and ResultSet will be closed automatically. The close() method
of Connection interface is used to close the connection.
con.close();
import java.sql.*;
class MysqlCon
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306
/mark","root","root");
//here mark is the database name, root is the username and root is the password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
int id = rs.getInt(1); // index 1 is the "id" column
String name = rs.getString(2); // index 2 is the "name" column
System.out.println(id);
System.out.println(name)
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}