Chap5 Database 1
Chap5 Database 1
29. Identify the invalid method of DriverManager- 35. JDBC stands for ________.
A. registerDriver() A. Java Database Connectivity
B. deregisterDriver() B. java database components
C. getConnection() C. java database controls
D. openConnection() D. None of the above
Answer: D Answer: A
30. In DriverManager class which method is used 36. Native-API driver converts JDBC calls into ____
to establish the connection with specified url A. ODBC calls
A. public static void deregisterDriver(Driver B. vendor specific database network protocol
driver) C. user calls
B. public static void registerDriver(Driver driver) D. native calls of the database API
C. public static Connection getConnection(String Answer: D
url)
D. public static Connection getConnection 37. ODBC Drivers are platform _______________
(String url,String userName,String password) A. Dependent
Answer: C B. Indepndent
C. Dependent,Independent
31. In setXXX() methods used for Prepared- D. All of above
Statement the first argument specifies which Answer: A
value?
38. PreparedStatement interface creates an object times?
that represents a ________________ String sqlInsert = "insert into books values (3001,
A. Compiled SQL Statement 'Gone Fishing', 'Kumar', 11.11, 11)";
B. Precompiled SQL Statement int countInserted = stmt.executeUpdate(
C. Select SQL Statement sqlInsert);
D. Insert SQL Statement System.out.println(countInserted + " records
Answer: B inserted.n");
A. records inserted
39. public class DriverManager extends B. 1 records inserted
A. Object C. null records inserted
B. String D. 2 records inserted
C. Connection Answer: A
D. Statement
Answer: A 45. The _____ package contains classes that help in
connecting to a database, sending SQL
40. Rearrange the steps to connect to the database statements to the database and processing the
in SQL query results.
a. Create the connection object A. connection.sql.*
b. Execute the query B. db.sql.*
c. Close the connection object C. pkg.sql.*
d. Register the driver class D. java.sql.*
e. Create the statement object Answer: D
A. a-b-c-d-e
B. d-a-e-b-c 46. The _____ method can be used on a ______
C. e-d-c-b-a object to create its meta data object
D. d-a-c-b-e A. getMetaData(),ResultSet
Answer: B B. executeQuery(),ResultSet
C. executeUpdate(),ResultSet
41. ReslutSetMetaData Interface Object created by D. execute(),ResultSet
_______ method, of _______ interface. Answer: A
A. getMetaData(), ResultSet
B. getMetaData(), Statement interface 47. The _____ driver resolves JDBC call and makes
C. getResultSet(), PreparedStatement interface equivalent ODBC Call
D. getMetaData(), DatabaseMetaData interface A. JDBC Native API
Answer: A B. JDBC-Net pure java
C. 100% pure java
42. Return type of executeQuery() method is ____ D. JDBC-ODBC bridge
A. Int Answer: D
B. ResultSet
C. Void 48. The ----------------- method executes a simple
D. Statement query and returns a single Result Set object
Answer: B A. ExecuteUpdate
B. executeQuery()
43. Set XXX() method binds values to the C. execute()
parameters. Where XXX represents D. noexecute()
A. Data Type Answer: B
B. Object
C. Both a & b 49. The ........................ object allows you to
D. None of above execute parameterized queries.
Answer: A A. ResultSet
B. Parametrized
44. What will be output if I execute the code 2 C. PreparedStatement
D. Condition C. after the first row
Answer: C D. previous row from the current position
Answer: A
50. The correct string for loading Jdbc-Odbc bridge
driver using Class.forName() method is: 56. The Type 3 architecture is _______________
A. jdbc.odbc.JdbcOdbcDriver A. JDBC-Net pure Java
B. microsoft.odbc.jdbc.OdbcJdbcDriver B. JDBC-Native API
C. sun.jdbc.odbc.JdbcOdbcDriver C. JDBC-ODBC Bridge Driver
D. microsoft.sun.JdbcOdbcDriver D. All of the above
Answer: C Answer: A
51. The executeQuery( String sql) method is used 57. The valid constant to represent ResultSet type
to execute ______ query & it returns _______ is ___________________
A. CREATE , integer A. TYPE_FORWARD_ONLY
B. INSERT ,single row B. TYPE_SCROLL_SENSITIVE
C. SELECT , ResultSet object C. TYPE_SCROLL_INSENSITIVE
D. DELETE , ResultSet object D. All of the mentioned
Answer: C Answer: D
52. The JDBC-ODBC bridge driver resolves _______ 58. To execute a SELECT statement "select * from
& makes equivalent _________ Address" on a Statement object stmt, use
A. JDBC call, ODBC call A. stmt.execute("select * from Address");
B. ODBC call, ODBC call B. stmt.executeQuery("select * from Address");
C. ODBC call, JDBC call C. stmt.executeUpdate("select * from Address");
D. JDBC call, JDBC call D. stmt.query("select * from Address");
Answer: A Answer: B
53. The Prepared statement object is used ______ 59. To retrieve a Connection object,………………….
A. to call the stored procedures from within a Method is used.
JDBC application program A. connection( )
B. IN, OUT, INOUT parameters when calling a B. establish( )
stored procedure C. getConnection( )
C. to execute dynamic SQL statement against D. getConnected( )
the database. Answer: C
D. to both pass information and retrieve
information from a stored procedure. 60. Type 1 driver is also known as
Answer: C A. JDBC-Native API
B. JDBC-ODBC Bridge Driver
54. The PreparedStatement__________symbol is C. JDBC-net pure java
placeholder that is replaced by the input D None of these
parameter at seen time. Answer: B
A. *
B. ? 61. Using JDBC you can send SQL, PL/SQL
C. # statements ___________________
D. @ A. to almost any relational database.
Answer: B B. to MySQL database
C. to MSAccess
55. The ResultSet object maintains a cursor D. to Oracle
pointing to a particular row of data. Initially, Answer: A
cursor points to ___________
A. before the first row 62. Valid constant to set ResultSet as Scrollable is
B. next row from the current position _____________________
A. ResultSet.TYPE_FORWARD_ONLY B. A DataSource is the Java representation of a
B. ResultSet.TYPE_SCROLL_SENSITIVE physical data source
C. ResultSet.TYPE_SCROLL_INSENSITIVE C. A DataSource is a registry point for JNDI-
D. All of above services
Answer: D D A DataSource is a factory of connections to a
physical data source
63. What are types of JDBC driver? Answer: D
A. JDBC-ODBC bridge
B. JDBC native API 69. What maintains a cursor that points to a
C. pure java JDBC network particular row of data. Initially, cursor points to
D. all the above before the first row?
Answer: D A. Connection interface
B. Statement interface
64. What contains object specific to database, and C. ResultSet object
used to create Connection object from the D. None of the Above
options? Answer: C
A. Driver Interface / Connect()
B. ResultSet 70. What MySQL property is used to create a
C. Statement surrogate key in MySQL?
D. Driver Manager A. UNIQUE
Answer: A B. SEQUENCE
C. AUTO_INCREMENT
65. What is error in this statement? D. None of the above -- Surrogate keys are not
class.forName("sun.jdbc.odbc.jdbcodbcDriver"); implemented in MySQL.
url="xyz" connection con= DriverManager. Answer: C
getConnection(url);
A. unidentified string 71. What the following statement returns?
B. object not created properly SQLWarning warning = statement.
C. no correction in code getWarnings();
D. driver name is wrong System.out.println("Message: " + warning.
Answer: D getMessage());
A. Output of Warning
66. What is return type of execute() method? B. Warning number
A. object of ResultSet class C. Description about SQL warning
B. integer D. Displays "Message"
C. void Answer: C
D. boolean
Answer: D 72. What will be the result of executing the
following code snippet?
67. What is the disadvantage of Type-4 Native- Statement sql2 = con.createStatement();
Protocol Driver? int result=sql2.executeUpdate("Select * From
A. At client side, a separate driver is needed for Publishers");
each database. A. Will throw an exception
B. Type-4 driver is entirely written in Java B. Will result in compilation error
C. The driver converts JDBC calls into vendor- C. Will return the number of rows retrieved from
specific database protocol Publishers table
D. It does not support to read MySQL data. D. Will return null
Answer: A Answer: B
(Error: statement does not generate a row count)
68. What is, in terms of JDBC, a DataSource?
73. When a connection is created, by default it is in
A. A DataSource is the basic service for
managing a set of JDBC drivers _____________
A. rollback mode the database?
B. execute mode A. DatabaseSetMetaData
C. select mode B. DatabaseMetaData
D. auto commit mode C. ResultSetMetaData
Answer: D D. ResultSet
Answer: B
74. Where is Metadata stored in MYSQL?
A. In MYSQL database Metadata 81. Which is a correct component of JDBC API
B. In MYSQL database Metasql A. DriverManager
C. In MYSQL database Mysql B. Driver
D. None of above C. PreparedStatement
Answer: C D. both a & b
Answer: D (?)
75. Which driver is called as a thin driver in JDBC?
A. Type-1 Driver 82. Which is not a Navigation method of ResultSet
B. Type-2 Driver interface
C. Type-3 Driver A. next()
D Type-4 Driver B. last()
Answer: D C. beforeLast()
D. beforeFirst()
76. Which driver type use middleware Answer: C
server(Application Server)?
A. Type 1 83. Which is the Jdbc-Odbc bridge driver?
B. Type 2 A. microsoft.jdbc.odbc.JdbcOdbcDriver
C. Type 3 B. sun.jdbc.odbc.JdbcOdbcDriver
D. Type 4 C. com.mysql.jdbc.Driver
Answer: C D. oracle.jdbc.odbc.JdbcOdbcDriver
Answer: B
77. Which from the below use "middle-tier"
A. Three-Tier 84. Which JDBC driver maps the JDBC calls to the
B. Four-Tier native method calls, which are
C. n-Tier passed to the local native Call Level Interface
D. All of above (CLI)?
Answer: D A. Native-API-Partly-Java driver
B. JDBC-ODBC Bridge driver
78. Which interface have following methods C. Native Protocol Pure-Java driver
clearParameter(),setInt(),execute(). D. JDBC-Net-Pure-Java driver
A. CallableStatement interface Answer: A
B. connection interface
C. preparedStatement interface 85. Which JDBC driver Type(s) can you use in a
D. statement interface three tier architecture and if the web server
Answer: C and the DBMS are running on the same
machine
79. Which Interface is used to execute dynamic SQL A. Type-1
statements? B. Type-2
A. Resultset C. Type3 and 4
B. PreparedStatement D. All of above
C. Statement Answer: D
D. ResultSetMetaData
Answer: B 86. Which JDBC driver Type(s) is (are) the JDBC-
ODBC bridge?
80. Which interface gives information related to A. Type-1
B. Type-2 D. executeQuery()
C. Type-3 Answer: A
D. Type-4
Answer: A 93. Which object allows you to create
parameterized queries?
87. Which JDBC driver Types are for use over A. ResultSet
communications networks? B. PreparedStatement
A. Type 3 only C. Statement
B. Type 4 only D. Condition
C. Both Type 3 and Type 4 Answer: B
D. Neither Type 3 nor Type 4
Answer: C 94. Which of the following class is used for
establishing connection?
88. Which list gives a quick way to determine A. DriverManager
which Connection method is appropriate for B. Connection
creating different types of SQL statements? C. Statement
A.createStatement D. ResultSet
B.prepareStatement Answer: A
C.prepareCall
D.All of the mentioned above 95. Which of the following function is used to find
Answer: D the column count of the particular ResultSet?
A. getMetaData()
89. Which maintains a cursor pointing to a B. MetaData()
particular row of data initially ,cursor points to C. getColumnCount()
before the first row ? D. getCount()
A. Connection interface Answer: C
B. Statement interface
C. Resultset interface 96. Which of the following is a following statement
D. None of the above is a prepared statements?
Answer: C A. Insert into department values(?,?,?)
B. Insert into department values(x,x,x).
90. Which method is used for saving records in a C. SQLSetConnectOption(conn, SQL
database? AUTOCOMMIT, 0)
A. commit() D. SQLTransact(conn, SQL ROLLBACK)
B. commit once() Answer: A
C. single Commit()
D. None of the Above 97. Which of the following is not a
Answer: A component/class of JDBC API?
A. Statement
91. Which method is used to send CREATE TABLE B. ResultSet
queries to database using JDBC? C. SQLException
A. executeQuery() D ConnectionPool
B. executeUpdate() Answer: D
C. createTable()
D. insertNewTable() 98. Which of the following is not method of
Answer: B DriverManager class
A. registerDriver(Driver driver)
92. Which method of class is used to register & B. getConnection(String url)
dynamically load the driver class? C. deregisterDriver(Driver driver)
A. forName() D. None of these
B. getConnection() Answer: D
C. createStatement()
99. Which of the following is not true for pure Java driver that uses a middleware driver
Statement and PreparedStatment? to connect to a databse?
A. Standard Statement is used to create a Java A. Type-1 Driver
representation of a literal SQL statement and B. Type-2 Driver
execute it on the database. A Prepared- C. Type-3 Driver
Statement is a precompiled statement. D. Type-4 Driver
B. Statement use for general purpose access and Answer: C
PreparedStaement use when you plan to use
SQL Statement Many times. 105. Which of the following method is used for
C. Statement is use when you are using static SELECT query?
SQL Statements and PreparedStatement is use A. execute()
to access parameter at run time B. execute(String sql);
D. None of above C. executeUpdate()
Answer: D D. executeQuery(String sql);
Answer: D
100. Which of the following is the correct syntax of
executeQuery() method? 106. Which of the following method is used for
A. int executeQuery() sending SQL statements
B. int executeQuery(String str) A. Statement
C. ResultSet executeQuery() B. Callablestatement()
D. ResultSet executeQuery(String str) C. Preparedstament
Answer: D D. Both C & B
Answer: A
101. Which of the following is true about JDBC
architecture? 107. Which of the following method is used to load
A. JDBC API layer provides the application-to- drivers for database?
JDBC Manager connection. A. forName()
B. JDBC Driver API layer supports the JDBC B. loadDriver()
Manager-to-Driver Connection C. installDriver()
C. Both of the above. D. None of these
D. None of the above. Answer: A
Answer: C
108. Which of the following statement is true in case
102. Which of the following is used to access the security consideration for JDBC:
database server at time of executing the i] Normal unsigned applets are generally
program and get the data from the server untrustworthy.
accordingly? ii] Downloaded driver should be used for
A. Embedded SQL connection purpose only.
B. Dynamic SQL iii] No automatic or implicit use of local
C. SQL declarations credentials while connecting to remote database
D. SQL data analysis servers.
Answer: B iv] Untrusted applets should not be permitted to
access to local database data.
103. Which of the following is used to call stored A. i and ii true
procedures on the database? B. ii and iii true
A. Statement C. i and iii true
B. PreparedStatement D. All of the above
C. CallableStatement Answer: D
D. None of the above
Answer: C 109. Which of the following statements is true
concerning JDBC?
104. Which of the following JDBC Driver refers to a A. It is Useful for all object-oriented language
B. It consist three main layers. B. javax.net
C. It is similar concept to ODBC. C. java.jdbc
D. It is language independent. D. java.lang
Answer: C Answer: A
110. Which of the following type of JDBC driver is 114. Which packages contain the JDBC classes?
the fastest one? A. java.jdbc and javax.jdbc
A. Type 4 B. java.jdbc and java.jdbc.sql
B. Type 2 C. java.sql and javax.sql
C. Type 3 D. java.rdb and javax.rdb
D. Type 1 Answer: C
Answer: A
115. Which statements about JDBC are true?
111. Which of the following type of JDBC driver i] JDBC is an API to connect to relational-, object-
should be used when you are accessing one and XML data sources
type of database, such as Oracle, Sybase, or ii] JDBC stands for Java DataBase Connectivity
IBM? iii] JDBC is an API to access relational databases,
A. Type 1 spreadsheets and flat files
B. Type 2 iv] JDBC is an API to bridge the object-relational
C. Type 3 mismatch between OO programs and relational
D. Type 4 databases
Answer: D A. i-true, iv-true
B. ii-true, iii-true
112. Which one of the following will not get the data C. i-true, ii-true
from the first column of ResultSet rs, returned D. iii-true, iv-true
from executing the following SQL statement: Answer: B
SELECT name, rank, serial No FROM employee?
A. rs.getString(0) 116. You can add a row using SQL in a database with
B. rs.getString("name") which of the following?
C. rs.getString(1) A. ADD
D. None of the Above B. CREATE
Answer: A C. INSERT
D. MAKE
113. Which package need to be imported for Answer: C
establishing connection with database
A. java.sql
Sr.
QUESTION
No.
1. _________________________ identify component
A. Application Server
B. DBMS
C. Host
D. Proxy
Answer: B
2. Consider above table and to extract marks of student whose name AJP, which will be correct
form of ResultSet class method:
4. Analyse the following code and fill the appropriate statement in ___________________.
import java.sql.*;
class Record
{
public static void main(String args[])throws Exception
{
Class.forName("jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn");
PreparedStatement stmt=con.prepareStatement("select * from emp where EmpName=?");
stmt.______________
ResultSet rs=stmt.executeQuery();
System.out.println("EmpNametDesignationtSalary");
where(rs.next())
{
System.out.println(rs.getString(1)+"t"+rs.getString(2)+"t"+rs.getInt(3));
}
con.close();
}
A. setString(0,"Rahul");
B. setString(0,'Rahul');
C. setString(1,"Rahul");
D. setString(1,'Rahul');
Answer: C
}
con.close();
}
}
A. Missing semicolon
B. Missing {
C. Missing driver registration statement
D Missing }
Answer: C
Connection con=DriverManager.getConnection(url);
System.out.println("Connection to database is created");
Statement st=con.createStatement();
System.out.println("Statement Object is created");
catch(SQLException e)
{
System.out.println("SQL Error");
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
A. { missing
B. ; missing
C. URL class missing
D. } missing
Answer: D
8. Choose missing statements in following code from given options. import java.sql.*;
class StudentData
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:stud");
PreparedStatement ps = con.prepareStatement("select *from Student where Marks > ?");
ps.setInt(1,70);
ResultSet rs = ps.*******************;
System.out.println("Students having marks > 70 are:");
while(rs.next())
System.out.println(rs.getString(2));
con.close();
}
catch(Exception e){ }
}
}
A. executeQuery()
B. execute()
C. executeUpdate()
D. resultSet()
Answer: A
B.
PreparedStatement pstmt = null;
try
{
String SQL = "Update Employees SET age = ? WHERE id = ? ";
pstmt = conn. prepareStatement (SQL);
…
}
catch (SQLException e )
{ … }
finally
{
pstmt. Close ();
}
C.
prepareStatement pstmt = null;
try
{
String SQL = "Update Employees SET age = ? WHERE id = ? ";
pstmt = conn. prepareStatement (SQL);
…
}
catch (SQLException e )
{ … }
finally
{
pstmt. Close ();
}
D.
both a and c
Answer: B
10. Choose the correct option to establish a connection to database named student and display
its contents.
A. import java.sql.*;
class OracleCon{
public static void main(String args[])throws Exception
{
try{
Class.forName("oracle.jdbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
C. import java.sql.*;
class Ddemo1
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:ODSN"," "," ");
Statement s=c.createStatement();
Result rs=s.executeQuery("Insert into table STUDENT");
}
D. import java.sql.*;
class Ddemo1
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:ODSN"," "," ");
ResultSet rs=s.executeQuery("select * from stud");
}
Answer: A
A.
B.
C.
D.
Answer: A
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
A.
Total columns: 2
Column Name of 1st column: empid
Column Type Name of 1st column: INTEGER
B.
Column Name of 1st column: empid
Column Type Name of 1st column: INTEGER
C.
Total columns: 2
Column Name of 1st column: empid
D.
Total columns: 2
Column Type Name of 1st column: INTEGER
Answer: A
14. Consider the following code, Find the missing statement to get correct output.
public class DeleteRecords
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DATA");
String query = "delete from tannis";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
15. Consider the following code.What should be the correction done in the code to get correct
output?
import java.sql.*;
class mytable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = ("jdbc:odbc:mystudtable");
Connection con=DriverManager.getConnection(url);
System.out.println("ConnectionEstablished");
Statement studste = con.createStatement();
String studqry = "create table student(rno int,name char)";
studste .execute(qry);
}
catch(SQLException e)
{
System.out.println("databse Connection error.....");
}
catch(Exception e)
{
System.out.println("Other error.....");
}
}
A. replace statement studste.execute(qry) with studste.execute(studqry)
B. missing }
C. missing ;
D. Both option i and ii
Answer: D
16. Consider the following code.What will be student table data after executing this code as table
was empty.
import java.sql.*;
public class mytableins
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String s = ("jdbc:odbc:mystudtable");
Connection con=DriverManager.getConnection(s);
System.out.println("Connection Established");
PreparedStatement studste = con.prepareStatement("insert into student values(2,'Rani')");
studste .executeUpdate();
Statement s1 = con.createStatement();
ResultSet Rset = s1.executeQuery( "select * from student");
System.out.println("Roll No ....Name");
while (Rset.next())
{
int studid = Rset.getInt("rno");
String studname = Rset.getString("name");
System.out.println(studid + " " +studname); }
}
}
A. two record will be inserted
B. only one record in table
C. table will be empty
D. error occurs
Answer: B
17. Consider the following program, select the statement that should be added to the program to
get correct output.
import java.sql.*;
public class DBDemo5
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN","","");
PreparedStatement s=c.prepareStatement( "update db3 set Name=? where Roll_no=?");
Statement s=c.createStatement( );
s.setString(1,args[0]);
s.setString(2,args[1]);
s.setString(3,args[2]);
ResultSet rs=s.executeQuery("select* from db3");
System.out.println("Name"+"t"+"Roll no"+"t"+"Avg");
while(rs.next())
{
System.out.println(rs.getString(1)+"t"+rs.getInt(2)+"t"+rs.getDouble(3));
}
s.close();
c.close();
}
}
A. s.executeUpdate()
B. c.createStatement( )
C. s.close()
D. c.close()
Answer: A
18. Consider the following program, select the statement that should be added to the program to
get correct output.
import java.sql.*;
public class SampleDemo
{
public static void main(String args[])throws SQLException
{
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN","","");
PreparedStatement s=c.prepareStatement( "update db3 set Name=? where Roll_no=?");
Statement s=c.createStatement( );
s.setString(1,args[0]);
s.setString(2,args[1]);
s.setString(3,args[2]);
ResultSet rs=s.executeQuery("select* from db3");
System.out.println("Name"+"t"+"Roll no"+"t"+"Avg");
while(rs.next())
{
System.out.println(rs.getString(1)+"t"+rs.getInt(2)+"t"+rs.getDouble(3));
}
s.close();
c.close();
}
}
A. s.executeUpdate() Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
B. }
C. s.close() Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
D. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Answer: A
19. Consider the following program, Select the statement that should be added to the program to
get correct output.
import java.sql.*;
try {
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:datastud");
try {
System.out.println("Getting All Rows from a table!");
Statement st = con.createStatement();
ResultSet res =---------------------------("SELECT * FROM college");
System.out.println("student_code: " + "t" + "student_name: ");
while (res.next()) {
int i = res.getInt("stud_code");
String s = res.getString("stud_name");
System.out.println(i + "tt" + s);
}
con.close();
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
}
} catch (Exception e) {
System.out.println("Error:connection not created");
}
}
}
A. st.createStatement( )
B. st.executeUpdate()
C. st.executeQuery()
D. res.executeQuery()
Answer: C
20. Consider the following program, What will be the output of following Code?
import java.sql.*;
public class db15
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:DSN2","","");
Statement s=c.createStatement( );
int n=s.executeUpdtae("delete from db3");
ResultSet rs=s.executeQuery("select* from db3");
System.out.println("Name"+"t"+"Roll no"+"t"+"Avg");
while(rs.next())
{
System.out.println(rs.getString(1)+"t"+rs.getInt(2)+"t"+rs.getDouble(3));
}
s.close();
c.close();
}
}
A. Empty Table
B. Table with All Data
C. Error in ResultSet Statement
D. Error in close()
Answer: A
21. Consider the following program, What should be the correction done in the program to get
correct output?
import java.sql.*;
public class MyDB4
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN");
String s= "delete from Employee where emp_id=2";
Statement st=c.createStatement();
int count=ps.executeUpdate(s);
System.out.println("Data deleted"+count);
s="select * from Employee ";
ResultSet rs= st.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt(1)+" tt "+rs.getString(2));
}
s.close(); c.close();
}
}
A. Missing Semicolon
B. Missing {
C. Missing }
D. Missing parameter
Answer: D
22. Consider the following program ,what should be the correction done in the program to get
the correct output
import java.sql.*;
public class displayemployee
{
public static void main(String[] args)
{
Connection con;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connection established");
}
catch(Exception e)
{
System.out.println("Connection error");
}
try
{
String str="jdbc.odbc:dsn1";
con=DriverManager.getConnection(str,"","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select*from employee");
int n=rs.getMetaData().getColumnCount();
for(int i=1;i<=n;i++)
{
System.out.print(rs.getMetaData().getColumnLabel(i)+'t');
System.out.println("");
while(rs.next())
{
for(int i=1;i<=n;i++)
{
System.out.print(rs.getString(i)+'t');
}
System.out.println("");
}
rs.close();
con.close();
}
catch(SQLException e)
{
System.out.print("SQL error");
}
}
}
A. missing }
B. missing {
C. missing semicolon
D. missing package statement
Answer: A
23. Consider the following program and identify the missing statement.
class Type4_simple
{ public static void main(String[] args)
{try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@pearl:1521:oracle10g";
Connection conn = DriverManager.getConnection(url,"java","java");
System.out.println("successfully connected");
}
catch(Exception e){e.printStackTrace()}
}
}
A. Missing package statement
B. Missing semicolon & Package Statement
C. Missing initialization
D. Missing semicolon
Answer: B
24. Consider the following program What should be the correction done in the program to get
the correct output?
import java.sql.*
class ExecuteQueryTest
{
public static void main(String args[])
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
String url="jdbc:odbc:xyz";
Connection con = DriverManager.getConnection(url);
Statement state =con.createStatement();
System.out.println("Statement object created");
String sql= "select Name,Age from student";
ResultSet rs =state.executeQuery(sql);
String text = " ";
while (rs.next())
{
text+= rs.getString(1) + " " + rs .getInt (2) + 'n' ;
}
System.out.println(text);
}
catch(Exception e)
{ System.out.println(“error”); }
}
}
25. Consider the following program, it has 10 Records in the Table. What will be the output of
following Code?
import java.sql.*;
public class db15
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:DSN2","","");
Statement s=c.createStatement( );
int n=s.executeUpdate("update table set name='Ramesh'");
ResultSet rs=s.executeQuery("select* from db3");
System.out.println("Name"+"t"+"Roll no"+"t"+"Avg");
while(rs.next())
{
System.out.println(rs.getString(1)+"t"+rs.getInt(2)+"t"+rs.getDouble(3));
}
s.close();
c.close();
}
}
A. Ramesh name will be given only for 1st Record
B. The query will not be processed
C. Ramesh name will be given to all the records
D. None Of Above
Answer: A
28. Consider the following program. What should be the correction done to get correct output?
import java.sql.*;
class DBEx
{
public static void main(String args[])
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:mydsn","","");
Statement s =
c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
s.executeUpdate("insert into Student values(8,'H','FYCO')");
c.close();
}catch(Exception e)
{
System.out.println("Exception generated: "+e);
}
}
}
A. Use executeQuery() method instead of executeUpdate() method
B. Use executeInsert() method instead of executeUpdate() method
C. Use executeNewRow() method instead of executeUpdate() method
D. No correction is required
Answer: D
30. Consider the following program.Select the interface to fill in the blank space.
import java.sql.*;
public class Column
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:xyz";
Connection con = DriverManager.getConnection(url);
try
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM Account");
.............................. md = rs.getMetaData();
int col = md.getColumnCount();
System.out.println("Number of Column : "+ col);
System.out.println("Columns Name: ");
for (int i = 1; i <= col; i++)
{
String col_name = md.getColumnName(i);
System.out.println(col_name);
}
}
catch (SQLException s)
{
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e)
{
System.out.println("error");
}
}
}
A. ResultSet
B. ResultSetMetaData
C. MetaData
D. PreparedStatement
Answer: B
31. Consider the following program.What shoud be added to the program to get the correct
output.
import java.sql.*;
32. Consider the following program.What should be the correction done in the program to get
correct output?
import java.sql.*;
public class InsertPre
{
public static void main(String args[])
{
String url = "jdbc:odbc:xyz";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url);
String sql ="insert into Account(Acc_No,Acc_Type,Ammount) values(?,?)";
pst = con.prepareStatement(sql);
pst.setInt(1, 15);
pst.setString(2, "saving");
pst.setInt(3, 30000);
pst.executeUpdate();
pst.close();
sql = "select * from Account";
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next())
{
System.out.print(rs.getInt(1) + "t");
System.out.print(rs.getString(2) + "t");
System.out.println(rs.getInt(3));
}
rs.close();
pst.close();
con.close();
}
catch (Exception e)
{
}
}
}
A. Error in main()
B. Error in close()
C. Error in loop
D. Error in Query
Answer: D
33. Consider the following programe & find the error statement.
import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String url="jdbc:odbc:mydsn";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception ee){System.out.println(ee);}
}
}
A. Connecction not established.
B. Missing Statement st=c.createStatement();
C. Other Error
D. Successfully Run
Answer: B
42. Observe the following code and what will be the output of the program
import java.sql.*;
import java.io.*;
public class Resultset1
{
public static void main(String a[])
{
String s1;
int cnt=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers are properly loded");
String url="jdbc:odbc:abc";
Connection con= DriverManager.getConnection(url);
System.out.println("Connection Sucessful");
s1="Select Comp_name,num,cost,capacity from company order by num";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery (s1);
rs= st.getResultSet();
ResultSetMetaData rsmd= rs.getMetaData();
System.out.println("Total COLOUNMS ="+ rsmd.getColumnCount());
System.out.println("Column no 1 is = " +rsmd.getColumnName(1));
System.out.println("Column no 1 data type is = " + rsmd.getColumnTypeName(1));
System.out.println("Name of table = "+rsmd.getTableName(1));
con.close();
}
catch(SQLException e)
System.out.println("SQL Error...!!!");
}
catch(Exception e)
{
System.out.println("Error Connection Un-sucessful...!!!");
}
}
}
A. The output will give information about rows and coloums
B. The output will give information about ResultSet Metadata
C. The output will give information about database
D. The output will give information about ResultSet table
Answer: B
B. Driver loaded
Record inserted 1
Record Inserted 2
Connection to database created
Statement object created
Record Inserted 3
C. Record inserted 1
Record Inserted 2
Connection to database created
Driver loaded
Statement object created
Record Inserted 3
A. import java.io.*;
public class EmployeeTableUpdate
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:EmployeeDatabase";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
String sql = "update Employee set Name=? where Name=?";
PreparedStatement state = con.prepareStatement(sql);
System.out.println("PreparedStatement Object Created.");
state.setString(1, "Ram");
state.setString(2, "Ramesh");
state.executeUpdate();
System.out.println("Entry Updated.");
System.out.println("Contents Of Database After Updating");
System.out.println("NAMEtAGE");
sql = "select * from Employee";
state = con.prepareStatement(sql);
ResultSet results = state.updateQuery();
String text = "" ;
while(results.next())
{
text += results.getString(1) + "t" + results.getInt(2) + "n";
}
System.out.println(text);
state.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
} } }
B. mport java.sql.*;
public class EmployeeTableUpdate
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:EmployeeDatabase";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
String sql = "update Employee set Name=? where Name=?";
Statement state = con.createStatement(sql);
System.out.println("PreparedStatement Object Created.");
state.setString(1, "Ram");
state.setString(2, "Ramesh");
state.executeUpdate();
System.out.println("Entry Updated.");
System.out.println("Contents Of Database After Updating");
System.out.println("NAMEtAGE");
sql = "select * from Employee";
state = con.Statement(sql);
ResultSet results = state.executeQuery();
String text = "" ;
while(results.next())
{
text += results.getString(1) + "t" + results.getInt(2) + "n";
}
System.out.println(text);
state.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
C. import java.sql.*;
public class EmployeeTableUpdate
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String URL = "jdbc:odbc:EmployeeDatabase";
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
String sql = "update Employee set Name=? where Name=?";
PreparedStatement state = con.prepareStatement(sql);
System.out.println("PreparedStatement Object Created.");
state.setString(1, "Ram");
state.setString(2, "Ramesh");
state.executeUpdate();
System.out.println("Entry Updated.");
System.out.println("Contents Of Database After Updating");
System.out.println("NAMEtAGE");
sql = "select * from Employee";
state = con.prepareStatement(sql);
ResultSet results = state.executeQuery();
String text = "" ;
while(results.next())
{
text += results.getString(1) + "t" + results.getInt(2) + "n";
}
System.out.println(text);
state.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
} } }
D. import java.sql.*;
public class EmployeeTableUpdate
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
Connection con = DriverManager.getConnection(URL);
System.out.println("Connection to database created.");
String sql = "update Employee set Name=? where Name=?";
PreparedStatement state = con.prepareStatement(sql);
System.out.println("PreparedStatement Object Created.");
state.setString(1, "Ram");
state.setString(2, "Ramesh");
state.executeUpdate();
System.out.println("Entry Updated.");
System.out.println("Contents Of Database After Updating");
System.out.println("NAMEtAGE");
sql = "select * from Employee";
state = con.prepareStatement(sql);
ResultSet results = state.executeQuery();
String text = "" ;
while(results.next())
{
text += results.getString(1) + "t" + results.getInt(2) + "n";
}
System.out.println(text);
state.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
} } }
Answer: C
A.
import java.sql.*;
public class MyDB1
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN");
PreparedStatement ps=c.prepareStatement( "insert into Student values(?,?,?)");
int count= ps.executeUpdate();
System.out.println("Data inserted"+count);
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select * from Student");
System.out.println("tName"+"tt"+"Roll no"+"tt"+"Avg n");
while(rs.next())
{
System.out.println("t"+rs.getString(1)+"tt"+rs.getInt(2)+"tt"+rs.getInt(3));
}
s.close();
c.close();
}
}
B.
import java.sql.*;
public class MyDB1
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN");
PreparedStatement ps=c.prepareStatement( "insert into Student values(?,?,?)");
ps.setString(1,args[0]);
ps.setString(2,args[1]);
ps.setString(3,args[2]);
int count= ps.executeUpdate();
System.out.println("Data inserted"+count);
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select* from Student");
System.out.println("tName"+"tt"+"Roll no"+"tt"+"Avg n");
while(rs.next())
{
System.out.println("t"+rs.getString(1)+"tt"+rs.getInt(2)+"tt"+rs.getInt(3));
}
s.close();
c.close();
}
}
C.
import java.sql.*;
public class MyDB1
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN");
PreparedStatement ps=c.prepareStatement( "insert into Student values(?,?,?)");
ps.setString(1,args[0]);
ps.setString(2,args[1]);
ps.setString(3,args[2]);
int count= ps.executeUpdate();
System.out.println("Data inserted"+count);
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("delete * from Student");
System.out.println("tName"+"tt"+"Roll no"+"tt"+"Avg n");
while(rs.next())
{
System.out.println("t"+rs.getString(1)+"tt"+rs.getInt(2)+"tt"+rs.getInt(3));
}
s.close();
c.close();
}
}
D.
import java.sql.*;
public class MyDB1
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:MyDSN");
String a= "insert into Student values(?,?,?)"
PreparedStatement ps=c.prepareStatement();
ps.setString(1,args[0]);
ps.setString(2,args[1]);
ps.setString(3,args[2]);
int count= ps.executeUpdate(a);
System.out.println("Data inserted"+count);
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select* from Student");
System.out.println("tName"+"tt"+"Roll no"+"tt"+"Avg n");
while(rs.next())
{
System.out.println("t"+rs.getString(1)+"tt"+rs.getInt(2)+"tt"+rs.getInt(3));
}
s.close();
c.close();
}
}
Answer: B
B. import java.sql.*;
import java.io.*;
public class DBDemo
{
public static void main(String []args)throws Exception
{
String sql;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:stud";
Connection con=DriverManger.getConnection(url);
Statement st=con.createStatement();
sql="insert into stud_table (roll,name,city) values (1,'ramesh','pune')";
Object msg=st.execute(sql);
System.out.print(msg);
st.close();
}
}
C. Both A & B
D. none of the above
Answer: D
A. Class.forName("JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:IIT");
B. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:IIT");
C. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = con.getConnection("jdbc:odbc:IIT");
D. forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager("jdbc:odbc:IIT");
Answer: B
49. Select the statement that must be added to the following program to get the correct output.
import java.sql.*;
import java.io.*;
class DisTable1
{
public static void main(String args[])
{
int id=0;
String name="",addr="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:mydsn");
Preparedstatement pst=cn.prepareStatement("select * from emp");
System.out.println("EmpIdt"+"Namet"+"Address");
while(rs.next())
{
System.out.print(rs.getInt(1)+"t");
System.out.print(rs.getString(2)+"t");
System.out.print(rs.getString(3)+"n");
}
cn.close();
}catch(Exception e){}
}
}
A. ResultSet rs=pst.executeQuery();
B. Resultset rs=pst.ExecuteQuery();
C. Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","","");
D. None of these
Answer: A
50. Select the statement that should be added to program to get corrected output
import java.sql.*;
import java.io.*;
class Jdbc_ResultSet
{
public static void main(String arg[])
{
String s1;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers loaded");
String url="jdbc:odbc:abc";
Connection con=DriverManager.getConnection(url);
System.out.println("connection established");
s1="select comp_name,num,cost,capacity from company order by num";
Statement st=con.createStatement();
rs=st.getResultSet();
System.out.println("Comp_name"+" "+"num"+" "+"cost"+ " "+"capacity");
System.out.println();
while(rs.next())
{
System.out.println(rs.getString("comp_name")+" "+rs.getInt("num")+" "+rs.getInt("cost")+"
"+rs.getString("capacity"));
}
con.close();
}
catch(SQLException e)
{
System.out.println(" sql error");
}
catch(Exception e)
{
System.out.println(" connection error1");
}
}
}
A. st.close()
B. rs.createStatement()
C. ResultSet rs=st.executeQuery(s1);
D. st.executeUpdate(s1);
Answer: C
51. To delete reord from database table student what modification we have to do in following
program.
import java.sql.*;
public class JdbcDemo1 {
public static void main(String args[]) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mydsn");
Statement stmt = con.createStatement();
int result=st.executeUpdate("delete student where id=1");
System.out.println(result+" records affected");
con.close();
}}
A. int result=("delete from student where id=1");
B. stmt.executeUpdate("delete from student ");
C. int result=stmt.executeUpdate("delete from student where id=1");
D. int result=st.executeUpdate("delete from student where id=1");
Answer: C
52. To display ID, NAME and SALARY of a customer whose salary is greater than 2000 AND age is
less tan 25 years from the CUSTOMERS table, fill the correct query statement in the
********* in the following code. Consider CUSTOMER table with 5 fields such as (ID, NAME,
AGE, ADDRESS, SALARY )
import java.sql.*;
class DeleteAllRecord
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:xyz");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("************");
where(rs.next())
{
System.out.println(rs.getInt(1)+"t"+rs.getString(2)+"t"+rs.getInt(3))
}
con.close();
}
}
A. SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000 AND AGE < 25
B. SELECT * FROM CUSTOMERS WHERE SALARY > 2000 AND AGE < 25
C. SELECT ID, NAME, SALARY FROM CUSTOMERS WHOSE SALARY > 2000 AND AGE < 25
D. SELECT ID, NAME, SALARY FROM CUSTOMERS TABLE WHERE SALARY > 2000 AND AGE <
25
Answer: A
53. To to get the column names and row data from a table. Select the proper statement from the
following AT *____________line
import java.sql.*;
public class SelDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odb c: myDSN ");
Statement st = con.createStatement();
*_________________________________//(Choose correct option for this line)
while(sel.next())
{
String name = sel.getString(1);
String pass = sel.getString(2);
System.out.println(name+" "+pass);
}
}
catch(Exception e)
{
System.out.println("Errooorrrr"+e.getMessage());
}
}
}
A. ResultSet sel = st.executeQuery ("select * from Login");
B. ResultSet sel = con.executeQuery ("select * from Login");
C. ResultSet con = sel.executeQuery ("select * from Login");
D. none of the above
Answer: A
54. try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException cnfe)
{
System.err.println("Error loading driver: " + cnfe);
}
A. Will load two different drivers
B. Will load only one drivers
C. Will throw error object
D. None of the above
Answer: A
55. What correction should be done in the program to get corrected output
import java.sql.*;
import java.io.*;
class JdbcP
{
public static void main(String arg[])
{
try
{
DataInputStream in=new DataInputStream(System.in);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers loaded");
String url="jdbc.odbc.abc";
Connection con=DriverManager.getConnection(url);
System.out.println("connection established");
PreparedStatement ps=con.prepareStatement("create table movie6(num Integer,mvnm
Varchar(30))");
ps.executeUpdate();
System.out.println("Table Created");
String sql="insert into movie6 VALUES(?,?)";
PreparedStatement ps2=con.prepareStatement(sql);
System.out.println("Enter movie code");
int mcd=Integer.parseInt(in.readLine());
ps2.setInt(1,mcd);
System.out.println("Enter movie name");
String name=in.readLine();
ps2.setString(2,name);
ps2.executeUpdate();
con.close();
}
catch(SQLException e)
{
System.out.println(" sql error");
}
catch(Exception e)
{
System.out.println(" connection error1");
}
}
}
A. Class.forName("sun.jdbc.odbc.JdbcOdbcDriverManger");
B. Class.forName("sun:jdbc:odbc:JdbcOdbcDriver");
C. String url="jdbc:jdbcodbcDriver:abc";
D. String url="jdbc:odbc:abc";
Answer: D
System.out.println("Data Inserted");
con.close();
}
catch(SQLException e)
{
System.out.println("SQL Error Has Occured");
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
A. to register the driver
B. to create the connection with database
C. to close the connection
D. None of above
Answer: A
59. What should be the correction done in the following program to get correct output?
import java.sql.*;
import java.io.*;
class DemoInsert
{
public static void main(String args[])
{
int u=0;
try
{
Class.ForName("sun.jdbc.odbc.JdbcOdbcDriver");
String myURL="jdbc:odbc:MyDSN";
Connection con=DriverManager.getConnection(myURL);
Statement st=con.createStatement();
c=st.executeUpdate("insert into student values(1,50)");
if(u!=0)
System.out.println("Record inserted");
else
System.out.println("Record NOT inserted");
con.close();
}catch(Exception e){}
}
}
A. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
B. Class.Forname("sun.jdbc.odbc.JdbcOdbcDriver");
C. Class.forName("sun.jdbc.odbc.jdbcodbcDriver");
D. Missing ;
Answer: A
61. Which is the correct method for updating table rows from given option.
import java.sql.*;
public class Updatedbase
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:data");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
St.********("Update emp set name1='jack' where id=2");
System.out.println("nnafter change");
rs=st.executeQuery("select * from emp");
while(rs.next())
{
System.out.println("id:"+rs.getInt("id"));
System.out.println("salary:"+rs.getString("salary"));
}
con.close();
}
catch(SQLException e){}
catch(Exception e)
{ }
} }
A. executeUpdate()
B. executeQuery()
C. execute()
D. ExecuteUpdate()
Answer: A
62. Which of the following program used to delete single row from table student having ID=15 in
following table using prepareStatement method?
A.
import java.sql.*;
class ConDemo5
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:samarth");
PreparedStatement ps=con.prepareStatement();
ps.executeUpdate("delete from student where ID=15");
ps.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
} } }
B.
import java.sql.*;
class ConDemo5
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:samarth");
PreparedStatement ps=con.prepareStatement();
ps.executeUpdate("delete from student where ID=?");
close();
close();
}
catch(Exception e)
{
System.out.println(e);
} } }
C.
import java.sql.*;
class ConDemo5
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:samarth");
PreparedStatement ps=con.prepareStatement("delete from student where ID=?");
ps.setInt(1,15);
ps.executeQuery();
ps.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
} } }
D.
import java.sql.*;
class ConDemo5
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:samarth");
PreparedStatement ps=con.prepareStatement("delete from student where ID=?");
ps.setInt(1,15);
ps.executeUpdate();
ps.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
} } }
Answer: D
63. Which of the missing code need to be inserted in a given code to delete second row in
ResultSet.
import java.sql.*;
import java.sql.ResultSet.*;
class delrset
{public static void main(String args[])
{try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:stud");
Statement s =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=s.executeQuery("select * from stud");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
System.out.println("After deleting row:");
rs.first();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
while(rs.next())
{System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
}
con.close();
}
catch(Exception e){ }
}
}
A. rs.next( )
rs.delete();
B. rs.first();
rs.next( )
rs.delete();
C. rs.first( );
rs.next( )
rs.deleterow();
D. rs.absolute(2);
rs.deleteRow();
Answer: D
64. Which ONE of the following is missing statement in the program below?
import java.sql.*;
class ExecuteQueryTest
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
String url="jdbc:odbc:aditya";
Connection con = DriverManager.getConnection(url);
System.out.println("connection to database created");
Statement st = con.createStatement();
System.out.println("Statement object created");
String sql="select Name,Age from tanni";
ResultSet results = state.executeQuery(sql);
String text=" ";
while()
{
text +=results.getString(1)+ "" +results .getInt (2)+ 'n';
}
System.out.println(text);
}
catch(SQLException e)
{
System.out.println("SQL error");
}
catch(Exception e)
{
System.out.println("error");
} } }
A. state.results()
B. results.next()
C. next.results()
D. results()
Answer: B
67. Which statements are required in following program to retrive records from table
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(jdbc:odbc:stud”);
System.out.println(" Records of table...");
stmt = conn.createStatement();
______________________________________________________
__________________________________________________________________
System.out.println("Inserted records into the table...");
System.out.println("Goodbye!");
}}
A. ResultSet rs=stmt.executeQuery("select * from student");
B. ResultSet rs=stmt.executeUpdate("select * from student");
C. ResultSet rs=stmt.execute("select from student");
D. None of these
Answer A