Support Me ��


SUBSCRIBE NOW

JDBC in JAVA 3


JDBC in JAVA 3

Steps to connect a Java Application to Database

The following 5 steps are the basic steps involve in connecting a Java application with Database using JDBC.

1. Register the Driver

2. Create a Connection

3. Create SQL Statement

4. Execute SQL Statement

5. Closing the connection
 

Register the Driver

Class.forName() is used to load the driver class explicitly.

Example to register with JDBC-ODBC Driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Create a Connection

getConnection() method of DriverManager class is used to create a connection.

getConnection() method of DriverManager class is used to create a connection.

Syntax:

getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)

Example establish connection with Oracle Driver

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","username","password");

Create SQL Statement

createStatement() method is invoked on current Connection object to create a SQL Statement.

Syntax

public Statement createStatement() throws SQLException

Example to create a SQL statement

Statement s=con.createStatement();

Execute SQL Statement.

executeQuery() method of Statement interface is used to execute SQL statements.

Syntax:

public ResultSet executeQuery(String query) throws SQLException

Example to execute a SQL statement

ResultSet rs=s.executeQuery("select * from user");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2));
}

Closing the connection

After executing SQL statement you need to close the connection and release the session. The close() method of Connection interface is used to close the connection.

Syntax:

public void close() throws SQLException


Example of closing a connection:

con.close();

Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.

Share this

Backlinks

Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website