JDBC   «Prev 

Basic SQL Commands

Question: What will the following code fragment print when compiled and run?
Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
try(Statement stmt = c.createStatement();){
  ResultSet rs = stmt.executeQuery("select * from STUDENT");
  while(rs.next()){
    System.out.println(rs.getString(1));
  }
}
catch(SQLException e){
  System.out.println("Exception ");
}

(Assume that items not specified such as import statements and try/catch block are all valid.)
  1. It will throw an exception if the first column of the result is not a String.
  2. It will throw an exception every time it is run irrespective of what the query returns.
  3. It will print the value for the first column of the result and if there is no row in STUDENT table, it will not print anything.
  4. It will not compile.


Answer: c
Explanation:
a. Since Object class has a toString() method, any type can be converted to a String.
So this cannot be a cause of the exception. If you use getInt() or getDouble and if the column value cannot be converted to an int or double, then that will cause an Exception.

1) Create is the sql statement to create table
1) Create is the SQL statement to handle the creation of database entities.
The syntax is database-dependent.
Most database systems usually provide (GUI) utilities to create tables without issuing a SQL statement.
The Cloudscape database has such a tool called Cloudview.

2)  SELECT is the SQL statement to select specific rows
2) SELECT is the SQL statement to select specific rows from the database based on your search criteria.


3) Insert is the SQL statement to Add data
3) Insert is the SQL statement to add data to a new row (or rows) in an existing table, or to populate the tables you create.

4)Update is the sql statement to modify data
4) UPDATE is the SQL statement to modify data that has been inserted into the database.
UPDATE <table name>
SET < column list>
WHERE <filter criteria>


5) The delete statement will delete an entire row
5) DELETE is the SQL statement to delete an entire row from the table.