- What technique can be used to load the drivers?
Answer:
Loading the driver or drivers you want to use is involves just one line of code.
If you want to use the JDBC-ODBC Bridge driver, the following code will load it:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will give you the class name to use.
If the class name is jdbc.DriverXYZ , you would load the driver with the following line of code:
Class.forName("jdbc.DriverXYZ");
-
What will
Class.forName
do while loading the drivers?
Answer:
It is used to create an instance of a driver and register it with the DriverManager.
After you have loaded a driver, it becomes available for making a connection with a DBMS.
- How is a JDBC connection established?
Answer:
When establishing a connection, it is necessary to have the appropriate driver connect to the DBMS.
The following line of code illustrates the concept:
String url = "jdbc:odbc:Fred";
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");
-
How can you create JDBC statements?
Answer:
A Statement object is what sends your SQL statement to the DBMS.
Create a Statement object and then execute it while 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
.
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 does one create a query?
Answer:
Create a Statement object and call the Statement.executeQuery method to select data from the database.
The results of the query are returned in a ResultSet object.
Statement stmt = con.createStatement();
ResultSet results = stmt.executeQuery("SELECT data FROM Database ");
-
How can you retrieve data from the ResultSet?
Answer:
Use getter methods to retrieve data from the returned ResultSet object.
ResultSet rs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");
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
.
- How does one navigate the ResultSet?
Answer:
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:
- beforeFirst(): Default position. Puts cursor before the first row of the result set.
- first(): Puts cursor on the first row of the result set.
- last(): Puts cursor before the last row of the result set.
- afterLast() Puts cursor beyond last row of the result set.
Calls to previous moves backwards through the ResultSet.
- absolute(pos): Puts cursor at the row number position where absolute(1) is the first row and absolute(-1) is the last row.
- relative(pos): Puts cursor at a row relative to its current position where relative(1) moves row cursor one row forward.
- What are the different types of JDBC Statements?
Answer:
- Statement (use createStatement method)
- Prepared Statement (Use prepareStatement method)
- Callable Statement (Use prepareCall)
-
If you want to use the percent sign (%) as the percent sign and not have it interpreted as the SQL wildcard used in SQL LIKE queries, how is this written?
Answer:
Use the escape keyword. For example:
stmt.executeQuery("select tax from sales where tax like '10\%' {escape '\'}");
-
How does one escape the ' symbol found in an input line?
Answer:
You may use a method to do so:
static public String escapeLine(String s) {
String retvalue = s;
if (s.indexOf ("'") != -1 ) {
StringBuffer hold = new StringBuffer();
char c;
for(int i=0; i < s.length(); i++ ) {
if ((c=s.charAt(i)) == '\'' ) {
hold.append ("''");
}else {
hold.append(c);
}
}
retvalue = hold.toString();
}
return retvalue;
}
Note that such method can be extended to escape any other characters that the database driver may interpret.