In: Computer Science
I am having a hard time with these questions regarding JDBC and XML.
1)
Which type of driver provides JDBC access via one or more ODBC drivers?
Group of answer choices
a) Type 1 driver
b) Type 2 driver
c) Type 3 driver
d) Type 4 driver
2)
Which of the following provides the application-to-JDBC Manager connection.
a) JDBC Driver
b) JDBC API
c) JDBC Driver API
d) SQL Server
3)
With the code below, indicate JDBC coding best practices that have been violated (this code does compile and run!):
import java.sql.*;
public class StudentData{
public static void main(String[] args) {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(args[2], args[0],
args[1]);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM Student WHERE rollno='" +
args[3] + "'");
while (rs.next()) {
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t ");
System.out.print(rs.getInt(3) + "\t ");
System.out.println(rs.getDate(4));
}
stmt.close();
stmt = null;
rs.close();
rs = null;
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
a) The program imports all of the java.sql packages
b) This program does not properly close database resources
c) The program should catch SQLException
d) The program should not string-concatenate a query, but rather use a PreparedStatement.
e) The program retrieves data from a ResultSet by column number instead of by name
f) The program hardcodes the name of the JDBC driver and the SQL query. These should be externalized from the compilable code.
g) Answer 1, 3 and 5 only
h) Answers 1-6 all apply
4)
In JDBC, it is a good practice to use standard SQL statement and avoid using db specific query until necessary.
a) True
b) False
Answer)
Q1) The type of driver provides JDBC access via one or more ODBC
drivers:
a) Type 1 driver
The Type 1 driver of JDBC is responsible for Java translating the JDBC interface calls to the ODBC calls.
Q2) The following provides the application-to-JDBC Manager connection.
b) JDBC API
The JDBC API gives access to the data from Java programming. Using this we can access the data sources such as traditional, relation databases and others.
Q3.
a) The program imports all of the java.sql packages -- only the required packages should be imported and not all
b) This program does not properly close database resources -- connection conn is not closed after the use
c) The program should catch SQLException -- SQLException should be caught before Generic Exception type
d) The program should not string-concatenate a query, but rather use a PreparedStatement. -- Should use the PreparedStatement rather than writing the query using string-concatenation
e) The program retrieves data from a ResultSet by column number instead of by name -- rs.getInt(1) this is confusing and could create errors while retrieving the data
f) The program hardcodes the name of the JDBC driver and the SQL query. These should be externalized from the compilable code. -- True, the program should not hardcode the database driver details and should draw these from some property file
Thus the correct answer is:
h) Answers 1-6 all apply
Q4. In JDBC, it is a good practice to use standard SQL statement and avoid using db specific query until necessary.
-- True, standard SQL statements should be sued and not db specific query as this means that we have to perform minimal changes to the Java DAO layer.