Saturday, 3 January 2015

Steps to connect with database using JDBC in java

There are 5 steps to connect any java application with the database in java using JDBC API. They are given below:

>Register the driver class
>Create  connection
>Create statement
>Execute queries
>Close connection

>1.Register the driver class

The forName() method of class Class is used to register the driver class. This method is used to dynamically load the driver class to connect with database.

Syntax of forName() method:

public static void forName(String className) throws ClassNotFoundException

For Example to register the OracleDriver class:

Class.forName("oracle.jdbc.driver.OracleDriver");

Note: Here oracle.jdbc.driver  is a package location where OracleDriver class is exits.You can see it in the ojdbc14.jar file.

>2.Create the connection object

The getConnection() method of DriverManager class is used to establish connection with the database.

Syntax of getConnection() method

>1. public static Connection getConnection(String url)  throws SQLException
>2. public static Connection getConnection(String url,String name,String password)  throws                     SQLException

For Example to establish connection with the Oracle database:

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

Here you want to know what is url ( "jdbc:oracle:thin:@localhost:1521:xe","username","password")
This is known as connection url in which...

jdbc: It is a jdbc api.
oracle: It is a database name to which you are going to connect.It may be change for different             database.
thin: A type of driver.
localhost: It is server name or system address on which oracle database is running.You can also use   IP address of that system or server.
1521: It  is port number.
xe: An Oracle service name.
username: It is an username of the Oracle Database.By default it is system
password: Password is given by the user at the time of installing the oracle database.


>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.

Syntax of createStatement() method:

public Statement createStatement() throws SQLException

For Example to create the statement object:

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 of executeQuery() method:

public ResultSet executeQuery(String sql) throws SQLException

For Example to execute query:

 ResultSet rs=stmt.executeQuery("select * from student");
 while(rs.next()){
 System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
 }

 >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 of close() method:

public void close()throws SQLException

For Example 

con.close();


No comments:

Post a Comment