Question

What happens when you try to compile and run the following code? String query = "INSERT...

What happens when you try to compile and run the following code?
String query = "INSERT INTO Invoices (InvoiceDate InvoiceTotal) "
             + "VALUES ('2/10/01', '443.55')";
Statement statement = connection.createStatement();

statement.executeUpdate(query);

a.

A SQLException is thrown at runtime because the executeUpdate method can’t accept a String object.

b.

A SQLException is thrown at runtime because the SQL statement is coded incorrectly.

c.

An error occurs at compile time because the SQL statement is coded incorrectly.

d.

This code compiles and runs without any problems.

  1. Assuming an appropriate database driver is available when the code below is executed, which of the following statements is true?
    private Connection getConnection() throws SQLException {
        String dbUrl = "jdbc:sqlite:products.sqlite";
        Connection connection = DriverManager.getConnection(dbUrl);
        return connection;

    }

    a.

    The driver must be loaded before this code is executed.

    b.

    The getConnection() method automatically loads the driver based on the database URL.

    c.

    The driver is loaded when the variable named dbUrl is declared.

    d.

    The driver won’t be loaded and an SQLException will be thrown.

1 points

Question 8

  1. Given the code below, which of the following statements will delete all records from the Invoices table that have a value of 0 in the InvoiceTotal column?
    String query = "DELETE FROM Invoices " +
                   "WHERE InvoiceTotal = 0 ";

    Statement statement = connection.createStatement();

    a.

    statement.executeQuery(query);

    b.

    statement.execute();

    c.

    statement.deleteRow();

    d.

    statement.executeUpdate(query);

1 points

Question 9

  1. If InvoiceID is an INTEGER type in a SQLite database and InvoiceDate is a TEXT type, which of the following statements returns those columns of the current row in the dueInvoices result set created by the code shown below?
    Statement statement = connection.createStatement();
    String query = "SELECT InvoiceID, InvoiceDate "
                 + "FROM Invoices "
                 + "WHERE InvoiceTotal >= 0 "
                 + "ORDER BY InvoiceDate ASC";
    ResultSet dueInvoices = statement.executeQuery(query);

    dueInvoices.next();

    a.

    int id = dueInvoices.getInt(0);
    String date = dueInvoices.getString(1);

    b.

    int id = dueInvoices.getInt("InvoiceID");
    String date = dueInvoices.getString("InvoiceDate");

    c.

    int id = dueInvoices.getInteger("InvoiceID");
    String date = dueInvoices.getText("InvoiceDate");

    d.

    int id = dueInvoices.getInteger(1);
    String date = dueInvoices.getText(2);

1 points

Question 10

  1. What rows are deleted after the code that follows is executed?
    String sql = "DELETE FROM Customer "
               + "WHERE FirstName = ?";
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setString(1, "John");

    ps.executeUpdate();

    a.

    The first row in the Customers table.

    b.

    The first row in the Customers table where the FirstName column is “John”

    c.

    All rows from the Customers table

    d.

    All rows in the Customers table where the FirstName column is “John”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Q.1.String query = "INSERT INTO Invoices (InvoiceDate InvoiceTotal) "
             + "VALUES ('2/10/01', '443.55')";
Statement statement = connection.createStatement();

statement.executeUpdate(query);

Answer :(B)-A SQLException is thrown at runtime because the SQL statement is coded incorrectly.

"INSERT INTO Invoices (InvoiceDate , InvoiceTotal) "
             + "VALUES ('2/10/01', '443.55')";

Here this statement is missing a comma in between the table column(InvoiceDate , InvoiceTotal) .while compiling code won't raise any error but while runtime it will raise sqlerror for incorrect query.

Q.2 Assuming an appropriate database driver is available when the code below is executed, which of the following statements is true?
private Connection getConnection() throws SQLException {
    String dbUrl = "jdbc:sqlite:products.sqlite";
    Connection connection = DriverManager.getConnection(dbUrl);
    return connection;

}

Answer:(A)-The driver must be loaded before this code is executed.

The programming involved to establish a JDBC connection is fairly simple. Here are these simple four steps −

  • Import JDBC Packages: Add import statements to your Java program to import required classes in your Java code.

  • Register JDBC Driver: This step causes the JVM to load the desired driver implementation into memory so it can fulfill your JDBC requests.

  • Database URL Formulation: This is to create a properly formatted address that points to the database to which you wish to connect.

  • Create Connection Object: Finally, code a call to the DriverManager object's getConnection( ) method to establish actual database connection.

Here we are missing the second step .Register JDBC Driver

You can register using below code:

try {
   Driver myDriver = new oracle.jdbc.driver.OracleDriver();
   DriverManager.registerDriver( myDriver );
}
catch(ClassNotFoundException ex) {
   System.out.println("Error: unable to load driver class!");
   System.exit(1);
}

Q.3.Given the code below, which of the following statements will delete all records from the Invoices table that have a value of 0 in the InvoiceTotal column?
String query = "DELETE FROM Invoices " +
               "WHERE InvoiceTotal = 0 ";

Statement statement = connection.createStatement();

Answer: (d)-statement.executeUpdate(query);

For both update and delete process in database executeUpdate() is used.

Q.4 .If InvoiceID is an INTEGER type in a SQLite database and InvoiceDate is a TEXT type, which of the following statements returns those columns of the current row in the dueInvoices result set created by the code shown below?
Statement statement = connection.createStatement();
String query = "SELECT InvoiceID, InvoiceDate "
             + "FROM Invoices "
             + "WHERE InvoiceTotal >= 0 "
             + "ORDER BY InvoiceDate ASC";
ResultSet dueInvoices = statement.executeQuery(query);

dueInvoices.next();

Answer: (b) int id = dueInvoices.getInt("InvoiceID");
String date = dueInvoices.getString("InvoiceDate");

To fetch table data from resultset we used table column name and desired columntype

Q.5.What rows are deleted after the code that follows is executed?
String sql = "DELETE FROM Customer "
           + "WHERE FirstName = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, "John");
//1 specifies the first parameter in the query
ps.executeUpdate();

Answer:(d)-All rows in the Customers table where the FirstName column is “John”

The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.

Let's see the example of parameterized query:

  1. String sql="insert into emp values(?,?,?)";  

As you can see, we are passing parameter (?) for the values. Its value will be set by calling the setter methods of PreparedStatement.

Hope the answer is helpful

Add a comment
Know the answer?
Add Answer to:
What happens when you try to compile and run the following code? String query = "INSERT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Customer (CustomerId, CustomerName) Employee (EmployeeId, EmployeeName, Salary, SupervisorId) Product(ProductId, ProductName, ListPrice) Orders (OrderId, OrderDate, CustomerId, EmployeeId,...

    Customer (CustomerId, CustomerName) Employee (EmployeeId, EmployeeName, Salary, SupervisorId) Product(ProductId, ProductName, ListPrice) Orders (OrderId, OrderDate, CustomerId, EmployeeId, Total) OrderedProduct (OrderId, ProductId, Quantity, Price) Write the code to complete the methods in OrderJDBC.java (look for TODO items). <---**IN BOLD** throughout code. /* OrderJDBC.java - A JDBC program for accessing and updating an order database on MySQL. */ import java.io.File; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; /** * An application for...

  • I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>"...

    I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>" at line 1, column 169." and "ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist." I do not know why this isn't working. Here is my code: DTCCDatabase.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * This program creates the CoffeeDB database. */ public class DTCCDatabase {    public static void main(String[] args)...

  • Chapter 2 How to use the Management Studio Before you start the exercises... Before you start...

    Chapter 2 How to use the Management Studio Before you start the exercises... Before you start these exercises, you need to install SQL Server and the SQL Server Management Studio. The procedures for doing both of these tasks are provided in appendix A of the book. In addition, you'll need to get the Exercise Starts directory from your instructor. This directory contains some script files that you need to do these exercises. Exercises In these exercises, you'll use SQL Server...

  • There is a problem in the update code. I need to be able to update one...

    There is a problem in the update code. I need to be able to update one attribute of the students without updating the other attributes (keeping them empty). package dbaccessinjava; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.*; import java.io.*; import java.util.Scanner; public class DBAccessInJava { public static void main(String[] argv) {    System.out.println("-------- Oracle JDBC Connection Testing ------"); Connection connection = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu"); String sql="select * from student"; Statement st; PreparedStatement ps; ResultSet rs;...

  • You are a database consultant with Ace Software, Inc., and have been assigned to develop a...

    You are a database consultant with Ace Software, Inc., and have been assigned to develop a database for the Mom and Pop Johnson video store in town. Mom and Pop have been keeping their records of videos and DVDs purchased from distributors and rented to customers in stacks of invoices and piles of rental forms for years. They have finally decided to automate their record keeping with a relational database. You sit down with Mom and Pop to discuss their...

  • Webdaxlpid-12771371-dt-content-rid-89561 4 Delete a student S. Register a counse 7. Check student...

    have to make an SQL Java Database. webdaxlpid-12771371-dt-content-rid-89561 4 Delete a student S. Register a counse 7. Check student egstration 8 Uplond grades 9.Check gale prop table Registered; op table student; reate table Student address varchar (100), mafor char (10) eate table course code char (10) primary key title varchar C reate table Registered code char(a0). senester char (10), preign Key ssn) reférences student (ssa) foreign key (code) references prinary key (ssn,code.year,senester) tnsert into Student values (555550001, "Ton Harks" 100...

  • You will develop an E-Commerce database used to maintain customers, products and sales information. You are...

    You will develop an E-Commerce database used to maintain customers, products and sales information. You are required to 1) gather and analyze requirements 2) design logical structure of the database 3) create stored procedures to develop the tables and insert the data 4) write SQL statements for data extraction and reporting. Throughout the course of this semester you have analyzed the requirements for an eCommerce database, designed and developed your database. As a class we have gone through the process...

  • This is about database system. Thank you. Question B1 Create a Crow's Foot ERD with the...

    This is about database system. Thank you. Question B1 Create a Crow's Foot ERD with the business rules described below. Write all appropriate connectivities and cardinalities in the ERD. A music store would like to develop a database to manage information about the CDs, or vinyl collection, the customers and transactions information. It stores the relationships between artists, albums, tracks, customers and transactions. Here is the list of requirements for the database: The collection consists of albums. An album is...

  • The lab for this week addresses taking a logical database design (data model) and transforming it...

    The lab for this week addresses taking a logical database design (data model) and transforming it into a physical model (tables, constraints, and relationships). As part of the lab, you will need to download the zip file titled CIS336Lab3Files from Doc Sharing. This zip file contains the ERD, Data Dictionary, and test data for the tables you create as you complete this exercise. Your job will be to use the ERD Diagram found below as a guide to define the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT