Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

KIET School of Engineering & Technology, Ghaziabad

Department of Information Technology (NBA Accredited)


(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)

Web Technologies
(KIT 501)
Assignment-2

KRITIK BANSAL
1802911018
CSI

1. Describe the concept of JDBC used in Java. What are the various JDBC drivers
are there?
Ans

JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the
query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database.

There are four types of JDBC drivers:

1. JDBC-ODBC Bridge Driver,


2. Native Driver,
3. Network Protocol Driver, and
4. Thin Driver

2. Write an program to connect java using jdbc with oracle database.


Ans

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{

Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Connected");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@Localhost:1521:xe", "system",
"system"); System.out.println("connected");
Statement statement = con.createStatement();
String q1 = "create table student(name varchar2(30),city varchar2(30),email varchar2(60),schooling
varchar2(60),college varchar2(60),age number )";
String q2 = "insert into student values('amit','ghaziabad','amit@gmail.com','Aps','KIET',23)";
String q3 = "insert into student values('sumit','varanasi','sumit@gmail.com','sps','IET',25)";
String q4 = "insert into student values('atul','agra','atul@gmail.com','Dps','ITS',24)";
statement.addBatch(q1);
statement.addBatch(q2);
statement.addBatch(q3);
statement.addBatch(q4);
int [] x = statement.executeBatch();
for(int xx : x){
System.out.println(xx);
}
ResultSet rs = statement.executeQuery("select * from student");
while(rs.next()){
System.out.println(rs.getString("name") + "," + rs.getString("city") + "," + rs.getString("email")+ "," +
rs.getString("schooling")+ "," + rs.getString("college")+ "," + rs.getString("age"));
}
con.close();
}
}

3. How to implement prepared statement in oracle and MySQL database.


Ans
The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized
query.

Let's see the example of parameterized query:

1. String sql="insert into emp values(?,?,?)";


2. PreparedStatement stmt=con.prepareStatement(sql);

As you can see, we are passing parameter (?) for the values. Its value will be set by calling the setter
methods of PreparedStatement.

You might also like