JDBC interview questions will help you in preparing in your interview. This is the part 2 of JDBC interview (FAQ) series. you can visit part-1 by visiting part 1 of JDBC Interview FAQ In this post I try to cover FAQ about preparedStatement interface, setMaxRows() and setFetchSize() function, How to load JDBC drivers, ways of insertion of file or image, videos or large files.
1. Why Prepared Statements are faster?
Prepared execution is faster than direct execution for statements because the PreparedStatement is compiled. Prepared statements and JDBC driver are linked with each other. We can bind input parameter value with columns in such a way sp amu scope of or chance of SQLInjection. When we execute Connection.prepareStatement(), all the columns binding take place, which we can call it index binding or nameParameter binding, which is moving verbose.
2. What are the differences between setMaxRows(int) and SetFetchSize(int)?
The difference between setFetchSize and setMaxRow are:
setFetchSize(int)defines the number of rows that will be read from the database when the ResultSet needs more rows while setMaxRows(int) method of the ResultSet specifies how many rows a
ResultSetcan contain at a time.
setFetchSize(int), method in the
java.sql.Statementinterface will set the ‘default’ value for all the ResultSet derived from that Statement whereas in setMaxRow(int) default value is 0, i.e. all rows will be included in the ResultSet.
setMaxRowsaffects the client side JDBC object while the setFetchSize affects how the database returns the ResultSet data.
3. How can you load the drivers?
It is very simple and you can load your respective jdbc driver in one line in code.
For example, We want to use the JDBC-ODBC Bridge driver, the following code will load it:
// load driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
you can get more names from the documentation. For instance, if the class name is JDBC.DriverNameClass.forName(“JDBC.DriverName”);
4. What does setAutoCommit do?
setAutoCommit() with either false or true does commit or not commit on database layer. if you supply a false value in the argument it means you are taking control to commit either in batch your when you want to commit. Usually, it uses either for batch or managing of transactions. If you taking control of commit and calling after performing multiple statements it means, you are not only increasing performance saving IOPS (input out per second) too.
setAutoCommit() allowed us to commit the transaction commit state manually the default values of the
setAutoCommit() is true i.e.
You can handle transaction by using
setAutoCommit(false) and later point of time once you done with db operation then you can call
5. What do you mean by JDBC?
The significance are given below:
- Java Database Connectivity (JDBC) is a standard Java API provided by Oracle Java.
- JDBC stands for Java Database Connectivity.
- It’s purpose is to interact with the relational databases or NoSQL database in Java.
- JDBC is having a set of classes and interfaces which can be used from any Java application.
- By using JDBC drivers, it interacts with a databases.
6. How do I insert an a file image/doc/video into a database?
All raw data types should be read and uploaded to the database as an array of bytes, byte.
- Originating from a binary file.
- Read all data from the file using a
- Create a byte array from the read data.
- Use method
setBytes(int index, byte data);of
Happy Learning 🙂