We have to follow 6 steps to play with Oracle database in java jdbc.
Note: We have use “ojdbc-version.jar” ex- ojdbc14.jar
Step 1: Create table in database
CREATE TABLE USER_DETAILS (
USER_ID NUMBER(5) NOT NULL,
USER_NAME VARCHAR2(50) NOT NULL,
PASSWORD VARCHAR2(50) NOT NULL,
Step 2: Load the Driver class
“oracle.jdbc.driver.OracleDriver” using below syntax –
Class.forName("oracle.jdbc.driver.OracleDriver");
Step 3: Get connection by using below url –
"jdbc jdbc:oracle:thin:@localhost:1521:xe","username","password"
Note : “xe” is service id, we can get the service id by using the below command-
“SELECT * FROM GLOBAL_NAME”
"jdbc jdbc:oracle:thin:@localhost:1521:xe","username","password"
Note : “xe” is service id, we can get the service id by using the below command-
“SELECT * FROM GLOBAL_NAME”
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password"); |
Step 4: Create Statement or PreparedStatement –
PreparedStatement prepareStatement = connection.prepareStatement("SELECT * FROM USER_DETAILS");
Step 5: Execute the query-
ResultSet resultSet = prepareStatement.executeQuery();
Step 6: Iterate the result set-
while (resultSet.next())
{ System.out.println("UserName:"+resultSet.getString("NAME") + "," + ("UserPassword:"+resultSet.getString("PASSWORD"));
}
Example-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.sql.*; class MySqlTest { public static void main(String[] args)throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password"); PreparedStatement prepareStatement = connection.prepareStatement("SELECT * FROM USER_DETAILS"); ResultSet resultSet = prepareStatement.executeQuery(); while (resultSet.next()) { System.out.println("UserName:"+resultSet.getString("NAME")+","+("UserPassword:"+ resultSet.getString("PASSWORD") ); } resultSet.close(); prepareStatement.close(); connection.close(); } } |
Nice Sir.........
ReplyDelete