Advanced Programming Quiz I Answers 13. 04.2004 1.) What is the JDBC API? How many packages include JDBC 3.0 API? JDBC API is a set of classes and interfaces written in Java programming language for tool and database developers. It is possible to write database applications to obtain a variety of different data sources using a Java API .For example; JDBC API makes possible to publish a web page containing an applet that uses information obtained from a remote data source. JDBC 3.0 API includes two packages: java.sql package and javax.sql package (adds serverside capabilities 2.) What Is the JDBC-ODBC Bridge? A JDBC driver implements JDBC operations which translate them into ODBC operations. ODBC acts as a normal application program. The bridge implements JDBC for any database for which an ODBC driver is available, and contains a native library used to access ODBC. 3.) How can we establish a connection by using JDBC API? We can establish a connection with a data source. Then we send queries and update statements to the data source, and finally we process the results. For example: Connection con = null; Statement st = null; ResultSet rs = null; ……………………………. con = DriverManager.getConnection ("jdbc:mysql:///examples", "root", "secret"); st = con.createStatement(); rs = st. executeQuery ("SELECT user_id, first_name, last_name, country_code FROM users"); 4.) Why ODBC is not feasible for direct uses from the Java? ODBC is not appropriate for direct use from the Java programming language. It uses C interface, and a number of drawbacks can be occurred in the security, implementation, robustness, automatic portability during the calls from Java to native C code. 5.) What is the main function of the java.sql package? java.sql package contains the entire JDBC API that sends SQL statements to relational databases and retrieves the results of executing those SQL statements 6.) Which classes must be imported from java.sql package for a mysql connection? Explain them briefly. We have to import three different classes from java.sql package. import java.sql. Connection; //Connection represents a connection to a database import java.sql. DriverManager; // DriverManager manages JDBC drivers and used to create connections to databases import java.sql. SQLException; // SQLException is an exception class // SQLException gets thrown in case any error occurs in the program 7.) What is the difference between ResultSet and RowSet? ResultSet (in java.sql package) is a set of results returned by the database in response to a SQL query. RowSet (in javax.sql package) extends the ResultSet interface to a JavaBeans component that can be manipulated at design time and used with non-SQL data sources 8.) How can we constitute a Cloudscape connection? Each framework (embedded or RmiJdbc) directory has a bin subdirectory containing batch files. SetEmbeddedCP.bat or setNetworkServerCP.bat file is executed in the related directory. Database connection URL is implemented as follows: In JDBC, a URL that we specify as an argument to the DriverManager.getConnection method call and that returns a connection to a database. Cloudscape’s database connection URL allows us to specify attributes to the connection. Connection con = DriverManager.getConnection (“jdbc: cloudscape: rmi: books); Statement sta= con.createStatement(); 9.) What are the Cloudscape frameworks? i) Embedded: It is the simplest Cloudscape environment and enables as part of a Java application. In this environment, only a single application can connect to a database at one time, and no network access occurs ii) Client/Server: When multiple applications connect to Cloudscape over the network, they are said to run in a client/server environment. Cloudscape runs embedded in a server framework that allows multiple network connections. 10.) Using a Cloudscape engine create a table, insert data, select data and drop that table, disconnect the database. Statement s = conn.createStatement(); s.execute ("create table db2jDB (num int, addr varchar (40))" ); s.execute ("insert into db2jDB values (19,'San St.')"); ………………. ResultSet rs = s.executeQuery ("SELECT num, addr FROM db2jDB ORDER BY num"); …………………………. s.execute ("drop table db2jDB"); …………………………… conn.close (); 11.) Using mysql commands create and select a database, create any table, then select particular rows, particular columns. Finally; count rows, count any column using GROUP BY clause. mysql> CREATE DATABASE example; mysql>USE example; mysql> CREATE TABLE answer (name VARCHAR (20), surname VARCHAR (20)); mysql>SELECT * FROM answer WHERE name=” ”; mysql>SELECT name FROM answer; mysql> SELECT COUNT (*) FROM answer; mysql> SELECT name, COUNT (*) FROM answer GROUP BY name;