JDBC Interview Questions and Answers Most Frequently Asked . The following interview Questions are Most Frequently Asked in Interview.
1) What are the steps involved in establishing a JDBC connection?
Answer:
2)How can you load the drivers?
Answer:
We can use Class.forName(Driver Name)
3)What will Class.forName do while loading drivers?
Answer:
- Class.forName method explicitly loads the driver class.
- It is used to create instance of a database driver and register in DriverManager.
- When the driver is loaded it is available for making connection with database based on specified driver.
4)How can you make the connection?
Answer:
By using Connection Class we can connect the database.
Example:
String url = “jdbc:mysql://localhost:3306/”;
String dbName = “sampledb”;
String username = “root”;
String password = “root”;
Connection connection = DriverManager.getConnection(url+dbName ,username, password);
5)How can you create JDBC statements?
Answer:
Statement st = con.createStatement();
st.executeUpdate(“CREATE TABLE SAMPLE_DB”);
How can you retrieve data from the ResultSet?
ResultSet rs = st.executeQuery(“SELECT * FROM SAMPLE_DB”);
while(rs.next()){
System.out.println(“SAMLE_NAME “+rs.getString(1));
}
6)What are the different types of Statements?
Answer:
1)Statement
2)Prepared Statement
7)What is the different between Statement and PreparedStatement?
7)What is the different between Statement and PreparedStatement?
Answer:
Statement:
Execute static SQL Statement and obtaining the results produced by it.
Prepared Statement:
The simple definition is “Pre Compilation”
The user created SQL is sent to the database and complied before execution.
8 ) How can you use PreparedStatement?
8 ) How can you use PreparedStatement?
Answer:
String sql =”select * from sample_db where first_name=?”;
PreparedStatement pst=con.prepareStatement(sql);
pst.setString=(1,”sample_name”);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.print(“Sample_dept is “+rs.getString(2));
}
9) What does setAutoCommit do?
Answer:
When the JDBC connection is successfully created that time the transaction mode is set to aoto-commit by default. When the SQL query getting executed its automattically commit the transtaction.
We can set setAutoCommit is as true or false. Example like connectionObj.setAutoCommit(false); Once the setAutoCommit is set to false you need to commit the SQL statement explicitly based on your requirement.
10) How do I retrieve warnings?
Answer:
SQLWarning object is a sub class of SQLException by using that we can get database access warnings details.
Example:
SQLWarning warnObj = stmt.getWarnings();
if warnObj != null) {
while warnObj != null) {
System.out.println(“Message: ” + warnObj.getMessage());
System.out.println(“SQLState: ” + warnObj.getSQLState());
warnObj = warnObj .getNextWarning();
}
}
11) How do you call a stored procedure from JDBC?
Answer:
We can call Stored procedure by using callablestatement class in java.
Example:
CallableStatement stmt = conn.prepareCall(“{call USER_DETAILS (?, ?)}”);
stmt.setString(1, “samplename”);
stmt.setString(2, “samplepwd”);
stmt.execute();
Example Procedure:
CREATE OR REPLACE PROCEDURE USER_DETAILS (username IN VARCHAR2, password IN VARCHAR2) AS
BEGIN
INSERT INTO users (username, password , email) VALUES (username, password, email);
END USER_DETAILS;