add official jdbc tutorials
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/App.java b/jdbc-tutorial/src/main/java/com/supwisdom/App.java
index d4069da..6cc4735 100644
--- a/jdbc-tutorial/src/main/java/com/supwisdom/App.java
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/App.java
@@ -1,16 +1,11 @@
package com.supwisdom;
-import java.sql.Connection;
-import java.sql.DriverManager;
public class App {
- public static void main(String[] args) throws Exception {
- Class.forName("org.h2.Driver");
- Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", "");
- // add application code here
-
- conn.close();
- }
+ public static final String JDBC_DRIVER = "org.h2.Driver";
+ public static final String JDBC_URL = "jdbc:h2:tcp://localhost/test";
+ public static final String DB_USERNAME = "sa";
+ public static final String DB_PASSWORD = "";
}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/BatchUpdate.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/BatchUpdate.java
new file mode 100644
index 0000000..8c9e106
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/BatchUpdate.java
@@ -0,0 +1,81 @@
+package com.supwisdom.jdbc;
+
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class BatchUpdate {
+
+ public static void main(String args[]) throws SQLException {
+
+ ResultSet rs = null;
+ PreparedStatement ps = null;
+
+ Connection con;
+ Statement stmt;
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+ con.setAutoCommit(false);
+
+ stmt = con.createStatement();
+
+ stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto', 49, 9.99, 0, 0)");
+ stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut', 49, 9.99, 0, 0)");
+ stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto_decaf', 49, 10.99, 0, 0)");
+ stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)");
+
+ int[] updateCounts = stmt.executeBatch();
+ con.commit();
+ con.setAutoCommit(true);
+
+ ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
+
+ System.out.println("Table COFFEES after insertion:");
+ while (uprs.next()) {
+ String name = uprs.getString("COF_NAME");
+ int id = uprs.getInt("SUP_ID");
+ float price = uprs.getFloat("PRICE");
+ int sales = uprs.getInt("SALES");
+ int total = uprs.getInt("TOTAL");
+ System.out.print(name + " " + id + " " + price);
+ System.out.println(" " + sales + " " + total);
+ }
+
+ uprs.close();
+ stmt.close();
+ con.close();
+
+ } catch (BatchUpdateException b) {
+ System.err.println("-----BatchUpdateException-----");
+ System.err.println("SQLState: " + b.getSQLState());
+ System.err.println("Message: " + b.getMessage());
+ System.err.println("Vendor: " + b.getErrorCode());
+ System.err.print("Update counts: ");
+ int[] updateCounts = b.getUpdateCounts();
+ for (int i = 0; i < updateCounts.length; i++) {
+ System.err.print(updateCounts[i] + " ");
+ }
+ System.err.println("");
+
+ } catch (SQLException ex) {
+ System.err.println("-----SQLException-----");
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Message: " + ex.getMessage());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateCoffees.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateCoffees.java
new file mode 100644
index 0000000..2aa46c0
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateCoffees.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class CreateCoffees {
+
+ public static void main(String args[]) {
+
+
+ Connection con;
+ String createString;
+ createString = "create table COFFEES " + "(COF_NAME varchar(32), " + "SUP_ID int, " + "PRICE float, "
+ + "SALES int, " + "TOTAL int)";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ stmt.executeUpdate(createString);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewTable.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewTable.java
new file mode 100644
index 0000000..114d1c8
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewTable.java
@@ -0,0 +1,242 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import java.util.*;
+import static com.supwisdom.App.*;
+
+public class CreateNewTable {
+
+ public static void main(String[] args) {
+
+ Connection con;
+ Statement stmt;
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ Vector dataTypes = getDataTypes(con);
+
+ String tableName;
+ String columnName;
+ String sqlType;
+ String prompt = "Enter the new table name and hit Return: ";
+ tableName = getInput(prompt);
+ String createTableString = "create table " + tableName + " (";
+
+ String commaAndSpace = ", ";
+ boolean firstTime = true;
+ while (true) {
+ System.out.println("");
+ prompt = "Enter a column name " + "(or nothing when finished) \nand hit Return: ";
+ columnName = getInput(prompt);
+ if (firstTime) {
+ if (columnName.length() == 0) {
+ System.out.print("Need at least one column;");
+ System.out.println(" please try again");
+ continue;
+ } else {
+ createTableString += columnName + " ";
+ firstTime = false;
+ }
+ } else if (columnName.length() == 0) {
+ break;
+ } else {
+ createTableString += commaAndSpace + columnName + " ";
+ }
+
+ String localTypeName = null;
+ String paramString = "";
+ while (true) {
+ System.out.println("");
+ System.out.println("LIST OF TYPES YOU MAY USE: ");
+ boolean firstPrinted = true;
+ int length = 0;
+ for (int i = 0; i < dataTypes.size(); i++) {
+ DataType dataType = (DataType) dataTypes.get(i);
+ if (!dataType.needsToBeSet()) {
+ if (!firstPrinted) System.out.print(commaAndSpace);
+ else firstPrinted = false;
+ System.out.print(dataType.getSQLType());
+ length += dataType.getSQLType().length();
+ if (length > 50) {
+ System.out.println("");
+ length = 0;
+ firstPrinted = true;
+ }
+ }
+ }
+ System.out.println("");
+
+ int index;
+ prompt = "Enter a column type " + "from the list and hit Return: ";
+ sqlType = getInput(prompt);
+ for (index = 0; index < dataTypes.size(); index++) {
+ DataType dataType = (DataType) dataTypes.get(index);
+ if (dataType.getSQLType().equalsIgnoreCase(sqlType) && !dataType.needsToBeSet()) {
+ break;
+ }
+ }
+
+ localTypeName = null;
+ paramString = "";
+ if (index < dataTypes.size()) { // there was a match
+ String params;
+ DataType dataType = (DataType) dataTypes.get(index);
+ params = dataType.getParams();
+ localTypeName = dataType.getLocalType();
+ if (params != null) {
+ prompt = "Enter " + params + ": ";
+ paramString = "(" + getInput(prompt) + ")";
+ }
+ break;
+ } else { // use the name as given
+ prompt = "Are you sure? " + "Enter 'y' or 'n' and hit Return: ";
+ String check = getInput(prompt) + " ";
+ check = check.toLowerCase().substring(0, 1);
+ if (check.equals("n")) continue;
+ else {
+ localTypeName = sqlType;
+ break;
+ }
+ }
+ }
+
+ createTableString += localTypeName + paramString;
+
+ }
+
+ createTableString += ")";
+ System.out.println("");
+ System.out.print("Your CREATE TABLE statement as ");
+ System.out.println("sent to your DBMS: ");
+ System.out.println(createTableString);
+ System.out.println("");
+
+ stmt.executeUpdate(createTableString);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+
+ private static Vector getDataTypes(Connection con) throws SQLException {
+ String structName = null, distinctName = null, javaName = null;
+
+ // create a vector of class DataType initialized with
+ // the SQL code, the SQL type name, and two null entries
+ // for the local type name and the creation parameter(s)
+
+ Vector dataTypes = new Vector();
+ dataTypes.add(new DataType(java.sql.Types.BIT, "BIT"));
+ dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT"));
+ dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT"));
+ dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER"));
+ dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT"));
+ dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT"));
+ dataTypes.add(new DataType(java.sql.Types.REAL, "REAL"));
+ dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE"));
+ dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC"));
+ dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL"));
+ dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR"));
+ dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR"));
+ dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR"));
+ dataTypes.add(new DataType(java.sql.Types.DATE, "DATE"));
+ dataTypes.add(new DataType(java.sql.Types.TIME, "TIME"));
+ dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP"));
+ dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY"));
+ dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY"));
+ dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY"));
+ dataTypes.add(new DataType(java.sql.Types.NULL, "NULL"));
+ dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER"));
+ dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB"));
+ dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB"));
+
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getTypeInfo();
+ while (rs.next()) {
+ int codeNumber = rs.getInt("DATA_TYPE");
+ String dbmsName = rs.getString("TYPE_NAME");
+ String createParams = rs.getString("CREATE_PARAMS");
+
+ if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;
+ else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName;
+ else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName;
+ else {
+ for (int i = 0; i < dataTypes.size(); i++) {
+ // find entry that matches the SQL code,
+ // and if local type and params are not already set,
+ // set them
+ DataType type = (DataType) dataTypes.get(i);
+ if (type.getCode() == codeNumber) {
+ type.setLocalTypeAndParams(dbmsName, createParams);
+ }
+ }
+ }
+ }
+
+ int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT };
+ rs = dbmd.getUDTs(null, "%", "%", types);
+ while (rs.next()) {
+ String typeName = null;
+ DataType dataType = null;
+
+ if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "."
+ + rs.getString(3);
+ else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1);
+
+ switch (rs.getInt(5)) {
+ case Types.STRUCT:
+ dataType = new DataType(Types.STRUCT, typeName);
+ dataType.setLocalTypeAndParams(structName, null);
+ break;
+ case Types.DISTINCT:
+ dataType = new DataType(Types.DISTINCT, typeName);
+ dataType.setLocalTypeAndParams(distinctName, null);
+ break;
+ case Types.JAVA_OBJECT:
+ dataType = new DataType(Types.JAVA_OBJECT, typeName);
+ dataType.setLocalTypeAndParams(javaName, null);
+ break;
+ }
+ dataTypes.add(dataType);
+ }
+
+ return dataTypes;
+ }
+
+ private static String getInput(String prompt) throws SQLException {
+
+ System.out.print(prompt);
+ System.out.flush();
+
+ try {
+ java.io.BufferedReader bin;
+ bin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
+
+ String result = bin.readLine();
+ return result;
+
+ } catch (java.io.IOException ex) {
+ System.out.println("Caught java.io.IOException:");
+ System.out.println(ex.getMessage());
+ return "";
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewType.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewType.java
new file mode 100644
index 0000000..23b3746
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateNewType.java
@@ -0,0 +1,255 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import java.util.*;
+import static com.supwisdom.App.*;
+
+public class CreateNewType {
+
+ public static void main(String[] args) {
+
+ Connection con;
+ Statement stmt;
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ String typeToCreate = null;
+ String prompt = "Enter 's' to create a structured type " + "or 'd' to create a distinct type\n"
+ + "and hit Return: ";
+ do {
+ typeToCreate = getInput(prompt) + " ";
+ typeToCreate = typeToCreate.toLowerCase().substring(0, 1);
+ } while (!(typeToCreate.equals("s") || typeToCreate.equals("d")));
+
+ Vector dataTypes = getDataTypes(con, typeToCreate);
+
+ String typeName;
+ String attributeName;
+ String sqlType;
+ prompt = "Enter the new type name and hit Return: ";
+ typeName = getInput(prompt);
+ String createTypeString = "create type " + typeName;
+ if (typeToCreate.equals("d")) createTypeString += " as ";
+ else createTypeString += " (";
+
+ String commaAndSpace = ", ";
+ boolean firstTime = true;
+ while (true) {
+ System.out.println("");
+ prompt = "Enter an attribute name " + "(or nothing when finished) \nand hit Return: ";
+ attributeName = getInput(prompt);
+ if (firstTime) {
+ if (attributeName.length() == 0) {
+ System.out.print("Need at least one attribute;");
+ System.out.println(" please try again");
+ continue;
+ } else {
+ createTypeString += attributeName + " ";
+ firstTime = false;
+ }
+ } else if (attributeName.length() == 0) {
+ break;
+ } else {
+ createTypeString += commaAndSpace + attributeName + " ";
+ }
+
+ String localTypeName = null;
+ String paramString = "";
+ while (true) {
+ System.out.println("");
+ System.out.println("LIST OF TYPES YOU MAY USE: ");
+ boolean firstPrinted = true;
+ int length = 0;
+ for (int i = 0; i < dataTypes.size(); i++) {
+ DataType dataType = (DataType) dataTypes.get(i);
+ if (!dataType.needsToBeSet()) {
+ if (!firstPrinted) System.out.print(commaAndSpace);
+ else firstPrinted = false;
+ System.out.print(dataType.getSQLType());
+ length += dataType.getSQLType().length();
+ if (length > 50) {
+ System.out.println("");
+ length = 0;
+ firstPrinted = true;
+ }
+ }
+ }
+ System.out.println("");
+
+ int index;
+ prompt = "Enter an attribute type " + "from the list and hit Return: ";
+ sqlType = getInput(prompt);
+ for (index = 0; index < dataTypes.size(); index++) {
+ DataType dataType = (DataType) dataTypes.get(index);
+ if (dataType.getSQLType().equalsIgnoreCase(sqlType) && !dataType.needsToBeSet()) {
+ break;
+ }
+ }
+
+ localTypeName = null;
+ paramString = "";
+ if (index < dataTypes.size()) { // there was a match
+ String params;
+ DataType dataType = (DataType) dataTypes.get(index);
+ params = dataType.getParams();
+ localTypeName = dataType.getLocalType();
+ if (params != null) {
+ prompt = "Enter " + params + ": ";
+ paramString = "(" + getInput(prompt) + ")";
+ }
+ break;
+ } else { // use the name as given
+ prompt = "Are you sure? " + "Enter 'y' or 'n' and hit Return: ";
+ String check = getInput(prompt) + " ";
+ check = check.toLowerCase().substring(0, 1);
+ if (check.equals("n")) continue;
+ else {
+ localTypeName = sqlType;
+ break;
+ }
+ }
+ }
+
+ createTypeString += localTypeName + paramString;
+
+ if (typeToCreate.equals("d")) break;
+ }
+
+ if (typeToCreate.equals("s")) createTypeString += ")";
+ System.out.println("");
+ System.out.print("Your CREATE TYPE statement as ");
+ System.out.println("sent to your DBMS: ");
+ System.out.println(createTypeString);
+ System.out.println("");
+
+ stmt.executeUpdate(createTypeString);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+
+ private static Vector getDataTypes(Connection con, String typeToCreate) throws SQLException {
+ String structName = null, distinctName = null, javaName = null;
+
+ // create a vector of class DataType initialized with
+ // the SQL code, the SQL type name, and two null entries
+ // for the local type name and the creation parameter(s)
+
+ Vector dataTypes = new Vector();
+ dataTypes.add(new DataType(java.sql.Types.BIT, "BIT"));
+ dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT"));
+ dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT"));
+ dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER"));
+ dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT"));
+ dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT"));
+ dataTypes.add(new DataType(java.sql.Types.REAL, "REAL"));
+ dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE"));
+ dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC"));
+ dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL"));
+ dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR"));
+ dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR"));
+ dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR"));
+ dataTypes.add(new DataType(java.sql.Types.DATE, "DATE"));
+ dataTypes.add(new DataType(java.sql.Types.TIME, "TIME"));
+ dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP"));
+ dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY"));
+ dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY"));
+ dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY"));
+ dataTypes.add(new DataType(java.sql.Types.NULL, "NULL"));
+ dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER"));
+ dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB"));
+ dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB"));
+
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getTypeInfo();
+ while (rs.next()) {
+ int codeNumber = rs.getInt("DATA_TYPE");
+ String dbmsName = rs.getString("TYPE_NAME");
+ String createParams = rs.getString("CREATE_PARAMS");
+
+ if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;
+ else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName;
+ else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName;
+ else {
+ for (int i = 0; i < dataTypes.size(); i++) {
+ // find entry that matches the SQL code,
+ // and if local type and params are not already set,
+ // set them
+ DataType type = (DataType) dataTypes.get(i);
+ if (type.getCode() == codeNumber) {
+ type.setLocalTypeAndParams(dbmsName, createParams);
+ }
+ }
+ }
+ }
+
+ if (typeToCreate.equals("s")) {
+ int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT };
+ rs = dbmd.getUDTs(null, "%", "%", types);
+ while (rs.next()) {
+ String typeName = null;
+ DataType dataType = null;
+
+ if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "."
+ + rs.getString(3);
+ else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1);
+
+ switch (rs.getInt(5)) {
+ case Types.STRUCT:
+ dataType = new DataType(Types.STRUCT, typeName);
+ dataType.setLocalTypeAndParams(structName, null);
+ break;
+ case Types.DISTINCT:
+ dataType = new DataType(Types.DISTINCT, typeName);
+ dataType.setLocalTypeAndParams(distinctName, null);
+ break;
+ case Types.JAVA_OBJECT:
+ dataType = new DataType(Types.JAVA_OBJECT, typeName);
+ dataType.setLocalTypeAndParams(javaName, null);
+ break;
+ }
+ dataTypes.add(dataType);
+ }
+ }
+
+ return dataTypes;
+ }
+
+ private static String getInput(String prompt) throws SQLException {
+
+ System.out.print(prompt);
+ System.out.flush();
+
+ try {
+ java.io.BufferedReader bin;
+ bin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
+
+ String result = bin.readLine();
+ return result;
+
+ } catch (java.io.IOException ex) {
+ System.out.println("Caught java.io.IOException:");
+ System.out.println(ex.getMessage());
+ return "";
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateRef.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateRef.java
new file mode 100644
index 0000000..45036cc
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateRef.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class CreateRef {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ String createManagers = "CREATE TABLE MANAGERS OF MANAGER " + "(OID REF(MANAGER) VALUES ARE SYSTEM GENERATED)";
+
+ String insertManager1 = "INSERT INTO MANAGERS " + "(MGR_ID, LAST_NAME, FIRST_NAME, PHONE) VALUES "
+ + "(000001, 'MONTOYA', 'ALFREDO', '8317225600')";
+
+ String insertManager2 = "INSERT INTO MANAGERS " + "(MGR_ID, LAST_NAME, FIRST_NAME, PHONE) VALUES "
+ + "(000002, 'HASKINS', 'MARGARET', '4084355600')";
+
+ String insertManager3 = "INSERT INTO MANAGERS " + "(MGR_ID, LAST_NAME, FIRST_NAME, PHONE) VALUES "
+ + "(000003, 'CHEN', 'HELEN', '4153785600')";
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ stmt.executeUpdate(createManagers);
+
+ con.setAutoCommit(false);
+
+ stmt.addBatch(insertManager1);
+ stmt.addBatch(insertManager2);
+ stmt.addBatch(insertManager3);
+ int[] updateCounts = stmt.executeBatch();
+
+ con.commit();
+
+ System.out.println("Update count for: ");
+ for (int i = 0; i < updateCounts.length; i++) {
+ System.out.print(" command " + (i + 1) + " = ");
+ System.out.println(updateCounts[i]);
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (BatchUpdateException b) {
+ System.err.println("-----BatchUpdateException-----");
+ System.err.println("Message: " + b.getMessage());
+ System.err.println("SQLState: " + b.getSQLState());
+ System.err.println("Vendor: " + b.getErrorCode());
+ System.err.print("Update counts for successful commands: ");
+ int[] rowsUpdated = b.getUpdateCounts();
+ for (int i = 0; i < rowsUpdated.length; i++) {
+ System.err.print(rowsUpdated[i] + " ");
+ }
+ System.err.println("");
+ } catch (SQLException ex) {
+ System.err.println("------SQLException------");
+ System.err.println("Error message: " + ex.getMessage());
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateStores.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateStores.java
new file mode 100644
index 0000000..bd504a3
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateStores.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class CreateStores {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ String createTable;
+ String createArray;
+ createArray = "CREATE TYPE COF_ARRAY AS ARRAY(10) OF VARCHAR(40)";
+ createTable = "CREATE TABLE STORES ( " + "STORE_NO INTEGER, LOCATION ADDRESS, "
+ + "COF_TYPES COF_ARRAY, MGR REF MANAGER )";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ stmt.executeUpdate(createArray);
+ stmt.executeUpdate(createTable);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateSuppliers.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateSuppliers.java
new file mode 100644
index 0000000..b5c15e3
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateSuppliers.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.net.URL;
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class CreateSuppliers {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ String createString;
+ createString = "create table SUPPLIERS " + "(SUP_ID int, " + "SUP_NAME varchar(40), " + "STREET varchar(40), "
+ + "CITY varchar(20), " + "STATE char(2), ZIP char(5))";
+
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ stmt.executeUpdate(createString);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateUDTs.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateUDTs.java
new file mode 100644
index 0000000..cdfdce7
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/CreateUDTs.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class CreateUDTs {
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+
+ String createAddress = "CREATE TYPE ADDRESS (NUM INTEGER, " + "STREET VARCHAR(40), CITY VARCHAR(40), "
+ + "STATE CHAR(2), ZIP CHAR(5))";
+
+ String createManager = "CREATE TYPE MANAGER (MGR_ID INTEGER, " + "LAST_NAME VARCHAR(40), FIRST_NAME VARCHAR(40), "
+ + "PHONE char(10))";
+
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ stmt.executeUpdate(createAddress);
+ stmt.executeUpdate("CREATE TYPE PHONE_NO AS CHAR(10)");
+ stmt.executeUpdate(createManager);
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("-----SQLException-----");
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Message: " + ex.getMessage());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/DataType.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/DataType.java
new file mode 100644
index 0000000..f795d56
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/DataType.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+public class DataType {
+
+ private int code;
+ private String SQLType;
+ private String localType = null;
+ private String params = null;
+ private boolean needsSetting = true;
+
+ public DataType(int code, String SQLType) {
+ this.code = code;
+ this.SQLType = SQLType;
+
+ }
+
+ public boolean needsToBeSet() {
+ return needsSetting;
+ }
+
+ public int getCode() {
+ return code;
+ }
+
+ public String getSQLType() {
+ return SQLType;
+ }
+
+ public String getLocalType() {
+ return localType;
+ }
+
+ public String getParams() {
+ return params;
+ }
+
+ public void setLocalTypeAndParams(String local, String p) {
+ if (needsSetting) {
+ localType = local;
+ params = p;
+ needsSetting = false;
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ForeignKeysCoffees.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ForeignKeysCoffees.java
new file mode 100644
index 0000000..886bd00
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ForeignKeysCoffees.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class ForeignKeysCoffees {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ String createString = "create table COFFEESFK " + "(COF_NAME varchar(32) NOT NULL, " + "SUP_ID int, "
+ + "PRICE float, " + "SALES int, " + "TOTAL int, " + "primary key(COF_NAME), "
+ + "foreign key(SUP_ID) references SUPPLIERSPK)";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ stmt.executeUpdate(createString);
+
+ DatabaseMetaData dbmd = con.getMetaData();
+
+ ResultSet rs = dbmd.getImportedKeys(null, null, "COFFEESFK");
+ while (rs.next()) {
+ String pkTable = rs.getString("PKTABLE_NAME");
+ String pkColName = rs.getString("PKCOLUMN_NAME");
+ String fkTable = rs.getString("FKTABLE_NAME");
+ String fkColName = rs.getString("FKCOLUMN_NAME");
+ short updateRule = rs.getShort("UPDATE_RULE");
+ short deleteRule = rs.getShort("DELETE_RULE");
+ System.out.println("primary key table name : " + pkTable);
+ System.out.print("primary key column name : ");
+ System.out.println(pkColName);
+ System.out.println("foreign key table name : " + fkTable);
+ System.out.print("foreign key column name : ");
+ System.out.println(fkColName);
+ System.out.println("update rule: " + updateRule);
+ System.out.println("delete rule: " + deleteRule);
+ System.out.println("");
+ }
+
+ rs.close();
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/GetParamMetaData.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/GetParamMetaData.java
new file mode 100644
index 0000000..9db7100
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/GetParamMetaData.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class GetParamMetaData {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ PreparedStatement pstmt;
+ ParameterMetaData pmd;
+
+ String sql = "UPDATE COFFEES SET SALES = ? " + "WHERE COF_NAME = ?";
+
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ pstmt = con.prepareStatement(sql);
+
+ pmd = pstmt.getParameterMetaData();
+
+ int totalDigits = pmd.getPrecision(1);
+ int digitsAfterDecimal = pmd.getScale(1);
+ boolean b = pmd.isSigned(1);
+ System.out.println("The first parameter ");
+ System.out.println(" has precision " + totalDigits);
+ System.out.println(" has scale " + digitsAfterDecimal);
+ System.out.println(" may be a signed number " + b);
+
+ int count = pmd.getParameterCount();
+ System.out.println("count is " + count);
+
+ for (int i = 1; i <= count; i++) {
+ int type = pmd.getParameterType(i);
+ String typeName = pmd.getParameterTypeName(i);
+ System.out.println("Parameter " + i + ":");
+ System.out.println(" type is " + type);
+ System.out.println(" type name is " + typeName);
+ }
+
+ pstmt.close();
+ con.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertCoffees.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertCoffees.java
new file mode 100644
index 0000000..b3735e4
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertCoffees.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class InsertCoffees {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ String query = "select COF_NAME, PRICE from COFFEES";
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ stmt.executeUpdate("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0, 0)");
+
+ stmt.executeUpdate("insert into COFFEES " + "values('French_Roast', 00049, 8.99, 0, 0)");
+
+ stmt.executeUpdate("insert into COFFEES " + "values('Espresso', 00150, 9.99, 0, 0)");
+
+ stmt.executeUpdate("insert into COFFEES " + "values('Colombian_Decaf', 00101, 8.99, 0, 0)");
+
+ stmt.executeUpdate("insert into COFFEES " + "values('French_Roast_Decaf', 00049, 9.99, 0, 0)");
+
+ ResultSet rs = stmt.executeQuery(query);
+
+ System.out.println("Coffee Break Coffees and Prices:");
+ while (rs.next()) {
+ String s = rs.getString("COF_NAME");
+ float f = rs.getFloat("PRICE");
+ System.out.println(s + " " + f);
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRow.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRow.java
new file mode 100644
index 0000000..579075e
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRow.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class InsertRow {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ String query = "select COF_NAME, PRICE from COFFEES";
+
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
+ ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
+
+ uprs.moveToInsertRow();
+
+ uprs.updateString("COF_NAME", "Kona");
+ uprs.updateInt("SUP_ID", 150);
+ uprs.updateFloat("PRICE", 10.99f);
+ uprs.updateInt("SALES", 0);
+ uprs.updateInt("TOTAL", 0);
+
+ uprs.insertRow();
+ uprs.beforeFirst();
+
+ System.out.println("Table COFFEES after insertion:");
+ while (uprs.next()) {
+ String s = uprs.getString("COF_NAME");
+ int sup = uprs.getInt("SUP_ID");
+ float f = uprs.getFloat("PRICE");
+ int sales = uprs.getInt("SALES");
+ int t = uprs.getInt("TOTAL");
+ System.out.print(s + " " + sup + " " + f + " ");
+ System.out.println(sales + " " + t);
+ }
+
+ uprs.close();
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRows.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRows.java
new file mode 100644
index 0000000..1d14c3a
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertRows.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class InsertRows {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
+
+ ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
+
+ uprs.moveToInsertRow();
+
+ uprs.updateString("COF_NAME", "Kona");
+ uprs.updateInt("SUP_ID", 150);
+ uprs.updateFloat("PRICE", 10.99f);
+ uprs.updateInt("SALES", 0);
+ uprs.updateInt("TOTAL", 0);
+
+ uprs.insertRow();
+
+ uprs.updateString("COF_NAME", "Kona_Decaf");
+ uprs.updateInt("SUP_ID", 150);
+ uprs.updateFloat("PRICE", 11.99f);
+ uprs.updateInt("SALES", 0);
+ uprs.updateInt("TOTAL", 0);
+
+ uprs.insertRow();
+
+ uprs.beforeFirst();
+
+ System.out.println("Table COFFEES after insertion:");
+ while (uprs.next()) {
+ String name = uprs.getString("COF_NAME");
+ int id = uprs.getInt("SUP_ID");
+ float price = uprs.getFloat("PRICE");
+ int sales = uprs.getInt("SALES");
+ int total = uprs.getInt("TOTAL");
+ System.out.print(name + " " + id + " " + price);
+ System.out.println(" " + sales + " " + total);
+ }
+
+ uprs.close();
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertStores.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertStores.java
new file mode 100644
index 0000000..cd9d99f
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertStores.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class InsertStores {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ con.setAutoCommit(false);
+
+ String insertStore1 = "INSERT INTO STORES VALUES (" + "100001, "
+ + "ADDRESS(888, 'Main_Street', 'Rancho_Alegre', " + "'CA', '94049'), "
+ + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf'), "
+ + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))";
+
+ stmt.addBatch(insertStore1);
+
+ String insertStore2 = "INSERT INTO STORES VALUES (" + "100002, " + "ADDRESS(1560, 'Alder', 'Ochos_Pinos', "
+ + "'CA', '94049'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
+ + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
+ + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))";
+
+ stmt.addBatch(insertStore2);
+
+ String insertStore3 = "INSERT INTO STORES VALUES (" + "100003, " + "ADDRESS(4344, 'First_Street', 'Verona', "
+ + "'CA', '94545'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
+ + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
+ + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))";
+
+ stmt.addBatch(insertStore3);
+
+ String insertStore4 = "INSERT INTO STORES VALUES (" + "100004, " + "ADDRESS(321, 'Sandy_Way', 'La_Playa', "
+ + "'CA', '94544'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
+ + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), "
+ + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))";
+
+ stmt.addBatch(insertStore4);
+
+ String insertStore5 = "INSERT INTO STORES VALUES (" + "100005, " + "ADDRESS(1000, 'Clover_Road', 'Happyville', "
+ + "'CA', '90566'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', "
+ + "'Colombian_Decaf', 'French_Roast_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000003))";
+
+ stmt.addBatch(insertStore5);
+
+ int[] updateCounts = stmt.executeBatch();
+
+ ResultSet rs = stmt.executeQuery("SELECT * FROM STORES");
+
+ System.out.println("Table STORES after insertion:");
+ System.out.println("STORE_NO LOCATION COF_TYPE MGR");
+ while (rs.next()) {
+ int storeNo = rs.getInt("STORE_NO");
+ Struct location = (Struct) rs.getObject("LOCATION");
+ Object[] locAttrs = location.getAttributes();
+ Array coffeeTypes = rs.getArray("COF_TYPE");
+ String[] cofTypes = (String[]) coffeeTypes.getArray();
+
+ Ref managerRef = rs.getRef("MGR");
+ PreparedStatement pstmt = con.prepareStatement("SELECT MANAGER FROM MANAGERS WHERE OID = ?");
+ pstmt.setRef(1, managerRef);
+ ResultSet rs2 = pstmt.executeQuery();
+ rs2.next();
+ Struct manager = (Struct) rs2.getObject("MANAGER");
+ Object[] manAttrs = manager.getAttributes();
+
+ System.out.print(storeNo + " ");
+ System.out.print(locAttrs[0] + " " + locAttrs[1] + " " + locAttrs[2] + ", " + locAttrs[3] + " " + locAttrs[4]
+ + " ");
+ for (int i = 0; i < cofTypes.length; i++)
+ System.out.print(cofTypes[i] + " ");
+ System.out.println(manAttrs[1] + ", " + manAttrs[2]);
+
+ rs2.close();
+ pstmt.close();
+ }
+
+ rs.close();
+ stmt.close();
+ con.close();
+
+ } catch (BatchUpdateException b) {
+ System.err.println("-----BatchUpdateException-----");
+ System.err.println("SQLState: " + b.getSQLState());
+ System.err.println("Message: " + b.getMessage());
+ System.err.println("Vendor: " + b.getErrorCode());
+ System.err.print("Update counts: ");
+ int[] updateCounts = b.getUpdateCounts();
+ for (int i = 0; i < updateCounts.length; i++) {
+ System.err.print(updateCounts[i] + " ");
+ }
+ System.err.println("");
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Message: " + ex.getMessage());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertSuppliers.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertSuppliers.java
new file mode 100644
index 0000000..1c2eaaa
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/InsertSuppliers.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class InsertSuppliers {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ String query = "select SUP_NAME, SUP_ID from SUPPLIERS";
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ stmt.executeUpdate("insert into SUPPLIERS " + "values(49, 'Superior Coffee', '1 Party Place', "
+ + "'Mendocino', 'CA', '95460')");
+
+ stmt.executeUpdate("insert into SUPPLIERS " + "values(101, 'Acme, Inc.', '99 Market Street', "
+ + "'Groundsville', 'CA', '95199')");
+
+ stmt.executeUpdate("insert into SUPPLIERS " + "values(150, 'The High Ground', '100 Coffee Lane', "
+ + "'Meadows', 'CA', '93966')");
+
+ ResultSet rs = stmt.executeQuery(query);
+
+ System.out.println("Suppliers and their ID Numbers:");
+ while (rs.next()) {
+ String s = rs.getString("SUP_NAME");
+ int n = rs.getInt("SUP_ID");
+ System.out.println(s + " " + n);
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/Join.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/Join.java
new file mode 100644
index 0000000..8fe63e6
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/Join.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class Join {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS "
+ + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ ResultSet rs = stmt.executeQuery(query);
+ System.out.println("Supplier, Coffee:");
+ while (rs.next()) {
+ String supName = rs.getString(1);
+ String cofName = rs.getString(2);
+ System.out.println(" " + supName + ", " + cofName);
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/OutputApplet.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/OutputApplet.java
new file mode 100644
index 0000000..d6a4d75
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/OutputApplet.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+
+/**
+ * This is a demonstration JDBC applet.
+ * It displays some simple standard output from the Coffee database.
+ */
+package com.supwisdom.jdbc;
+
+import java.applet.Applet;
+import java.awt.Graphics;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Enumeration;
+import java.util.Vector;
+import static com.supwisdom.App.*;
+
+public class OutputApplet extends Applet implements Runnable {
+ private Thread worker;
+ private Vector queryResults;
+ private String message = "Initializing";
+
+ public synchronized void start() {
+ // Every time "start" is called we create a worker thread to
+ // re-evaluate the database query.
+ if (worker == null) {
+ message = "Connecting to database";
+ worker = new Thread(this);
+ worker.start();
+ }
+ }
+
+ /**
+ * The "run" method is called from the worker thread. Notice that
+ * because this method is doing potentially slow databases accesses
+ * we avoid making it a synchronized method.
+ */
+
+ public void run() {
+
+ String query = "select COF_NAME, PRICE from COFFEES";
+
+ try {
+ Class.forName(JDBC_DRIVER);
+ } catch (Exception ex) {
+ setError("Can't find Database driver class: " + ex);
+ return;
+ }
+
+ try {
+ Vector results = new Vector();
+ Connection con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+ Statement stmt = con.createStatement();
+ ResultSet rs = stmt.executeQuery(query);
+ while (rs.next()) {
+ String s = rs.getString("COF_NAME");
+ float f = rs.getFloat("PRICE");
+ String text = s + " " + f;
+ results.addElement(text);
+ }
+
+ stmt.close();
+ con.close();
+
+ setResults(results);
+
+ } catch (SQLException ex) {
+ setError("SQLException: " + ex);
+ }
+ }
+
+ /**
+ * The "paint" method is called by AWT when it wants us to
+ * display our current state on the screen.
+ */
+
+ public synchronized void paint(Graphics g) {
+ // If there are no results available, display the current message.
+ if (queryResults == null) {
+ g.drawString(message, 5, 50);
+ return;
+ }
+
+ // Display the results.
+ g.drawString("Prices of coffee per pound: ", 5, 10);
+ int y = 30;
+ Enumeration enum1 = queryResults.elements();
+ while (enum1.hasMoreElements()) {
+ String text = (String) enum1.nextElement();
+ g.drawString(text, 5, y);
+ y = y + 15;
+ }
+ }
+
+ /**
+ * This private method is used to record an error message for
+ * later display.
+ */
+
+ private synchronized void setError(String mess) {
+ queryResults = null;
+ message = mess;
+ worker = null;
+ // And ask AWT to repaint this applet.
+ repaint();
+ }
+
+ /**
+ * This private method is used to record the results of a query, for
+ * later display.
+ */
+
+ private synchronized void setResults(Vector results) {
+ queryResults = results;
+ worker = null;
+ // And ask AWT to repaint this applet.
+ repaint();
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrimaryKeysSuppliers.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrimaryKeysSuppliers.java
new file mode 100644
index 0000000..12b2413
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrimaryKeysSuppliers.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+import java.sql.*;
+import static com.supwisdom.App.*;
+public class PrimaryKeysSuppliers {
+
+ public static void main(String args[]) {
+
+
+ Connection con;
+ String createString = "create table SUPPLIERSPK " +
+ "(SUP_ID INTEGER NOT NULL, " +
+ "SUP_NAME VARCHAR(40), " +
+ "STREET VARCHAR(40), " +
+ "CITY VARCHAR(20), " +
+ "STATE CHAR(2), " +
+ "ZIP CHAR(5), " +
+ "primary key(SUP_ID))";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch(java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL,
+ DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+ stmt.executeUpdate(createString);
+
+ DatabaseMetaData dbmd = con.getMetaData();
+
+ ResultSet rs = dbmd.getPrimaryKeys(null, null, "SUPPLIERSPK");
+ while (rs.next()) {
+ String name = rs.getString("TABLE_NAME");
+ String columnName = rs.getString("COLUMN_NAME");
+ String keySeq = rs.getString("KEY_SEQ");
+ String pkName = rs.getString("PK_NAME");
+ System.out.println("table name : " + name);
+ System.out.println("column name: " + columnName);
+ System.out.println("sequence in key: " + keySeq);
+ System.out.println("primary key name: " + pkName);
+ System.out.println("");
+ }
+
+ rs.close();
+ con.close();
+
+ } catch(SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
+
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumnTypes.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumnTypes.java
new file mode 100644
index 0000000..28a55c7
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumnTypes.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+
+public class PrintColumnTypes {
+
+ public static void printColTypes(ResultSetMetaData rsmd) throws SQLException {
+ int columns = rsmd.getColumnCount();
+ for (int i = 1; i <= columns; i++) {
+ int jdbcType = rsmd.getColumnType(i);
+ String name = rsmd.getColumnTypeName(i);
+ System.out.print("Column " + i + " is JDBC type " + jdbcType);
+ System.out.println(", which the DBMS calls " + name);
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumns.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumns.java
new file mode 100644
index 0000000..bf54d47
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/PrintColumns.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+import java.sql.*;
+import static com.supwisdom.App.*;
+class PrintColumns {
+
+ public static void main(String args[]) {
+
+
+ Connection con;
+ String query = "select * from COFFEES";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch(java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL,
+ DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ ResultSet rs = stmt.executeQuery(query);
+ ResultSetMetaData rsmd = rs.getMetaData();
+
+ PrintColumnTypes.printColTypes(rsmd);
+ System.out.println("");
+
+ int numberOfColumns = rsmd.getColumnCount();
+
+ for (int i = 1; i <= numberOfColumns; i++) {
+ if (i > 1) System.out.print(", ");
+ String columnName = rsmd.getColumnName(i);
+ System.out.print(columnName);
+ }
+ System.out.println("");
+
+ while (rs.next()) {
+ for (int i = 1; i <= numberOfColumns; i++) {
+ if (i > 1) System.out.print(", ");
+ String columnValue = rs.getString(i);
+ System.out.print(columnValue);
+ }
+ System.out.println("");
+ }
+
+ stmt.close();
+ con.close();
+ } catch(SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
+
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/README b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/README
new file mode 100644
index 0000000..b677000
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/README
@@ -0,0 +1,53 @@
+The example code in this directory is taken from the book "JDBC(TM) API Tutorial
+and Reference, Third Edition," by Maydene Fisher, Jon Ellis, and Jonathan Bruce.
+Most of the examples are from the tutorials.
+
+Before you can run the example code, you will need to do the following:
+
+1. Unzip the file containing the sample code.
+
+ unzip codeExamples.zip
+
+2. Substitute your driver's JDBC URL for the generic JDBC URL
+ that appears in the code. In other words, put your driver's JDBC URL
+ between the quotation marks in the follwoing line:
+
+ String url = "jdbc:mySubprotocol:myDataSource";
+
+ The documentation for your driver should give you this URL.
+
+3. Substitute the driver's full class name for "myDriver.ClassName" in
+ the following line:
+
+ Class.forName("myDriver.ClassName");
+
+4. Substitute the username and password you use for your database
+ in the following:
+
+ "myLogin", "myPassword"
+
+
+Two of the .java files contain method(s) that are used in other files and cannot
+be run by themselves:
+
+ - PrintColumnTypes.java defines a method used in PrintColumns.java
+ - DataType.java defines methods used in CreateNewTable.java
+
+
+
+Note that the following code samples have been compiled but not executed because
+at the time of testing, not all drivers implemented all of the features used in the
+code:
+
+ CreateRef.java
+ CreateStores.java
+ InsertStores.java
+ AutoGenKeys.java
+ GetParamMetaData.java
+
+Note also that the syntax for creating SQL99 types can vary from one DBMS to
+another, so you should be sure to check the documentation to see the exact
+syntax your DBMS expects.
+
+
+
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/RSMetaDataMethods.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/RSMetaDataMethods.java
new file mode 100644
index 0000000..b276fa1
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/RSMetaDataMethods.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class RSMetaDataMethods {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ ResultSet rs = stmt.executeQuery("select * from COFFEES");
+ ResultSetMetaData rsmd = rs.getMetaData();
+
+ int numberOfColumns = rsmd.getColumnCount();
+ for (int i = 1; i <= numberOfColumns; i++) {
+ String colName = rsmd.getColumnName(i);
+ String tableName = rsmd.getTableName(i);
+ String name = rsmd.getColumnTypeName(i);
+ boolean caseSen = rsmd.isCaseSensitive(i);
+ boolean writable = rsmd.isWritable(i);
+ System.out.println("Information for column " + colName);
+ System.out.println(" Column is in table " + tableName);
+ System.out.println(" DBMS name for type is " + name);
+ System.out.println(" Is case sensitive: " + caseSen);
+ System.out.println(" Is possibly writable: " + writable);
+ System.out.println("");
+ }
+
+ while (rs.next()) {
+ for (int i = 1; i <= numberOfColumns; i++) {
+ String s = rs.getString(i);
+ System.out.print(s + " ");
+ }
+ System.out.println("");
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SQLStatement.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SQLStatement.java
new file mode 100644
index 0000000..7987cfc
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SQLStatement.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class SQLStatement {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS "
+ + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID";
+ Statement stmt;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement();
+
+ ResultSet rs = stmt.executeQuery(query);
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int numberOfColumns = rsmd.getColumnCount();
+ int rowCount = 1;
+ while (rs.next()) {
+ System.out.println("Row " + rowCount + ": ");
+ for (int i = 1; i <= numberOfColumns; i++) {
+ System.out.print(" Column " + i + ": ");
+ System.out.println(rs.getString(i));
+ }
+ System.out.println("");
+ rowCount++;
+ }
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ScrollableResultSet.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ScrollableResultSet.java
new file mode 100644
index 0000000..8e4f60e
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/ScrollableResultSet.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class ScrollableResultSet {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
+
+ ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");
+
+ srs.absolute(4);
+ int rowNum = srs.getRow(); // rowNum should be 4
+ System.out.println("rowNum should be 4 " + rowNum);
+ srs.relative(-3);
+ rowNum = srs.getRow(); // rowNum should be 1
+ System.out.println("rowNum should be 1 " + rowNum);
+ srs.relative(2);
+ rowNum = srs.getRow(); // rowNum should be 3
+ System.out.println("rowNum should be 3 " + rowNum);
+
+ srs.absolute(1);
+ System.out.println("after last? " + srs.isAfterLast());
+ if (!srs.isAfterLast()) {
+ String name = srs.getString("COF_NAME");
+ float price = srs.getFloat("PRICE");
+ System.out.println(name + " " + price);
+ }
+
+ srs.afterLast();
+ while (srs.previous()) {
+ String name = srs.getString("COF_NAME");
+ float price = srs.getFloat("PRICE");
+ System.out.println(name + " " + price);
+ }
+
+ srs.close();
+ stmt.close();
+ con.close();
+
+ } catch (BatchUpdateException b) {
+ System.err.println("-----BatchUpdateException-----");
+ System.err.println("SQLState: " + b.getSQLState());
+ System.err.println("Message: " + b.getMessage());
+ System.err.println("Vendor: " + b.getErrorCode());
+ System.err.print("Update counts: ");
+ int[] updateCounts = b.getUpdateCounts();
+ for (int i = 0; i < updateCounts.length; i++) {
+ System.err.print(updateCounts[i] + " ");
+ }
+ System.out.println("");
+
+ } catch (SQLException ex) {
+ System.err.println("-----SQLException-----");
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Message: " + ex.getMessage());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SetSavepoint.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SetSavepoint.java
new file mode 100644
index 0000000..861ea44
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/SetSavepoint.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class SetSavepoint {
+
+ public static void main(String args[]) {
+
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ Connection con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+ con.setAutoCommit(false);
+
+ String query = "SELECT COF_NAME, PRICE FROM COFFEES " + "WHERE TOTAL > ?";
+ String update = "UPDATE COFFEES SET PRICE = ? " + "WHERE COF_NAME = ?";
+
+ PreparedStatement getPrice = con.prepareStatement(query);
+ PreparedStatement updatePrice = con.prepareStatement(update);
+
+ getPrice.setInt(1, 7000);
+ ResultSet rs = getPrice.executeQuery();
+
+ Savepoint save1 = con.setSavepoint();
+
+ while (rs.next()) {
+ String cof = rs.getString("COF_NAME");
+ float oldPrice = rs.getFloat("PRICE");
+ float newPrice = oldPrice + (oldPrice * .05f);
+ updatePrice.setFloat(1, newPrice);
+ updatePrice.setString(2, cof);
+ updatePrice.executeUpdate();
+ System.out.println("New price of " + cof + " is " + newPrice);
+ if (newPrice > 11.99) {
+ con.rollback(save1);
+ }
+
+ }
+
+ getPrice = con.prepareStatement(query);
+ updatePrice = con.prepareStatement(update);
+
+ getPrice.setInt(1, 8000);
+
+ rs = getPrice.executeQuery();
+ System.out.println();
+
+ Savepoint save2 = con.setSavepoint();
+
+ while (rs.next()) {
+ String cof = rs.getString("COF_NAME");
+ float oldPrice = rs.getFloat("PRICE");
+ float newPrice = oldPrice + (oldPrice * .05f);
+ updatePrice.setFloat(1, newPrice);
+ updatePrice.setString(2, cof);
+ updatePrice.executeUpdate();
+ System.out.println("New price of " + cof + " is " + newPrice);
+ if (newPrice > 11.99) {
+ con.rollback(save2);
+ }
+ }
+
+ con.commit();
+
+ Statement stmt = con.createStatement();
+ rs = stmt.executeQuery("SELECT COF_NAME, " + "PRICE FROM COFFEES");
+
+ System.out.println();
+ while (rs.next()) {
+ String name = rs.getString("COF_NAME");
+ float price = rs.getFloat("PRICE");
+ System.out.println("Current price of " + name + " is " + price);
+ }
+
+ con.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TableTypes.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TableTypes.java
new file mode 100644
index 0000000..f6afacf
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TableTypes.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class TableTypes {
+
+ public static void main(String args[]) {
+
+ Connection con;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ DatabaseMetaData dbmd = con.getMetaData();
+ String dbmsName = dbmd.getDatabaseProductName();
+ ResultSet rs = dbmd.getTableTypes();
+ System.out.print("The following types of tables are ");
+ System.out.println("available in " + dbmsName + ": ");
+
+ while (rs.next()) {
+ String tableType = rs.getString("TABLE_TYPE");
+ System.out.println(" " + tableType);
+ }
+
+ rs.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.print("SQLException: ");
+ System.err.println(ex.getMessage());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TransactionPairs.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TransactionPairs.java
new file mode 100644
index 0000000..3631989
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TransactionPairs.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class TransactionPairs {
+
+ public static void main(String args[]) {
+
+ Connection con = null;
+ Statement stmt;
+ PreparedStatement updateSales;
+ PreparedStatement updateTotal;
+ String updateString = "update COFFEES " + "set SALES = ? where COF_NAME = ?";
+
+ String updateStatement = "update COFFEES " + "set TOTAL = TOTAL + ? where COF_NAME = ?";
+ String query = "select COF_NAME, SALES, TOTAL from COFFEES";
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ updateSales = con.prepareStatement(updateString);
+ updateTotal = con.prepareStatement(updateStatement);
+ int[] salesForWeek = { 175, 150, 60, 155, 90 };
+ String[] coffees = { "Colombian", "French_Roast", "Espresso", "Colombian_Decaf", "French_Roast_Decaf" };
+ int len = coffees.length;
+ con.setAutoCommit(false);
+ for (int i = 0; i < len; i++) {
+ updateSales.setInt(1, salesForWeek[i]);
+ updateSales.setString(2, coffees[i]);
+ updateSales.executeUpdate();
+
+ updateTotal.setInt(1, salesForWeek[i]);
+ updateTotal.setString(2, coffees[i]);
+ updateTotal.executeUpdate();
+ con.commit();
+ }
+
+ con.setAutoCommit(true);
+
+ updateSales.close();
+ updateTotal.close();
+
+ stmt = con.createStatement();
+ ResultSet rs = stmt.executeQuery(query);
+
+ while (rs.next()) {
+ String c = rs.getString("COF_NAME");
+ int s = rs.getInt("SALES");
+ int t = rs.getInt("TOTAL");
+ System.out.println(c + " " + s + " " + t);
+ }
+
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ if (con != null) {
+ try {
+ System.err.print("Transaction is being ");
+ System.err.println("rolled back");
+ con.rollback();
+ } catch (SQLException excep) {
+ System.err.print("SQLException: ");
+ System.err.println(excep.getMessage());
+ }
+ }
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeConcurrency.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeConcurrency.java
new file mode 100644
index 0000000..2c908d3
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeConcurrency.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class TypeConcurrency {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ Statement stmt;
+ try {
+
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
+
+ ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");
+
+ int type = srs.getType();
+
+ System.out.println("srs is type " + type);
+
+ int concur = srs.getConcurrency();
+
+ System.out.println("srs has concurrency " + concur);
+ while (srs.next()) {
+ String name = srs.getString("COF_NAME");
+ int id = srs.getInt("SUP_ID");
+ float price = srs.getFloat("PRICE");
+ int sales = srs.getInt("SALES");
+ int total = srs.getInt("TOTAL");
+ System.out.print(name + " " + id + " " + price);
+ System.out.println(" " + sales + " " + total);
+ }
+
+ srs.close();
+ stmt.close();
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("-----SQLException-----");
+ System.err.println("SQLState: " + ex.getSQLState());
+ System.err.println("Message: " + ex.getMessage());
+ System.err.println("Vendor: " + ex.getErrorCode());
+ }
+ }
+}
diff --git a/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeInfo.java b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeInfo.java
new file mode 100644
index 0000000..1da79b7
--- /dev/null
+++ b/jdbc-tutorial/src/main/java/com/supwisdom/jdbc/TypeInfo.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Use of this software is authorized pursuant to the terms of the license found at
+ * http://developer.java.sun.com/berkeley_license.html.
+ */
+package com.supwisdom.jdbc;
+
+import java.net.URL;
+import java.sql.*;
+import static com.supwisdom.App.*;
+
+public class TypeInfo {
+
+ public static void main(String args[]) {
+
+ Connection con;
+ DatabaseMetaData dbmd;
+
+ try {
+ Class.forName(JDBC_DRIVER);
+
+ } catch (java.lang.ClassNotFoundException e) {
+ System.err.print("ClassNotFoundException: ");
+ System.err.println(e.getMessage());
+ }
+
+ try {
+ con = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);
+
+ dbmd = con.getMetaData();
+
+ ResultSet rs = dbmd.getTypeInfo();
+ while (rs.next()) {
+ String typeName = rs.getString("TYPE_NAME");
+ short dataType = rs.getShort("DATA_TYPE");
+ String createParams = rs.getString("CREATE_PARAMS");
+ int nullable = rs.getInt("NULLABLE");
+ boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE");
+ System.out.println("DBMS type " + typeName + ":");
+ System.out.println(" java.sql.Types: " + dataType);
+ System.out.print(" parameters used to create: ");
+ System.out.println(createParams);
+ System.out.println(" nullable?: " + nullable);
+ System.out.print(" case sensitive?: ");
+ System.out.println(caseSensitive);
+ System.out.println("");
+
+ }
+
+ con.close();
+
+ } catch (SQLException ex) {
+ System.err.println("SQLException: " + ex.getMessage());
+ }
+ }
+}