What
is JDBC?
JDBC is a layer of abstraction that allows users to choose between databases. It allows you to change to a different database engine and to write to a single API. JDBC allows you to write database applications in Java without having to concern yourself with the underlying details of a particular database.
JDBC is a layer of abstraction that allows users to choose between databases. It allows you to change to a different database engine and to write to a single API. JDBC allows you to write database applications in Java without having to concern yourself with the underlying details of a particular database.
What are the two
major components of JDBC?
One implementation interface for database manufacturers, the other implementation interface for application and applet writers.
One implementation interface for database manufacturers, the other implementation interface for application and applet writers.
What is JDBC Driver
interface?
The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendors driver must provide implementations of the java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver.
The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendors driver must provide implementations of the java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver.
What are the common
tasks of JDBC?
1.Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers;
2. Register a driver;
3. Specify a database;
4. Open a database connection;
5. Submit a query;
6. Receive results.
1.Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers;
2. Register a driver;
3. Specify a database;
4. Open a database connection;
5. Submit a query;
6. Receive results.
What packages are
used by JDBC?
There are 8 packages: java.sql.Driver, Connection,Statement, PreparedStatement, CallableStatement, ResultSet, ResultSetMetaData, DatabaseMetaData.
There are 8 packages: java.sql.Driver, Connection,Statement, PreparedStatement, CallableStatement, ResultSet, ResultSetMetaData, DatabaseMetaData.
What are the flow
statements of JDBC?
A URL string
–>getConnection
–>DriverManager
–>Driver
–>Connection
–>Statement
–>executeQuery
–>ResultSet.
A URL string
–>getConnection
–>DriverManager
–>Driver
–>Connection
–>Statement
–>executeQuery
–>ResultSet.
What are the steps involved
in establishing a connection using JDBC in JAVA?
This involves two steps:
(1) loading the driver and
(2) making the connection.
This involves two steps:
(1) loading the driver and
(2) making the connection.
How can you load the
drivers in JDBC?
Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:
Eg.
Class.forName(â€sun.jdbc.odbc.JdbcOdbcDriverâ€);
Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ , you would load the driver with the following line of code:
E.g.
Class.forName(â€jdbc.DriverXYZâ€);
Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:
Eg.
Class.forName(â€sun.jdbc.odbc.JdbcOdbcDriverâ€);
Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ , you would load the driver with the following line of code:
E.g.
Class.forName(â€jdbc.DriverXYZâ€);
What Class.forName
will do while loading drivers of JDBC?
It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.
It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.
How can you make the
connection using JDBC?
In establishing a connection is to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea:
E.g.
String url = “jdbc:odbc:Fredâ€;
Connection con = DriverManager.getConnection(url, “Fernandaâ€, “J8â€);
In establishing a connection is to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea:
E.g.
String url = “jdbc:odbc:Fredâ€;
Connection con = DriverManager.getConnection(url, “Fernandaâ€, “J8â€);
How can you create
JDBC statements?
A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send. For a SELECT statement, the method to use is executeQuery. For statements that create or modify tables, the method to use is executeUpdate. E.g. It takes an instance of an active connection to create a Statement object. In the following example, we use our Connection object con to create the Statement object stmt :
Statement stmt = con.createStatement();
A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send. For a SELECT statement, the method to use is executeQuery. For statements that create or modify tables, the method to use is executeUpdate. E.g. It takes an instance of an active connection to create a Statement object. In the following example, we use our Connection object con to create the Statement object stmt :
Statement stmt = con.createStatement();
How can you retrieve
data from the ResultSet using JDBC?
First JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.
E.g.
ResultSet rs = stmt.executeQuery(â€SELECT COF_NAME, PRICE FROM COFFEESâ€);
Second:
String s = rs.getString(â€COF_NAMEâ€);
The method getString is invoked on the ResultSet object rs , so getString will retrieve (get) the value stored in the column COF_NAME in the current row of rs
First JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.
E.g.
ResultSet rs = stmt.executeQuery(â€SELECT COF_NAME, PRICE FROM COFFEESâ€);
Second:
String s = rs.getString(â€COF_NAMEâ€);
The method getString is invoked on the ResultSet object rs , so getString will retrieve (get) the value stored in the column COF_NAME in the current row of rs
What are the different types of
Statements in JDBC?
1.Statement (use createStatement method)
2. Prepared Statement (Use prepareStatement method) and
3. Callable Statement (Use prepareCall)
1.Statement (use createStatement method)
2. Prepared Statement (Use prepareStatement method) and
3. Callable Statement (Use prepareCall)
How can you use
PreparedStatement in JDBC?
This special type of statement is derived from the more general class, Statement. If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement ’s SQL statement without having to compile it first.
E.g.
PreparedStatement updateSales = con.prepareStatement(â€UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?â€);
This special type of statement is derived from the more general class, Statement. If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement ’s SQL statement without having to compile it first.
E.g.
PreparedStatement updateSales = con.prepareStatement(â€UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?â€);
How to call a Stored
Procedure from JDBC?
The first step is to create a CallableStatement object. As with Statement an and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure;
E.g.
CallableStatement cs = con.prepareCall(â€{call SHOW_SUPPLIERS}â€);
ResultSet rs = cs.executeQuery();
The first step is to create a CallableStatement object. As with Statement an and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure;
E.g.
CallableStatement cs = con.prepareCall(â€{call SHOW_SUPPLIERS}â€);
ResultSet rs = cs.executeQuery();
How to Retrieve
Warnings in JDBC?
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object
E.g.
SQLWarning warning = stmt.getWarnings();
if (warning != null) {
while (warning != null) {
System.out.println(â€Message: †+ warning.getMessage());
System.out.println(â€SQLState: †+ warning.getSQLState());
System.out.print(â€Vendor error code: “);
System.out.println(warning.getErrorCode());
warning = warning.getNextWarning();
}
}
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object
E.g.
SQLWarning warning = stmt.getWarnings();
if (warning != null) {
while (warning != null) {
System.out.println(â€Message: †+ warning.getMessage());
System.out.println(â€SQLState: †+ warning.getSQLState());
System.out.print(â€Vendor error code: “);
System.out.println(warning.getErrorCode());
warning = warning.getNextWarning();
}
}
How to Make Updates
to Updatable Result Sets in JDBC?
Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement method.
E.g.
Connection con = DriverManager.getConnection(â€jdbc:mySubprotocol:mySubNameâ€);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = (â€SELECT COF_NAME, PRICE FROM COFFEESâ€);
Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement method.
E.g.
Connection con = DriverManager.getConnection(â€jdbc:mySubprotocol:mySubNameâ€);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = (â€SELECT COF_NAME, PRICE FROM COFFEESâ€);
What is
Serialization and deserialization in JAVA Programming?
Serialization is the process of writing the state of an object to a byte stream.
Deserialization is the process of restoring these objects.
Serialization is the process of writing the state of an object to a byte stream.
Deserialization is the process of restoring these objects.
How you restrict a
user to cut and paste from the html page using JAVA Programing?
Using JavaScript to lock keyboard keys. It is one of easiest solutions.
Using JavaScript to lock keyboard keys. It is one of easiest solutions.
No comments:
Post a Comment