⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.java

📁 关系型数据库 Postgresql 6.5.2
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package postgresql;import java.io.*;import java.net.*;import java.sql.*;import java.util.*;import postgresql.Field;import postgresql.fastpath.*;import postgresql.largeobject.*;import postgresql.util.*;/** * $Id: Connection.java,v 1.17 1999/05/18 23:17:15 peter Exp $ * * This abstract class is used by postgresql.Driver to open either the JDBC1 or * JDBC2 versions of the Connection class. * */public abstract class Connection{  // This is the network stream associated with this connection  public PG_Stream pg_stream;    // This is set by postgresql.Statement.setMaxRows()  public int maxrows = 0;		// maximum no. of rows; 0 = unlimited    private String PG_HOST;  private int PG_PORT;  private String PG_USER;  private String PG_PASSWORD;  private String PG_DATABASE;  private boolean PG_STATUS;    public boolean CONNECTION_OK = true;  public boolean CONNECTION_BAD = false;    public boolean autoCommit = true;  public boolean readOnly = false;    public Driver this_driver;  private String this_url;  private String cursor = null;	// The positioned update cursor name    // These are new for v6.3, they determine the current protocol versions  // supported by this version of the driver. They are defined in  // src/include/libpq/pqcomm.h  protected static final int PG_PROTOCOL_LATEST_MAJOR = 1;  protected static final int PG_PROTOCOL_LATEST_MINOR = 0;  private static final int SM_DATABASE	= 64;  private static final int SM_USER	= 32;  private static final int SM_OPTIONS	= 64;  private static final int SM_UNUSED	= 64;  private static final int SM_TTY	= 64;    private static final int AUTH_REQ_OK       = 0;  private static final int AUTH_REQ_KRB4     = 1;  private static final int AUTH_REQ_KRB5     = 2;  private static final int AUTH_REQ_PASSWORD = 3;  private static final int AUTH_REQ_CRYPT    = 4;    // New for 6.3, salt value for crypt authorisation  private String salt;    // This is used by Field to cache oid -> names.  // It's here, because it's shared across this connection only.  // Hence it cannot be static within the Field class, because it would then  // be across all connections, which could be to different backends.  public Hashtable fieldCache = new Hashtable();    // Now handle notices as warnings, so things like "show" now work  public SQLWarning firstWarning = null;      /**     * This is called by Class.forName() from within postgresql.Driver     */    public Connection()    {    }        /**     * This method actually opens the connection. It is called by Driver.     *     * @param host the hostname of the database back end     * @param port the port number of the postmaster process     * @param info a Properties[] thing of the user and password     * @param database the database to connect to     * @param u the URL of the connection     * @param d the Driver instantation of the connection     * @return a valid connection profile     * @exception SQLException if a database access error occurs     */    protected void openConnection(String host, int port, Properties info, String database, String url, Driver d) throws SQLException    {    // Throw an exception if the user or password properties are missing    // This occasionally occurs when the client uses the properties version    // of getConnection(), and is a common question on the email lists    if(info.getProperty("user")==null)      throw new PSQLException("postgresql.con.user");    if(info.getProperty("password")==null)      throw new PSQLException("postgresql.con.pass");        this_driver = d;    this_url = new String(url);    PG_DATABASE = new String(database);    PG_PASSWORD = new String(info.getProperty("password"));    PG_USER = new String(info.getProperty("user"));    PG_PORT = port;    PG_HOST = new String(host);    PG_STATUS = CONNECTION_BAD;        // Now make the initial connection    try      {	pg_stream = new PG_Stream(host, port);      } catch (ConnectException cex) {	// Added by Peter Mount <peter@retep.org.uk>	// ConnectException is thrown when the connection cannot be made.	// we trap this an return a more meaningful message for the end user	throw new PSQLException ("postgresql.con.refused");      } catch (IOException e) {	throw new PSQLException ("postgresql.con.failed",e);      }            // Now we need to construct and send a startup packet      try	{	  // Ver 6.3 code	  pg_stream.SendInteger(4+4+SM_DATABASE+SM_USER+SM_OPTIONS+SM_UNUSED+SM_TTY,4);	  pg_stream.SendInteger(PG_PROTOCOL_LATEST_MAJOR,2);	  pg_stream.SendInteger(PG_PROTOCOL_LATEST_MINOR,2);	  pg_stream.Send(database.getBytes(),SM_DATABASE);	  	  // This last send includes the unused fields	  pg_stream.Send(PG_USER.getBytes(),SM_USER+SM_OPTIONS+SM_UNUSED+SM_TTY);	  	  // now flush the startup packets to the backend	  pg_stream.flush();	  	  // Now get the response from the backend, either an error message	  // or an authentication request	  int areq = -1; // must have a value here	  do {	    int beresp = pg_stream.ReceiveChar();	    switch(beresp)	      {	      case 'E':		// An error occured, so pass the error message to the		// user.		//		// The most common one to be thrown here is:		// "User authentication failed"		//		throw new SQLException(pg_stream.ReceiveString(4096));			      case 'R':		// Get the type of request		areq = pg_stream.ReceiveIntegerR(4);				// Get the password salt if there is one		if(areq == AUTH_REQ_CRYPT) {		  byte[] rst = new byte[2];		  rst[0] = (byte)pg_stream.ReceiveChar();		  rst[1] = (byte)pg_stream.ReceiveChar();		  salt = new String(rst,0,2);		  DriverManager.println("Salt="+salt);		}				// now send the auth packet		switch(areq)		  {		  case AUTH_REQ_OK:		    break;		    		  case AUTH_REQ_KRB4:		    DriverManager.println("postgresql: KRB4");		    throw new PSQLException("postgresql.con.kerb4");		    		  case AUTH_REQ_KRB5:		    DriverManager.println("postgresql: KRB5");		    throw new PSQLException("postgresql.con.kerb5");		    		  case AUTH_REQ_PASSWORD:		    DriverManager.println("postgresql: PASSWORD");		    pg_stream.SendInteger(5+PG_PASSWORD.length(),4);		    pg_stream.Send(PG_PASSWORD.getBytes());		    pg_stream.SendInteger(0,1);		    pg_stream.flush();		    break;		    		  case AUTH_REQ_CRYPT:		    DriverManager.println("postgresql: CRYPT");		    String crypted = UnixCrypt.crypt(salt,PG_PASSWORD);		    pg_stream.SendInteger(5+crypted.length(),4);		    pg_stream.Send(crypted.getBytes());		    pg_stream.SendInteger(0,1);		    pg_stream.flush();		    break;		    		  default:		    throw new PSQLException("postgresql.con.auth",new Integer(areq));		  }		break;			      default:		throw new PSQLException("postgresql.con.authfail");	      }	    } while(areq != AUTH_REQ_OK);	  	} catch (IOException e) {	  throw new PSQLException("postgresql.con.failed",e);	}	      // Originally we issued a SHOW DATESTYLE statement to find the databases default      // datestyle. However, this caused some problems with timestamps, so in 6.5, we      // went the way of ODBC, and set the connection to ISO.      //      // This may cause some clients to break when they assume anything other than ISO,      // but then - they should be using the proper methods ;-)      //      //      firstWarning = null;            ExecSQL("set datestyle to 'ISO'");            // Initialise object handling      initObjectTypes();            // Mark the connection as ok, and cleanup      firstWarning = null;      PG_STATUS = CONNECTION_OK;    }        // These methods used to be in the main Connection implementation. As they    // are common to all implementations (JDBC1 or 2), they are placed here.    // This should make it easy to maintain the two specifications.        /**     * This adds a warning to the warning chain.     * @param msg message to add     */    public void addWarning(String msg)    {	DriverManager.println(msg);		// Add the warning to the chain	if(firstWarning!=null)	    firstWarning.setNextWarning(new SQLWarning(msg));	else	    firstWarning = new SQLWarning(msg);		// Now check for some specific messages		// This is obsolete in 6.5, but I've left it in here so if we need to use this	// technique again, we'll know where to place it.	//	// This is generated by the SQL "show datestyle"	//if(msg.startsWith("NOTICE:") && msg.indexOf("DateStyle")>0) {	//// 13 is the length off "DateStyle is "	//msg = msg.substring(msg.indexOf("DateStyle is ")+13);	//  	//for(int i=0;i<dateStyles.length;i+=2)	//if(msg.startsWith(dateStyles[i]))	//currentDateStyle=i+1; // this is the index of the format	//}    }        /**     * Send a query to the backend.  Returns one of the ResultSet     * objects.     *     * <B>Note:</B> there does not seem to be any method currently     * in existance to return the update count.     *     * @param sql the SQL statement to be executed     * @return a ResultSet holding the results     * @exception SQLException if a database error occurs     */    public java.sql.ResultSet ExecSQL(String sql) throws SQLException    {	// added Oct 7 1998 to give us thread safety.	synchronized(pg_stream) {	    	    Field[] fields = null;	    Vector tuples = new Vector();	    byte[] buf = new byte[sql.length()];	    int fqp = 0;	    boolean hfr = false;	    String recv_status = null, msg;		int update_count = 1;	    SQLException final_error = null;	    	    if (sql.length() > 8192)		throw new PSQLException("postgresql.con.toolong",sql);	    try		{		    pg_stream.SendChar('Q');		    buf = sql.getBytes();		    pg_stream.Send(buf);		    pg_stream.SendChar(0);		    pg_stream.flush();		} catch (IOException e) {		    throw new PSQLException("postgresql.con.ioerror",e);		}	    	    while (!hfr || fqp > 0)		{		    Object tup=null;	// holds rows as they are recieved		    		    int c = pg_stream.ReceiveChar();		    		    switch (c)			{			case 'A':	// Asynchronous Notify			    int pid = pg_stream.ReceiveInteger(4);			    msg = pg_stream.ReceiveString(8192);			    break;			case 'B':	// Binary Data Transfer			    if (fields == null)				throw new PSQLException("postgresql.con.tuple");			    tup = pg_stream.ReceiveTuple(fields.length, true);			    // This implements Statement.setMaxRows()			    if(maxrows==0 || tuples.size()<maxrows)				tuples.addElement(tup);			    break;			case 'C':	// Command Status			    recv_status = pg_stream.ReceiveString(8192);								// Now handle the update count correctly.				if(recv_status.startsWith("INSERT") || recv_status.startsWith("UPDATE")) {					try {						update_count = Integer.parseInt(recv_status.substring(1+recv_status.lastIndexOf(' ')));					} catch(NumberFormatException nfe) {						throw new PSQLException("postgresql.con.fathom",recv_status);					}				}			    if (fields != null)				hfr = true;			    else				{				    try					{					    pg_stream.SendChar('Q');					    pg_stream.SendChar(' ');					    pg_stream.SendChar(0);					    pg_stream.flush();					} catch (IOException e) {					    throw new PSQLException("postgresql.con.ioerror",e);					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -