JDBC Exam Q & A
JDBC Exam Q & A
JDBC Exam Q & A
b)executeQuery()
c)updateExecute()
d)queryExecute()
Ans) b
b)executeQuery()
c)updateExecute()
d)queryExecute()
Ans) a
b)java.sql.Driver
c) a and b
Ans) a
b)java.sql.Driver
c) a and b
Ans) b
Q5) What is the correct sequence to create a database connection?
i. Import JDBC packages.
Ans) b
Explanation: To create a database connection in Java, we must follow the sequence given below:
Ans) a
b)executeUpdate()
c)executeQuery()
d)getConnection()
Ans) d
b)PreparedStatement
c)QueryStatement
d)CallableStatement
Ans) c
Ans) b
Q10) What should be the correct order to close the database
resource? What should be the correct order to close the database
resource?
a)Connection, Statements, and then ResultSet
Ans) d
Ans) a
Q12) How many stages are used by Java programmers while using
JDBC in their programs?
a)3
b)2
c)5
d)6
Ans) d
Explnation:
Connecting to a database
a)Variable
b)Object
c)Data type
d)Keyword
Ans) c
Q14) Which data type is used to store files in the database table?
a)BLOB
b)CLOB
c)File
d)Both a and b
Ans) b
b)ResultSet Interface
c)Connection Interface
d)RowSet Interface
Ans) c
b)1
c)3
d)Multiple
Ans) d
Q17)Draw JDBC architecture for the databases oracle,mysql and
MS-SQL servers?
Ans) jdbc_architecture.png
Classes
a)com.mysql.jdbc.Driver
b)java.sql.DriverManager
c)java.sql.Types
d)java.sql.Date
e)oracle.jdbc.driver.OracleDriver
Interfaces
a)java.sql.Connection
b)java.sql.Driver
c)java.sql.Statement
d)java.sql.PreparedStatement
e)java.sql.CallableStatement
f)java.sql.ResultSet
Statement: Use this for general-purpose access to your database. It is useful when we are using
static SQL statements at runtime. The Statement interface cannot accept parameters.
PreparedStatement: It represents the pre-compiled SQL statements that can be executed multiple
times.
a)NOT NULL
b)UNIQUE
c)PRIMARY KEY
d)FOREIGN KEY
e)CHECK
f)DEFAULT
OR
SELECT MAX(sal) AS salary FROM emp WHERE sal <> (SELECT MAX(sal) FROM emp);
Q41)Write a query to display empno,ename,manager and location
from below data using joins?
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
MGR NUMBER(6)
DEPTNO NUMBER(6)
DEPTNO NUMBER(2)
DNAME VARCHAR2(15)
LOC VARCHAR2(12)
Ans)
SQL> SELECT e.empno, e.ename, e.mgr AS Manager,d.loc as location FROM emp e LEFT JOIN
dept d ON d.deptno = e.deptno ORDER BY d.deptno;
Q42)Write a java program to insert a row into the database?
Ans)
import com.mysql.jdbc.Driver;
import java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
System.out.println(con);
Statement stmt=con.createStatement();
int rowsEffected=stmt.executeUpdate(INSERT_SQL);
System.out.println("Records Insered:"+rowsEffected);
con.close();
}
Q-43) Write a java program to get the data from the database and
display on the console?
Ans)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
Statement stmt=con.createStatement();
while(rs.next())
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
stmt.close();
con.close();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
PreparedStatement pstmt=con.prepareStatement(query);
pstmt.setInt(1,11);
pstmt.setString(2,"ashok");
pstmt.setInt(3,100000);
pstmt.executeUpdate();
pstmt.close();
con.close();
}
Q-45)Write a java program to get the data from the database and
display using PreparedStatement?
Ans)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
PreparedStatement pstmt=con.prepareStatement(query);
ResultSet rs=pstmt.executeQuery(query);
while(rs.next())
System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));
pstmt.close();
con.close();
}
Q-46)Write a java program to insert 5 rows into database table at a
time as a batch?
Ans)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
Statement stmt=con.createStatement();
stmt.executeBatch();
stmt.close();
con.close();
}
Q-47) Write java program to select all records from the table using
CallableStatement?
Ans)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
/*
-> BEGIN
-> END //
*/
System.out.println(cstmt);
con.close();
}
Q-48) Write a java program to call the procedure using IN and OUT
Parameters?
Ans)
import java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
int bookPrice=sc.nextInt();
/*
-> BEGIN
-> END $$
*/
cstmt.setInt(1,bookPrice);
cstmt.registerOutParameter(2,Types.VARCHAR);
cstmt.executeQuery();
System.out.println(cstmt.getString(2));
con.close();
}
Q-49) Write a java program to insert an image into the database?
Ans)
import java.io.*;
import java.sql.*;
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
pstmt.setInt(1,104);
pstmt.setString(2,"stud4");
pstmt.setBinaryStream(3,fis,(int)f.length());
int num=pstmt.executeUpdate();
import java.io.File;
import java.io.FileOutputStream;
import java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(DB_URL,DB_USER,DB_PWD);
Statement stmt=con.createStatement();
rs.next();
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
byte imgarr[]=rs.getBytes(3);
fos.write(imgarr);
fos.close();
con.close();