Assignment Day13 Lesson10 JDBC
Assignment Day13 Lesson10 JDBC
Which of the following interfaces can be used to execute a stored procedure on the
database?
A. CallableStatement
B. PreparedStatement
C. ProceduralStatement
D. StoredStatement
Question 2:
B. When a JDBC Connection is created, its commit mode depends on the parameters
used while creating the connection.
Question 2:
B. When a JDBC Connection is created, its commit mode depends on the parameters
used while creating the connection.
Question 3:
Given:
String qr = "insert into STOCK ( ID, TICKER, LTP, EXCHANGE ) values( ?, ?, ?, ?)";
String[] tickers = {"AA", "BB", "CC", "DD" };
You are trying to initialize the STOCK table and for that you need to insert one
row for each of the ticker value in the tickers array. Each row has to be
initialized with the same values except the ID and TICKER columns, which are
different for each row. The ID column is defined as AUTO_INCREMENT and so you need
to pass only 0 for this column.
A.
for(String ticker : tickers)
try(PreparedStatement ps = c.prepareStatement(qr);) {
ps.setInt(1, 0);
ps.setString(2, ticker);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
ps.executeUpdate();
}
B.
try(PreparedStatement ps = c.prepareStatement(qr);) {
for(String ticker : tickers) {
ps.setInt(1, 0);
ps.setString(2, ticker);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
ps.executeUpdate();
}
}
C.
try(PreparedStatement ps = c.prepareStatement(qr);) {
ps.setInt(1, 0);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
for(String ticker : tickers) {
ps.setString(2, ticker);
ps.executeUpdate();
}
}
D.
for(String ticker : tickers)
try(Statement s = c.createStatement(qr);) {
s.executeUpdate("insert into STOCK ( ID, TICKER, LTP, EXCHANGE ) values( 0,
'"+ticker+"', 0.0, 'NYSE')");
}
Question 4:
A JDBC driver implementation must provide implementation classes for which of the
following interfaces?
A. java.sql.DriverManager
B. java.sql.Driver
C. java.sql.Connection
D. java.sql.Statement
E. java.sql.SQLException
F. java.sql.Date
Question 5:
Given:
String qr = "insert into USERINFO values( ?, ?, ?)";
try(PreparedStatement ps = c.prepareStatement(qr);)
{
ps.setObject(0, 1); //1
ps.setObject(1, "Ally A", JDBCType.VARCHAR); //2
ps.setObject(2, "101 main str"); //3
ps.executeUpdate(); //4
ps.setObject(1, "Bob B"); //5
ps.setNull(2, java.sql.Types.VARCHAR); //5
ps.executeUpdate(); //6
}
A. Two rows with the following values will be inserted in the USERINFO table: 1,
Ally A, 101 main str null, Bob B, null
B. Two rows with the following values will be inserted in the USERINFO table: 1,
Ally A, 101 main str 1, Bob B, 101 main str
Question 6:
Given that a table named STOCK exists with nullable columns as follows:
PreparedStatement ps = c.prepareStatement(qr);
ps.setInt(1, 1);
ps.setString(2, "APPL");
What can be inserted in the above code so that a row with values (1, "APPL", null,
null) will be inserted in the STOCK table?
A.
ps.setNull(3, JDBCType.INTEGER);
ps.setNull(4, JDBCType.STRING);
B.
ps.setNull(3, JDBCType.INTEGER);
ps.setNull(4, JDBCType.VARCHAR);
C.
ps.setNull(3, Types.INTEGER);
ps.setNull(4, Types.STRING);
D.
Integer ltp = null;
ps.setInt(3, ltp);
ps.setString(4, ltp);
E.
ps.setNull(3, Types.INTEGER);
ps.setString(4, null);
Question 7:
What can be inserted in the above code so that it will print the GPA value for each
student?
(Assume that items not specified such as import statements and try/catch block are
all valid.)
A. rs.getString(2)
B. rs.getString(3)
C. rs.getInt(2)
D. rs.getInteger(2)
E. rs.getInt("GPA")
Question 8:
D. PreparedStatement allows several additional SQL types such as BLOB and CLOB.
Question 9:
Which of the following method would you use to get a JDBC connection while using
JDBC 4.0 but not while using a previous version of JDBC?
A.
public Connection getConnection1(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
return DriverManager.getConnection(url, p);
}
B.
public Connection getConnection2(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
DriverManager.registerDriver("com.xyz.Driver");
return DriverManager.getConnection(url, p);
}
C.
public Connection getConnection3(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("jdbc.driver", "com.xyz.Driver");
p.setProperty("jdbc.user", userid);
p.setProperty("jdbc.user.password", pwd);
return DriverManager.getConnection(url, p);
}
D.
public Connection getConnection4(String url, String userid, String pwd) throws
Exception{
Class.forName("com.xyz.Driver");
return DriverManager.getConnection(url, userid, pwd);
}
E.
public Connection getConnection5(String url, String userid, String pwd) throws
SQLException{
return DriverManager.getConnection(url, userid, pwd);
}
Question 10:
A. The JDBC Driver must be loaded explicitly in the code before attempting to
create a connection to the database.
B. Starting JDBC 4.0, the JDBC Driver class is not required any more.
C. Starting JDBC 4.0, the JDBC Driver class is not required to be loaded explicitly
in the code any more.
D. JDBC 4.0 allows loading multiple JDBC Drivers while previous versions did not.
Question 11:
Given that a code fragment has just created a JDBC Connection and has executed an
update statement, which of the following statements is correct?
C. Changes to the database will be committed right after the update statement has
completed execution.
D. Changes to the database will be committed when another query (update or select)
is fired using the connection.
Question 12:
A. jdbc:derby://localhost:1527/sample
B. //jdbc://derby://localhost:1527/sample
C. http://jdbc:mysql:localhost/sample
D. https://mysql.com:3306/sample
Question 13:
Which of the following code fragments can you use to get a JDBC connection?
A.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
DriverManager.setClientInfo(p);
Connection c = DriverManager.getConnection(url);
B.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
Connection c = DriverManager.getConnection(dburl);
c.setClientInfo(p);
c.connect();
C.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
Connection c = DriverManager.getConnection(dburl, p);
D.
Connection c = DriverManager.getConnection(url);
c.connect(userid, pwd)
Question 14:
Assuming that the query returns exactly 1 row, what will be printed when this code
is run?
(Assume that items not specified such as import statements, DB url, and try/catch
block are all valid.)
Question 15:
You want to use a third party JDBC driver for a database. Which of the following
actions must you take to retrieve a Connection using that driver in your JDBC
program?
E. Load the driver class using Class.forName and then retrieve a connection using
DriverManager.getConnection.
Question 16:
Given:
What statement can be added to the above code so that the update is committed to
the database?
A. con.setAutoCommit(true);
B. con.commit(true);
C. stmt.commit();
D. con.setRollbackOnly(false);
E. No code is necessary.
Question 17:
Question 18:
Given :
TEACHER table in the database with the following data:
try(
Connection c = ds.getConnection(); //assume that ds refers to a DataSource
PreparedStatement selectPS = c.prepareStatement("select TID, SUBJECT from
TEACHER where NAME = ?");
PreparedStatement insertPS = c.prepareStatement("insert into SUBJECT (NAME,
TID) VALUES (?, ?)");
)
{
selectPS.setString(1, "Ally");
ResultSet rs = selectPS.executeQuery();
while(rs.next()){
insertPS.setObject(1, rs.getObject(2), JDBCType.VARCHAR);
insertPS.setObject(2, rs.getObject(1), JDBCType.INTEGER);
insertPS.execute(); //1
}
}
Assuming that a SUBJECT table with no rows also exists, identify correct
statements.
Question 19:
Given:
What statement should be added to the above code so that the update is committed to
the database?
A. stmt.commit();
B. con.commit();
C. stmt.commit(true);
D. con.commit(true);
E. No code is necessary.
Question 20:
Question 21:
Given:
What statement should be added to the above code so that the update is committed to
the database?
A. stmt.commit();
B. con.commit();
C. stmt.commit(true);
D. No code is necessary.
Question 22:
A. jdbc.derby.localhost/sample
B. jdbc://mysql.com/sample
C. http://jdbc.mysql/sample
D. jdbc:oracle:thin:@localhost:1521:mydb
E. jdbc:mysql://192.168.1.10:3306/sample
F. jdbc:xderby:1106:sample