Tag: Java/J2EE/J2MEJDBC

Jan 02

FruitTest.java: A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the "fruits" table.

# FruitTest.java A class that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the "fruits" table. package cwp; import java.sql.*; /** A JDBC example that connects to either an Oracle or * a Sybase database and prints out the values of * predetermined columns in …

Continue reading

Jan 02

Some simple utilities for building Oracle and Sybase JDBC connections

DriverUtilities.java Some simple utilities for building Oracle and Sybase JDBC connections. This is not general-purpose code — it is specific to our local setup. package cwp; /** Some simple utilities for building Oracle and Sybase * JDBC connections. This is not general-purpose * code — it is specific to my local setup. */ public class …

Continue reading

Jan 02

FruitCreation.java: Creates a simple table named fruits in either an Oracle or a Sybase database.

FruitCreation.java Creates a simple table named fruits in either an Oracle or a Sybase database. ************************************** package cwp; import java.sql.*; /** Creates a simple table named "fruits" in either * an Oracle or a Sybase database. * */ public class FruitCreation { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; …

Continue reading

Jan 02

DatabaseUtilities.java: Several general-purpose utilities discussed and used in the chapter.

package cwp; import java.sql.*; /** Three database utilities: * 1) getQueryResults. Connects to a database, executes * a query, retrieves all the rows as arrays * of strings, and puts them inside a DBResults * object. Also places the database product name, * database version, and the names of all the columns * into the …

Continue reading

Jan 02

DBResults.java: Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways

# DBResults.java Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways: * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows. * This class stores results as strings, in arrays. * This class includes DatabaseMetaData (database product name and version) …

Continue reading

Jan 02

EmployeeTest.java: A test case for the database utilities. Prints results in plain text.

package cwp; import java.sql.*; /** Connect to Oracle or Sybase and print "employees" table. * */ public class EmployeeTest { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; } String driver = DriverUtilities.getDriver(vendor); String …

Continue reading

Jan 02

EmployeeTest2.java: A test case for the database utilities. Prints results formatted as an HTML table.

package cwp; import java.sql.*; /** Connect to Oracle or Sybase and print "employees" table * as an HTML table. * */ public class EmployeeTest2 { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; } …

Continue reading

Jan 02

EmployeeCreation.java: Make a simple "employees" table using the database utilities

package cwp; import java.sql.*; /** Make a simple "employees" table using DatabaseUtilities. */ public class EmployeeCreation { public static Connection createEmployees(String driver, String url, String username, String password, boolean close) { String format = "(id int, firstname varchar(32), lastname varchar(32), " + "language varchar(16), salary float)"; String[] employees = {"(1, ‘Wye’, ‘Tukay’, ‘COBOL’, 42500)", "(2, …

Continue reading

Jan 02

QueryViewer.java: An interactive database query viewer

# QueryViewer.java An interactive database query viewer. Connects to the specified Oracle or Sybase database, executes a query, and presents the results in a JTable. Uses the following file: * DBResultsTableModel.java Simple class that tells a JTable how to extract relevant data from a DBResults object (which is used to store the results from a …

Continue reading

Jan 02

extract relevant data from a DBResults

package cwp; import javax.swing.table.*; /** Simple class that tells a JTable how to extract * relevant data from a DBResults object (which is * used to store the results from a database query). */ public class DBResultsTableModel extends AbstractTableModel { private DBResults results; public DBResultsTableModel(DBResults results) { this.results = results; } public int getRowCount() { …

Continue reading

Jan 02

PreparedStatements.java An example to test the timing differences resulting from repeated raw queries vs. repeated calls

package cwp; import java.sql.*; /** An example to test the timing differences resulting * from repeated raw queries vs. repeated calls to * prepared statements. These results will vary dramatically * among database servers and drivers. With my setup * and drivers, Oracle prepared statements took only half * the time that raw queries required …

Continue reading

Skip to toolbar