Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
472
Chapter 18 JDBC and Database Connection Pooling
Listing 18.5 FruitCreation.java
package coreservlets;
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;
}
String vendorName = args[4];
int vendor = DriverUtilities.getVendor(vendorName);
if (vendor == DriverUtilities.UNKNOWN) {
printUsage();
return;
}
String driver = DriverUtilities.getDriver(vendor);
String host = args[0];
String dbName = args[1];
String url =
DriverUtilities.makeURL(host, dbName, vendor);
String username = args[2];
String password = args[3];
String format =
"(quarter int, " +
"apples int, applesales float, " +
"oranges int, orangesales float, " +
"topseller varchar(16))";
String[] rows =
{ "(1, 32248, 3547.28, 18459, 3138.03, 'Maria')",
"(2, 35009, 3850.99, 18722, 3182.74, 'Bob')",
"(3, 39393, 4333.23, 18999, 3229.83, 'Joe')",
"(4, 42001, 4620.11, 19333, 3286.61, 'Maria')" };
Connection connection =
DatabaseUtilities.createTable(driver, url,
username, password,
"fruits", format, rows,
false);
// Test to verify table was created properly. Reuse
// old connection for efficiency.
DatabaseUtilities.printTable(connection, "fruits",
11, true);
}
private static void printUsage() {
System.out.println("Usage: FruitCreation host dbName " +
"username password oracle|sybase.");
}
}
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved