JDBC interview questions and answers
6: How to establish a Database connection between
java application and Database?
If we want to
establish a connection between java application and the database we will the
following piece of code.
Connection con=
DriverManager.getConnection(“jdbc:odbc:nag”,”nag”,”system”,”manager”);
Where getConnectin()
is a static method from DriverManager class, which can be used to return
connection object.
7: Basically
Connection is an interface, how getConnection() will create an object for
Connection interface?
Connection interface?
Ans: Connection is an interface from java.sql package,
for which getConnection(_) was return an anonymous inner class object of
the Connection interface.
Note:- Anonymous inner class is a nameless inner
class, which can be sued to provide an implementation either for the interfaces
or for abstract classes.
Eg: interface I
{
void m1();
}
Class Outer
{
I i = new I(){
public void m1()
{
}
public void m2()
{
{
void m1();
}
Class Outer
{
I i = new I(){
public void m1()
{
}
public void m2()
{
}
}
}
Outer o = new Outer();
o.i.m1(); à correct
o.i.m2(); à wrong
}
}
Outer o = new Outer();
o.i.m1(); à correct
o.i.m2(); à wrong
getConnection(_) is a static method from DriverManager class, which will call
internally connect() method, this connect() will establish a virtual
socket connection in between the java application and the database.
8: What
is the requirement to use Statement object?
- After establishing a connection between java
application and the database we need to write the sql queries and we need
to execute them.
- To execute the sql queries we will use some pre-defined
library, which was defined in the form of Statement object,
PreparedStattement object and CallableStatement object.
- As per the application requirements we need to create
either Statement object or CallableStatement object and PreparedStatement
object.
- To create Statement object dwe will use the following
method from connection object.
public Statement
createStatement()
Eg: Statement st = con.createStatement();
Eg: Statement st = con.createStatement();
9:
How to execute SQL Queries from a java application?
To execute the sql
queries we will use the following methods from Statement object.
- st.executeQuery(…)
- st.executeUpdate(…)
- st.execute(…)
10: What are the differences between executeQuery(…),
executeUpdate(…) and execute(…)
methods?
methods?
Ans:
where executeQuery() can be used to execute selection group sql queries to
fetch the data from database.
When we use selection group sql query with executeQuery() then JVM will send that sql query to the database engine, database engine will execute it, by this database engine(DBE) will fetch the data from database and send back to the java application.
Java is a purely object oriented technology. That’s why the jdbc application will maintain the fetched data from database, in the form of an object at heap memory, called as ResultSet object.
When we use selection group sql query with executeQuery() then JVM will send that sql query to the database engine, database engine will execute it, by this database engine(DBE) will fetch the data from database and send back to the java application.
Java is a purely object oriented technology. That’s why the jdbc application will maintain the fetched data from database, in the form of an object at heap memory, called as ResultSet object.
public ResultSet executeQuery(String sqlquery)
where executeUpdate() can be used to execute updation group sql query to update
the database. When we provide updation group sql query as a parameter to
executeUpdate(), then JVM will send that sql query to DBE, here DBE will
execute it and perform updations on the database, by this DBE will identify the
number of records got updated value called as “records updated count” and
return back to the java application.
public int executeUpdate(String sqlquery)
where execute() can be
used to execute either selection group sql queries or updation group queries.
When we use selection group sql query with the execute() then we will get ResultSet object at heap memory with the fetched data. But execute() will return “true” as a Boolean value.
When we use updation group sql query with execute() then we will get “ records updated count value” at jdbc application. But execute() will return “false” as a Boolean value.
When we use selection group sql query with the execute() then we will get ResultSet object at heap memory with the fetched data. But execute() will return “true” as a Boolean value.
When we use updation group sql query with execute() then we will get “ records updated count value” at jdbc application. But execute() will return “false” as a Boolean value.
public boolean execute(String sqlquery)
11: How to create a table dynamically from a jdbc application?
//import section
import java.sql.*;
import java.io.*;
import java.sql.*;
import java.io.*;
public class CreateTableEx
{
public static void main(String[] args)throws Exception
{
//create buffered reader object
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
{
public static void main(String[] args)throws Exception
{
//create buffered reader object
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//load and register the driver
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//establish connection
Connection con = DriverManager.getConnection(“jdbc:odbc:nag”,”system”,”durga”);
Connection con = DriverManager.getConnection(“jdbc:odbc:nag”,”system”,”durga”);
//create statement object
Statement st = con.createStatement();
Statement st = con.createStatement();
//take table name as dynamic input
System.out.println(“Enter table name”);
String tname = br.readLine();
System.out.println(“Enter table name”);
String tname = br.readLine();
//execute sql query
St.executeUpdate(“create table”+tname+”(eno number,ename varchar2(10),esal number,eaddr varchar2(10))”);
St.executeUpdate(“create table”+tname+”(eno number,ename varchar2(10),esal number,eaddr varchar2(10))”);
System.out.println(“table created successfully”);
//closing the connection
con.close();
}
}
con.close();
}
}
12: How to insert records into a table from a JDBC application?
import java.sql.*;
import java.io.*;
public class InsertTableEx
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con = DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”durga”);
Statement st = con.createStatement();
while(true)
{
System.out.println(“Enter emp number”);
Int eno = Integer.parseInt(br.readLine());
System.out.println(“Enter emp name”);
String ename = br.readLine();
System.out.println(“Enter emp sal”);
Float esal = Float.parseFloat(br.readLine());
System.out.println(“Enter emp address”);
String eaddr = br.readLine();
st.executeUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);
System.out.println(“read successfully inserted”);
System.out.println(“one more record[y/n]);
String option = br.readLine();
If(option.equals(“n”))
break;
}
}
}
import java.io.*;
public class InsertTableEx
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con = DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”durga”);
Statement st = con.createStatement();
while(true)
{
System.out.println(“Enter emp number”);
Int eno = Integer.parseInt(br.readLine());
System.out.println(“Enter emp name”);
String ename = br.readLine();
System.out.println(“Enter emp sal”);
Float esal = Float.parseFloat(br.readLine());
System.out.println(“Enter emp address”);
String eaddr = br.readLine();
st.executeUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);
System.out.println(“read successfully inserted”);
System.out.println(“one more record[y/n]);
String option = br.readLine();
If(option.equals(“n”))
break;
}
}
}
0 comments:
Post a Comment