Professional Documents
Culture Documents
What is the query used to display all tables names in SQL Server (Query analyzer)?
Question (JDBC)
Statement is used for static SQL statement with no input and output parameters,
PreparedStatement is used for dynamic SQL statement with input parameters and
CallableStatement is used for dynamic SQL satement with both input and output
parameters, but PreparedStatement and CallableStatement can be used for static SQL
statements as well. CallableStatement is mainly meant for stored procedures.
1. What is JDBC?
JDBC may stand for Java Database Connectivity. It is also a trade mark. 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.
1.Type 1 Drivers
Bridge drivers such as the jdbc-odbc bridge. They rely on an intermediary such
as ODBC to transfer the SQL calls to the database and also often rely on native
code.
2.Type 2 Drivers
Use the existing database API to communicate with the database on the client.
Faster than Type 1, but need native code and require additional permissions to
work in an applet. Good for client-side connection.
3.Type 3 Drivers
Call the database API on the server.Flexible. Pure Java and no native code.
4.Type 4 Drivers
The hightest level of driver reimplements the database network API in Java. No
native code.
1.java.sql.Driver
2.Connection
3.Statement
4.PreparedStatement
5.CallableStatement
6.ResultSet
7.ResultSetMetaData
8.DatabaseMetaData
3. There are three basic types of SQL statements, what are they?
1.Statement
2.callableStatement
3.PreparedStatement
4. What are the flow statements of JDBC?
This involves two steps: (1) loading the driver and (2) making the connection.
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");
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.
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");
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 :
Create a Statement object and calls the Statement.executeQuery method to select data
from the database. The results of the query are returned in a ResultSet object.
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
By default the result set cursor points to the row before the first row of the result set. A
call to next() retrieves the first result set row. The cursor can also be moved by calling
one of the following ResultSet methods:
o beforeFirst(): Default position. Puts cursor before the first row of the result set.
o first(): Puts cursor on the first row of the result set.
o last(): Puts cursor before the last row of the result set.
o afterLast() Puts cursor beyond last row of the result set. Calls to previous moves
backwards through the ResultSet.
o absolute(pos): Puts cursor at the row number position where absolute(1) is the
first row and absolute(-1) is the last row.
o relative(pos): Puts cursor at a row relative to its current position where relative(1)
moves row cursor one row forward.
stmt.executeQuery("select tax from sales where tax like '10\%' {escape '\'}");
Note that such method can be extended to escape any other characters that the database
driver may interprete another way.
You can update a value in a result set by calling the ResultSet.update method on the row
where the cursor is positioned. The type value here is the same used when retrieving a
value from the result set, for example, updateString updates a String value and
updateDouble updates a double value in the result set.
rs.first();
updateDouble("balance", rs.getDouble("balance") - 5.00);
The update applies only to the result set until the call to rs.updateRow(), which updates
the underlying database.
To delete the current row, use rs.deleteRow().
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.
CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery();
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
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.
You may verify that your database supports these types by calling
con.getMetaData().supportsResultSetConcurrency(ResultSet.SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
By default, every JDBC statement is sent to the database individually. To send multiple
statements at one time , use addBatch() method to append statements to the original
statement and call executeBatch() method to submit entire statement.
The return result of the addBatch() method is an array of row counts affected for each
statement executed in the batch job. If a problem occurred, a
java.sql.BatchUpdateException is thrown. An incomplete array of row counts can be
obtained from BatchUpdateException by calling its getUpdateCounts() method.
int itemnumber=400456;
To retrieve an image:
int itemnumber=400456;
byte[] imageBytes;//hold an image bytes to pass to createImage().
A class can be serialized to a binary database field in much the same way as the image.
You may use the code above to store and retrive an object.
int count=0;
Connection con=getConnection();
Statement stmt= con.createStatement();
stmt.executeQuery("select counter from aTable");
ResultSet rs = stmt.getResultSet();
if(rs.next()) {
if(rs.getMetaData().getColumnType(1) == Types.INTEGER) {
Integer i=(Integer)rs.getObject(1);
count=i.intValue();
}
}
rs.close();
Because java.util.Date represents both date and time. SQL has three types to represent
date and time.
o java.sql.Date -- (00/00/00)
o java.sql.Time -- (00:00:00)
o java.sql.Timestamp -- in nanoseconds
Calendar currenttime=Calendar.getInstance();
java.sql.Date startdate= new java.sql.Date((currenttime.getTime()).getTime());
or