Commit ff246d7b authored by Marc G. Fournier's avatar Marc G. Fournier

Bring in Adrian's JDBC driver as an interface

parent fd86ae15
import java.io.*;
import java.lang.*;
import java.sql.*;
class JDBC_Test
{
public JDBC_Test()
{
}
public static void main(String argv[])
{
String url = new String(argv[0]);
Connection db;
Statement s;
ResultSet rs;
// Load the driver
try
{
Class.forName("postgresql.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Exception: " + e.toString());
}
// Lets do a few things -- it doesn't do everything, but
// it tests out basic functionality
try
{
System.out.println("Connecting to Database URL = " + url);
db = DriverManager.getConnection(url, "adrian", "");
System.out.println("Connected...Now creating a statement");
s = db.createStatement();
System.out.println("Ok...now we will create a table");
s.executeUpdate("create table test (a int2, b int2)");
System.out.println("Now we will insert some columns");
s.executeUpdate("insert into test values (1, 1)");
s.executeUpdate("insert into test values (2, 1)");
s.executeUpdate("insert into test values (3, 1)");
System.out.println("Inserted some data");
System.out.println("Now lets try a select");
rs = s.executeQuery("select a, b from test");
System.out.println("Back from the select...the following are results");
int i = 0;
while (rs.next())
{
int a = rs.getInt("a");
int b = rs.getInt("b");
System.out.println("row " + i + " " + a + " " + b);
i++;
}
System.out.println("Ok...dropping the table");
s.executeUpdate("drop table test");
System.out.println("Now closing the connection");
s.close();
db.close();
} catch (SQLException e) {
System.out.println("Exception: " + e.toString());
}
}
}
package postgresql;
import java.math.*;
import java.sql.*;
/**
* @version 1.0 15-APR-1997
* @author <A HREF="mailto:adrian@hottub.org">Adrian Hall</A>
*
* CallableStatement is used to execute SQL stored procedures.
*
* JDBC provides a stored procedure SQL escape that allows stored procedures
* to be called in a standard way for all RDBMS's. This escape syntax has
* one form that includes a result parameter and one that does not. If used,
* the result parameter must be generated as an OUT parameter. The other
* parameters may be used for input, output or both. Parameters are refered
* to sequentially, by number. The first parameter is 1.
*
* <PRE>
* {?= call <procedure-name>[<arg1>,<arg2>, ...]}
* {call <procedure-name>[<arg1>,<arg2>, ...]}
* </PRE>
*
* IN parameters are set using the set methods inherited from
* PreparedStatement. The type of all OUT parameters must be registered
* prior to executing the stored procedure; their values are retrieved
* after execution via the get methods provided here.
*
* A CallableStatement may return a ResultSet or multiple ResultSets. Multiple
* ResultSets are handled using operations inherited from Statement.
*
* For maximum portability, a call's ResultSets and update counts should be
* processed prior to getting the values of output parameters.
*
* @see java.sql.Connection#prepareCall
* @see java.sql.ResultSet
* @see java.sql.CallableStatement
*/
public class CallableStatement implements java.sql.CallableStatement
{
public void registerOutParameter (int paramterIndex, int sqlType) throws SQLException
{
// XXX-Not Implemented
}
public void registerOutParameter (int parameterIndex, int sqlType, int scale) throws SQLException
{
// XXX-Not Implemented
}
public boolean wasNull () throws SQLException
{
// XXX-Not Implemented
}
public String getString (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public boolean getBoolean (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public byte getByte (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public short getShort (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public int getInt (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public long getLong (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public float getFloat (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public double getDouble (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public BigDecimal getBigDecimal (int parameterIndex, int scale) throws SQLException
{
// XXX-Not Implemented
}
public byte[] getBytes (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public Date getDate (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public Time getTime (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public Timestamp getTimestamp (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
public Object getObject (int parameterIndex) throws SQLException
{
// XXX-Not Implemented
}
}
This diff is collapsed.
This diff is collapsed.
package postgresql;
import java.sql.*;
import java.util.*;
import postgresql.*;
/**
* @version 1.0 15-APR-1997
* @author <A HREF="mailto:adrian@hottub.org">Adrian Hall</A>
*
* The Java SQL framework allows for multiple database drivers. Each
* driver should supply a class that implements the Driver interface
*
* The DriverManager will try to load as many drivers as it can find and then
* for any given connection request, it will ask each driver in turn to try
* to connect to the target URL.
*
* It is strongly recommended that each Driver class should be small and
* standalone so that the Driver class can be loaded and queried without
* bringing in vast quantities of supporting code.
*
* When a Driver class is loaded, it should create an instance of itself and
* register it with the DriverManager. This means that a user can load and
* register a driver by doing Class.forName("foo.bah.Driver")
*
* @see postgresql.Connection
* @see java.sql.Driver
*/
public class Driver implements java.sql.Driver
{
static
{
try
{
new Driver();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Construct a new driver and register it with DriverManager
*
* @exception SQLException for who knows what!
*/
public Driver() throws SQLException
{
java.sql.DriverManager.registerDriver(this);
}
/**
* Try to make a database connection to the given URL. The driver
* should return "null" if it realizes it is the wrong kind of
* driver to connect to the given URL. This will be common, as
* when the JDBC driverManager is asked to connect to a given URL,
* it passes the URL to each loaded driver in turn.
*
* The driver should raise an SQLException if it is the right driver
* to connect to the given URL, but has trouble connecting to the
* database.
*
* The java.util.Properties argument can be used to pass arbitrary
* string tag/value pairs as connection arguments. Normally, at least
* "user" and "password" properties should be included in the
* properties.
*
* Our protocol takes the form:
* <PRE>
* jdbc:postgresql://host:port/database
* </PRE>
*
* @param url the URL of the database to connect to
* @param info a list of arbitrary tag/value pairs as connection
* arguments
* @return a connection to the URL or null if it isnt us
* @exception SQLException if a database access error occurs
* @see java.sql.Driver#connect
*/
public java.sql.Connection connect(String url, Properties info) throws SQLException
{
DriverURL dr = new DriverURL(url);
int port;
if (!(dr.protocol().equals("jdbc")))
return null;
if (!(dr.subprotocol().equals("postgresql")))
return null;
if (dr.host().equals("unknown"))
return null;
port = dr.port();
if (port == -1)
port = 5432; // Default PostgreSQL port
return new Connection (dr.host(), port, info, dr.database(), url, this);
}
/**
* Returns true if the driver thinks it can open a connection to the
* given URL. Typically, drivers will return true if they understand
* the subprotocol specified in the URL and false if they don't. Our
* protocols start with jdbc:postgresql:
*
* @see java.sql.Driver#acceptsURL
* @param url the URL of the driver
* @return true if this driver accepts the given URL
* @exception SQLException if a database-access error occurs
* (Dont know why it would *shrug*)
*/
public boolean acceptsURL(String url) throws SQLException
{
DriverURL dr = new DriverURL(url);
if (dr.protocol().equals("jdbc"))
if (dr.subprotocol().equals("postgresql"))
return true;
return false;
}
/**
* The getPropertyInfo method is intended to allow a generic GUI
* tool to discover what properties it should prompt a human for
* in order to get enough information to connect to a database.
* Note that depending on the values the human has supplied so
* far, additional values may become necessary, so it may be necessary
* to iterate through several calls to getPropertyInfo
*
* @param url the Url of the database to connect to
* @param info a proposed list of tag/value pairs that will be sent on
* connect open.
* @return An array of DriverPropertyInfo objects describing
* possible properties. This array may be an empty array if
* no properties are required
* @exception SQLException if a database-access error occurs
* @see java.sql.Driver#getPropertyInfo
*/
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException
{
return null; // We don't need anything except
// the username, which is a default
}
/**
* Gets the drivers major version number
*
* @return the drivers major version number
*/
public int getMajorVersion()
{
return 1;
}
/**
* Get the drivers minor version number
*
* @return the drivers minor version number
*/
public int getMinorVersion()
{
return 0;
}
/**
* Report whether the driver is a genuine JDBC compliant driver. A
* driver may only report "true" here if it passes the JDBC compliance
* tests, otherwise it is required to return false. JDBC compliance
* requires full support for the JDBC API and full support for SQL 92
* Entry Level.
*/
public boolean jdbcCompliant()
{
return false;
}
}
/**
* The DriverURL class splits a JDBC URL into its subcomponents
*
* protocol:subprotocol:/[/host[:port]/][database]
*/
class DriverURL
{
private String protocol, subprotocol, host, database;
private int port = -1;
/**
* Constructs a new DriverURL, splitting the specified URL into its
* component parts
*/
public DriverURL(String url) throws SQLException
{
int a, b, c;
String tmp, hostport, dbportion;
a = url.indexOf(':');
if (a == -1)
throw new SQLException("Bad URL Protocol specifier");
b = url.indexOf(':', a+1);
if (b == -1)
throw new SQLException("Bad URL Subprotocol specifier");
protocol = new String(url.substring(0, a));
subprotocol = new String(url.substring(a+1, b));
tmp = new String(url.substring(b+1, url.length()));
if (tmp.length() < 2)
throw new SQLException("Bad URL Database specifier");
if (!tmp.substring(0, 2).equals("//"))
{
host = new String("unknown");
port = -1;
database = new String(tmp.substring(1, tmp.length()));
return;
}
dbportion = new String(tmp.substring(2, tmp.length()));
c = dbportion.indexOf('/');
if (c == -1)
throw new SQLException("Bad URL Database specifier");
a = dbportion.indexOf(':');
if (a == -1)
{
host = new String(dbportion.substring(0, c));
port = -1;
database = new String(dbportion.substring(c+1, dbportion.length()));
} else {
host = new String(dbportion.substring(0, a));
port = Integer.valueOf(dbportion.substring(a+1, c)).intValue();
database = new String(dbportion.substring(c+1, dbportion.length()));
}
}
/**
* Returns the protocol name of the DriverURL
*/
public String protocol()
{
return protocol;
}
/**
* Returns the subprotocol name of the DriverURL
*/
public String subprotocol()
{
return subprotocol;
}
/**
* Returns the hostname portion of the URL
*/
public String host()
{
return host;
}
/**
* Returns the port number portion of the URL
* or -1 if no port was specified
*/
public int port()
{
return port;
}
/**
* Returns the database name of the URL
*/
public String database()
{
return database;
}
}
package postgresql;
import java.lang.*;
import java.sql.*;
import java.util.*;
import postgresql.*;
/**
* postgresql.Field is a class used to describe fields in a PostgreSQL ResultSet
*
* @version 1.0 15-APR-1997
* @author <A HREF="mailto:adrian@hottub.org">Adrian Hall</A>
*/
public class Field
{
int length; // Internal Length of this field
int oid; // OID of the type
Connection conn; // Connection Instantation
String name; // Name of this field
int sql_type = -1; // The entry in java.sql.Types for this field
String type_name = null;// The sql type name
/**
* Construct a field based on the information fed to it.
*
* @param conn the connection this field came from
* @param name the name of the field
* @param oid the OID of the field
* @param len the length of the field
*/
public Field(Connection conn, String name, int oid, int length)
{
this.conn = conn;
this.name = name;
this.oid = oid;
this.length = length;
}
/**
* the ResultSet and ResultMetaData both need to handle the SQL
* type, which is gained from another query. Note that we cannot
* use getObject() in this, since getObject uses getSQLType().
*
* @return the entry in Types that refers to this field
* @exception SQLException if a database access error occurs
*/
public int getSQLType() throws SQLException
{
if (sql_type == -1)
{
ResultSet result = (postgresql.ResultSet)conn.ExecSQL("select typname from pg_type where oid = " + oid);
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
throw new SQLException("Unexpected return from query for type");
result.next();
type_name = result.getString(1);
if (type_name.equals("int2")) sql_type = Types.SMALLINT;
else if (type_name.equals("int4")) sql_type = Types.INTEGER;
else if (type_name.equals("int8")) sql_type = Types.BIGINT;
else if (type_name.equals("cash")) sql_type = Types.DECIMAL;
else if (type_name.equals("money")) sql_type = Types.DECIMAL;
else if (type_name.equals("float4")) sql_type = Types.REAL;
else if (type_name.equals("float8")) sql_type = Types.DOUBLE;
else if (type_name.equals("bpchar")) sql_type = Types.CHAR;
else if (type_name.equals("varchar")) sql_type = Types.VARCHAR;
else if (type_name.equals("bool")) sql_type = Types.BIT;
else if (type_name.equals("date")) sql_type = Types.DATE;
else if (type_name.equals("time")) sql_type = Types.TIME;
else if (type_name.equals("abstime")) sql_type = Types.TIMESTAMP;
else sql_type = Types.OTHER;
}
return sql_type;
}
/**
* We also need to get the type name as returned by the back end.
* This is held in type_name AFTER a call to getSQLType. Since
* we get this information within getSQLType (if it isn't already
* done), we can just call getSQLType and throw away the result.
*
* @return the String representation of the type of this field
* @exception SQLException if a database access error occurs
*/
public String getTypeName() throws SQLException
{
int sql = getSQLType();
return type_name;
}
}
package postgresql;
import java.lang.*;
import java.sql.*;
import java.util.*;
import postgresql.*;
/**
* postgresql.PG_Object is a class used to describe unknown types
* An unknown type is any type that is unknown by JDBC Standards
*
* @version 1.0 15-APR-1997
* @author <A HREF="mailto:adrian@hottub.org">Adrian Hall</A>
*/
public class PG_Object
{
public String type;
public String value;
/**
* Constructor for the PostgreSQL generic object
*
* @param type a string describing the type of the object
* @param value a string representation of the value of the object
*/
public PG_Object(String type, String value)
{
this.type = type;
this.value = value;
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment