java tutorial - How to connect database in java - java programming - learn java - java basics - java for beginners
Learn Java - Java tutorial - jdbc connect with oracle - Java examples - Java programs
- To connect any java application with the database in java using JDBC.We are using 5 steps,
- Register the driver class
- Creating connection
- Creating statement
- Executing queries
- Closing connection
Register the driver class
- The forName() method of Class is used to register the driver class. This method is used to dynamically load the driver class.
Syntax
Create the connection object
- The getConnection() method of DriverManager class is used to establish connection with the database.
Syntax
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
Execute the query
- The executeQuery() method of Statement interface is used to execute queries to the database.
- Method returns the object of ResultSet that can be used to get all the records of a table.
Syntax
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
Sample Code
- In this sample we are using Oracle 10g as the database. So we need to know following information for the oracle database.
Properties of oracle thin driver.
Properties | Description |
---|---|
Driver Class: | Oracle.Jdbc.OracleDriver |
Connection url: | jdbc:oracle:thin:@localhost:1521:xe |
Username: | The default username for the oracle database is system. |
password | Password is given by the user at the time of installing the oracle database. |
jdbc:oracle:thin:@localhost:1521:x
- jdbc is the API(Application Program Interface)
- oracle is the database
- thin is the driver
- localhost is the server name on which oracle is running, we may also use IP address
- 1521 is the port number and
- XE is the Oracle service name.