155. What are the two levels of acheiving synchronization?
- Method level
- Block level
A thread may notify another thread that the task has been completed. This communication between threads is known as interthread communication.
- wait()
- notify()
- notifyAll()
- JDBC Application layer
- JDBC Driver layer
159. In which package jdbc api classes and interfaces are available?
The JDBC API classes and interfaces are available in the java.sql and the javax.sql packages.
160. What are the commonly used classes and interfaces in JDBC api?
The commonly used classes and interfaces in the JDBC API are:
- DriverManager class: Loads the driver for a database.
- Driver interface: Represents a database driver. All JDBC driver classes must implement the Driver interface.
- Connection interface: Enables you to establish a connection between a Java application and a database.
- Statement interface: Enables you to execute SQL statements.
- ResultSet interface: Represents the information retrieved from a database.
- SQLException class: Provides information about the exceptions that occur while interacting with databases.
161. What are the steps to create JDBC application?
- Load a driver
- Connect to a database
- Create and execute sql statements
- Handle exceptions
162. How do we load a driver in jdbc?
Loading a Driver can be done in two ways:
Programmatically:
- Using the forName() method
- Using the registerDriver()method
Manually:
- By setting system property
163. How do we load driver programmatically?
Using the forName() method
- The forName() method is available in the java.lang.Class class.
- The forName() method loads the JDBC driver and registers the driver with the driver manager.
- The method call to use the the forName() method is:
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Using the registerDriver() method
- You can create an instance of the Driver class to load a JDBC driver. This instance provide the name of the driver class at run time. The statement to create an instance of the Driver class is:
Driver d = new sun.jdbc.odbc.JdbcOdbcDriver();
You need to call the registerDriver() method to register the Driver object with the DriverManager. The method call to register the JDBC-ODBC Bridge driver is:
DriverManager.registerDriver(d);
164. How we connect to a database in JDBC?
Connecting to a Database Using DriverManager.getConnection() method:
Connection getConnection (String <url>)
Connection getConnection (String <url>, String <username>, String <password>)
Connects to given JDBC URL.
throws java.sql.SQLException
Returns a connection object.
Example:
Connection con=DriverManager.getConnection(“jdbc:odbc:MyDSN”,”scott”,”tiger”);
165. What are the different types of statements in JDBC?
- Statement Interface : A Statement object is used for executing a static SQL statement and obtaining the results produced by it.
- Statement createStatement() : returns a new Statement object.
- Prepared Statement Interface: When you use a PreparedStatement object to execute a SQL statement, the statement is parsed and compiled by the database, and then placed in a statement cache. From then on, each time when you execute the same PreparedStatement, it is once again parsed, but no recompile occurs. Instead, the precompiled statement is found in the cache and is reused.
- PreparedStatement prepareStatement(String sql) : returns a new PreparedStatement object.
- CallableStatement Interface : The interface used to execute SQL stored procedures.
166. What is a ResultSet interface?
A ResultSet provides access to a set of data generated by executing SQL query. We can open only one ResultSet per Statement. We can access only one row at a time in sequence.