|
Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++
Interview Questions
XML
Interview Questions
EJB
Interview Questions
JSP
Interview Questions
.........More
Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
JDBC Interview Questions and Answers
How can I connect to an Excel spreadsheet file using jdbc?
Let's say you have created the following Excel
spreadsheet in a worksheet called Sheet1 (the default
sheet name). And you've saved the file in c:\users.xls.
USERID FIRST_NAME LAST_NAME
pkua Peter Kua
jlsmith John Smith
gh2312 Everett Johnson
chimera Faiz Abdullah
roy6943 Roy Sudirman
Since Excel comes with an ODBC driver, we'll use the
JDBC-ODBC bridge driver that comes packaged with Sun's
JDK to connect to our spreadsheet.
In Excel, the name of the worksheet is the equivalent of
the database table name, while the header names found on
the first row of the worksheet is the equivalent of the
table field names. Therefore, when accessing Excel via
jdbc, it is very important to place your data with the
headers starting at row 1.
1. Create a new ODBC Data Source using the Microsoft
Excel Driver. Name the DSN "excel", and have it point to
c:\users.xls.
2. Type in the following code:
package classes;
import java.sql.*;
public class TestServer
{
static
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e) {
System.err.println(e);
}
}
public static void main(String args[]) {
Connection conn=null;
Statement stmt=null;
String sql="";
ResultSet rs=null;
try {
conn=DriverManager.getConnection("jdbc:odbc:excel","","");
stmt=conn.createStatement();
sql="select * from [Sheet1$]";
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("USERID")+
" "+ rs.getString("FIRST_NAME")+" "+
rs.getString("LAST_NAME"));
}
}
catch (Exception e){
System.err.println(e);
}
finally {
try{
rs.close();
stmt.close();
conn.close();
rs=null;
stmt=null;
conn=null;
}
catch(Exception e){}
}
}
}
Notice that we have connected to the Excel ODBC Data
Source the same way we would connect to any normal
database server.
The only significant difference is in the SELECT
statement. Although your data is residing in the
worksheet called "Sheet1", you'll have to refer to the
sheet as Sheet1$ in your SQL statements. And because the
dollar sign symbol is a reserved character in SQL,
you'll have to encapsulate the word Sheet1$ in brackets,
as shown in the code.
How do I execute stored procedures?
Here is an example on how to execute a stored procedure
with JDBC (to use this in a servlet is the same the only
thing is that you create the connection and callable
statement in the init() of the servlet):
package DBTest;
import java.sql.*;
public class JdbcTest {
private String msDbUrl = "jdbc:odbc:ms";
private String msJdbcClass = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection mcDbAccess;
private CallableStatement msProcedure;
public JdbcTest() {
try {
Class.forName( msDbUrl ).newInstance();
mcDbAccess = DriverManager.getConnection( msJdbcClass,
"milestone", "milestone" );
msProcedure = mcDbAccess.prepareCall(
"{? = call sp_sav_Bom_Header( ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) }"
);
msProcedure.registerOutParameter( 1,
java.sql.Types.VARCHAR );
msProcedure.setInt( 2, -1 );
msProcedure.setInt( 3, 39 );
msProcedure.setString( 4, "format" );
long ltTest = new java.util.Date().getTime();
System.out.println( "Today: " + ltTest );
msProcedure.setTimestamp( 5, new Timestamp( ltTest ) );
msProcedure.setString( 6, "type" );
msProcedure.setString( 7, "submitter" );
msProcedure.setString( 8, "email" );
msProcedure.setString( 9, "phone" );
msProcedure.setString( 10, "comments" );
msProcedure.setString( 11, "label" );
msProcedure.setInt( 12, 52 );
msProcedure.setBoolean( 13, true );
msProcedure.setBoolean( 14, false );
msProcedure.setInt( 15, 53 );
msProcedure.setString( 16, "runtime" );
msProcedure.setString( 17, "configuration" );
msProcedure.setBoolean( 18, true );
msProcedure.setBoolean( 19, false );
msProcedure.setString( 20, "special instructions" );
msProcedure.setInt( 21, 54 );
ResultSet lrsReturn = null;
System.out.println( "Execute: " + (lrsReturn =
msProcedure.executeQuery() ) );
while( lrsReturn.next() ) {
System.out.println( "Got from result set: " +
lrsReturn.getInt( 1 ) );
}
System.out.println( "Got from stored procedure: " +
msProcedure.getString( 1 ) );
} catch( Throwable e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JdbcTest();
}
}
I also tried it by using a native JDBC driver (i-net)
and it also works fine. The only problem we encounter
with JDBC-ODBC bridge is that a stored procedure pads
spaces to the full length of a VARCHAR but the native
JDBC behaves right. Therefore I suggest to use JDBC
native drivers.
The above example uses the MS SQL Server.
Page Numbers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
|