Database Programming (Chapter No 1)
Database Programming (Chapter No 1)
Database
Programming
(JDBC)
Aditya Ingale
Class.forName("oracle.jdbc.driver.OracleDriver");
7.
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe"
,"system"
,"system");
8. System.out.println("Connected to database
successfully..."); 9. }
10. }
Explanation
1. The code in the main method creates an instance of OracleDriver and then
connects to a database called "jdbc:oracle:thin:@localhost:1521:xe" using
the username "system" with password "system".
2. The code in the main method prints out that it has successfully connected
to the database.
3. The code is a Java program that connects to the database and prints out a
success message.
Explanation
1. The code creates a table named "employee" with three columns:
emp_id, emp_name, and emp_salary.
2. The code creates the table using an SQL statement that is executed
by executing the following command in the Oracle database: create
table employee (emp_id int, emp_name varchar(255), emp_salary float).
3. The code creates a table in the database called "employee" with three
columns: "emp_id", "emp_name", and "emp_salary".
4 : Program to insert record in database in java (Oracle)
>>>
1. import java.sql.*;
2. class
Insert 3. {
4. public static void main(String[] args)throws
Exception 5. {
6. Class.forName("oracle.jdbc.driver.OracleDriver");
7. Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe"
,"system"
,"system");
8.
9. Statement st = con.createStatement();
10. st.executeUpdate("insert into employee values(101, 'Aditya',
11.
12. 1000.25)"); System.out.println("Data saved...");
13.
14. con.close();
15.
}
16. }
Explanation
1. The code starts by declaring a class called Insert.
2. The main method of the class is executed when the program starts.
3. It creates an instance of OracleDriver and connects to localhost on port
1521 with user system and password system using the statement
createStatement().
4. The code then executes an update statement that inserts data into
employee table with values 101, 'Aditya', 1000.25 in it.
5. After executing this statement, it prints "Data saved..." to indicate
success before closing the connection con.
6. The code is used to insert a row into the table.
16.
17.
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system ","system");
18.
19. Statement st = con.createStatement();
st.executeUpdate("insert into employee values('"+id+"',
20. '"+name+"', '"+salary+"')");
System.out.println("New data saved...");
21.
con.close();
22. }
23.
24.
25. }
Explanation
1. The code starts by declaring a Scanner object and assigning it to the variable
sc.
2. Next, the code prints out "Enter employee ID : " followed by an input
prompt.
3. The user enters their employee ID and presses enter.
4. The program then prints out "Enter employee Name : " followed by another
input prompt.
5. The user enters their name and presses enter again.
6. After that, the program prints out "Enter employee Salary : ".
7. Finally, the code executes a statement called executeUpdate().
8. This statement is used to insert data into a table in Oracle database using
SQL commands such as INSERT INTO Employee VALUES('102', 'Aditya',
1000.25).
9. The first line of code declares a Class object for oracle.jdbc driver which is
needed for connecting to Oracle database through JDBC API (Java Database
Connectivity API) .
10.Then on next line we declare connection with username system, password
system , hostname localhost:1521:xe , port 1521 .
11.On last line we create Statement object from this connection which will be
used later in main method of class InsertData
12.The code creates a connection to the database and then executes an
update statement using the insert into employee values('id', 'name',
'salary') format.
6 : Program to update the record in the database in
java (Oracle)
>>>
1. import java.sql.*;
2. class
Update 3. {
4. public static void main(String[] args)throws
Exception 5. {
6. Class.forName("oracle.jdbc.driver.OracleDriver");
7. Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe"
,"system"
,"system");
8.
9. Statement st = con.createStatement();
10 st.executeUpdate("update employee set emp_salary = 5000
. where emp_id = 101");
System.out.println("Data updated...");
11
. con.close();
12 }
.
13
.
14
.
15. }
Explanation
1. The code is a Java program that uses the Oracle JDBC driver to connect to
an Oracle database.
2. The code creates a connection and executes an update statement on the
employee table.
3. The code will execute the following SQL statement: update employee set
emp_salary = 5000 where emp_id = 101.