You are on page 1of 4

10.

JDBC CONNECTIVITY
AIM:

To Write a Java program using jdbc connectivity.

ALGORITHM:

STEPS:

1. Start the process.


2. Open sql window and get connected to sql.
3. Create a table named pd with the fields including id, age ,first and last.
4. Then insert the values in to the sql table and do not exit from sql window .
5. Go to control panel then select administrative tools.
6. Then select ODBC data sources 32 bit, then select add option ;select
Microsoft ODBC for oracle. Double click on it.
7. Give the data source name as pd(sql table name) and give the user name as
system. Then select ok.
8. Now the database named pd is created by selecting ok. Now close the
window.
9. Then go to c drive and copy the ojdbc6 jar file and paste it into the java package.
10. Now open notepad window and start typing the java code.
11. Import the java.sql packages.
12. Create a class named fe in java.
13. First declare the necessary variables.
14. Open main method and write the actual part of the code for connecting with sql.
15. Save the java file name as fe .java.
16. Compile the java program using javac command.
17. Execute the code using java command.
18. Display the output in command prompt.
19. Stop the process.
SQL QUERY:

SQL*Plus: Release 11.2.0.2.0 Production on Wed Nov 16 12:21:40 2022


Copyright (c) 1982, 2014, Oracle. All rights reserved.
SQL> connect
Enter user-name: system
Enter password:
Connected.
SQL> create table pd(id int,age int,first varchar(9),last varchar(9));
Table created.
SQL> insert into pd values(100,18,'Zara','Ali');
1 row created.
SQL> insert into pd values(101,25,'Anu','Nair');
1 row created.

SQL> insert into pd values(102,13,'Zaid','Khan');


1 row created.
SQL> insert into pd values(103,28,'Sumit','Mittal');
1 row created.
SQL> select * from pd;
ID AGE FIRST LAST
---------- ---------- --------- ---------
100 18 Zara Ali
101 25 Anu Nair
102 13 Zaid Khan
103 28 Sumit Mittal
CODE:
import java.sql.*;
public class fe {
static final String DB_URL = "jdbc:oracle:thin@localhost:1521.xe";
static final String USER = "system";
static final String PASS = "oracle";
static final String QUERY = "SELECT id, first, last, age FROM pd";
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

OUTPUT:
C:\Users\LENOVO\Documents>javac fe.java
C:\Users\LENOVO\Documents> java fe
Connecting to database...
Creating statement...
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Anu, Last: Nair
ID: 102, Age: 13, First: Zaid, Las: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal

RESULT:

Thus the java program using database connectivity has been successfully
created .

You might also like