JDBC Questions in Java 1 Why do you have to load a JDBC driv

JDBC Questions in Java

1. Why do you have to load a JDBC driver when accessing MySQL but not MS Access? What does the driver do anyway?

2. You put a .jar file on your class path. What does this .jar file contain?

3. Difference between regular JDBC statement and a prepared statement. What can you do with a prepared statement you can\'t do with a regular statement?

4. What might make a DB program not portable between DBs?

5. What does the attribute AUTO_INCREMENT do for a DB field?

6. Know the standard code for updating a DB and reading a DB with JDBC. Example: SQL commands are sent to a relational database using ________ objects. You get instances of these objects from what other type of object?

7. What is the connection string used for? Is the connection string specific to a certain type of database? If so, how can your code be portable from one type of DB to another?

8. Can you read and write data through a result set? (Yes, although we didn\'t discuss modifying data through a result set.)

9. What does the following statement do? Class.forName (\"java.lang.String\")
Will it cause an error?

10. T/F Transactions are only useful when you have multiple simultaneous clients accessing a database. Explain.

Solution

1. JDBC driver is a component that allows the java code to interact with different databases. As far as I know, you still have to load the jdbc driver even if you are using MS Access.

2. .jar means java archive. This format was developed to package and distribute java class files along with manifest files, metadata and various other resources such as text , images etc.

3. Difference between Statement and PreparedStatement

It provides some additional methods such as

addBatch() - for adding a parameter to a PreparedStatement object.

getMetadata() - returns the metadata of the ResultSet columns

getParameterMetadata() - returns the metadata of the parameters of the PreparedStatement\'s object.

4. Hardcoding database related information such as JDBC url, database username, database password will make the DB code non-portable. These details should be provided in a properties file which will allow you to edit even after deploying the code to a different environment.

5. AUTO_INCREMENT is used for incrementing a value by 1 as and when a new row is added. By default it starts with 1. This feature is helpful when the table contains an ID field which has to be unique and continuously numbered.

6. SQL commands are sent to a relational database using statement objects. statement objects are created using connection object.

7. The connection string takes the JDBC url for the database. Hence it becomes specific to the database. Instead of hardcoding the URL, we can make the code portable by providng the JDBC url, Username and password in the properties file which can be changed even after the code is deployed. That way the code becomes generic.

8. Yes, we can read and write using ResultSet. There is a collection of getxxx() and updatexxx() methods available for various datatypes.

9. No, the statement will not throw any error. Look at the following program.

public class SampleClass {

public static void main(String[] args) {

try {
Class class1 = Class.forName(\"SampleClass\"); // finds the class

ClassLoader myClassLoader = class1.getClassLoader(); // loads the class

// another initialization where finding and loading happens at the same time
Class class2 = Class.forName(\"java.lang.String\", true, myClassLoader);   
  
// returns the name of the classes
System.out.println(\"Class = \" + class1.getName());
System.out.println(\"Class = \" + class2.getName());
}
catch(ClassNotFoundException exception) {
System.out.println(exception.toString());
}
}
}

OUTPUT:

Class = SampleClass
Class = java.lang.String

10. Please let me know what T/F transactions are. Im not understanding it.

Statement PreparedStatement
Statements are basically used for executing DDL commands such as CREATE, ALTER, DROP etc.. PreparedStatement is used for executing DML commands such as SELECT, UPDATE, DELETE etc..
They do not allow for CLOB and BLOB datatypes which are used for images They allow BLOB and CLOB datatypes
They work on static SQL statment. They can work on pre-compiled SQL statements.
This is slower in execution when compared to PreparedStatement because it has to compile the SQL statement first and then execute it. It works on pre-compiled SQL statements, hence a bit quicker and also it allows for binary protocol communication to the server. This means that there will be less packets of data to transfer thereby making it faster.
It does not directly allow to set an array into Database It aloows for setting an array into Database
These methods are not available.

It provides some additional methods such as

addBatch() - for adding a parameter to a PreparedStatement object.

getMetadata() - returns the metadata of the ResultSet columns

getParameterMetadata() - returns the metadata of the parameters of the PreparedStatement\'s object.

JDBC Questions in Java 1. Why do you have to load a JDBC driver when accessing MySQL but not MS Access? What does the driver do anyway? 2. You put a .jar file o
JDBC Questions in Java 1. Why do you have to load a JDBC driver when accessing MySQL but not MS Access? What does the driver do anyway? 2. You put a .jar file o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site