AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-jdbc: What are the steps to connect a java application to database?

Please explain step by step process to connect a java application to database.

java x 211
jdbc x 32
Posted On : 2013-11-28 18:51:41.0
profile Garima Gupta - anyforum.in Garima Gupta
596129554549
up-rate
6
down-rate

Answers


There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:
Register the driver class
Creating connection
Creating statement
Executing queries
Closing 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.

Syntax of forName() method :
----------------------------------------
public static void forName(String className)throws ClassNotFoundException

Example to register the OracleDriver class :
--------------------------------------------------------------
Class.forName("oracle.jdbc.driver.OracleDriver");

-----------------------------------------------------------------------------------------------------------------------------------

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

Example to establish connection with the Oracle database :
-------------------------------------------------------------------------------
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","username","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.

Syntax of createStatement() method :
--------------------------------------------------
public Statement createStatement()throws SQLException

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 rocords of a table.

Syntax of executeQuery() method :
----------------------------------------------
public ResultSet executeQuery(String sql)throws SQLException

Example to execute query :
-----------------------------------------
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 statment 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

Example to close connection :
-------------------------------------------
con.close();

Posted On : 2013-11-28 19:12:15
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188245674
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in