From: | Lew <noone(at)lwsc(dot)ehost-services(dot)com> |
---|---|
To: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Re: org.postgresql.util.PSQLException: Protocol error. Session setup failed |
Date: | 2010-03-11 01:45:08 |
Message-ID: | hn9hv5$l9l$1@news.albasani.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
Major Services wrote:
> Output is :
Output of what?
> //============================================================ // Main
> public access point method for instantiating the // PostgreSQL_JDBC
> application. Arguments: database, username, // & password. //
> ============================================================== public
> static void main(String[] args) throws SQLException,
> InstantiationException, IllegalAccessException, ClassNotFoundException,
> InterruptedException { String host, database, username, password;
> Connection dbConnection; // Collect connection properties. and setup
> connection. //host = "cindy"; host = "localhost"; if (args.length != 0)
> { database = args[0]; username = (args.length > 1) ? args[1] : null;
> password = (args.length > 2) ? args[2] : null; } else { database =
> "postgres"; username = "postgres"; password = "major"; } dbConnection =
> null; Class.forName("org.postgresql.Driver").newInstance(); dbConnection
> = DriverManager.getConnection("jdbc:postgresql://" + host + "/" +
> database, username, password); System.out.println("Connection Created");
> new PostgreSQL_JDBC(dbConnection); // Close. dbConnection.close();
> System.out.println("Connection Closed"); }
Wow, that is some unreadable source code. I see nothing here that sheds light
on your question, though.
What caught my eye in what
dmp wrote:
is
> public static void main(String[] args) throws SQLException,
> InstantiationException,
> IllegalAccessException, ClassNotFoundException,
> InterruptedException
If you don't handle the exceptions, perhaps even with logging, it's harder to
diagnose what went wrong. I am also curious why the declaration of
'InterruptedException' is in the throws list.
> {
> String host, database, username, password;
> Connection dbConnection;
Declared but not instantiated - correct enough except that
...
> dbConnection = null;
assigned here, then
> Class.forName("org.postgresql.Driver").newInstance();
> dbConnection = DriverManager.getConnection("jdbc:postgresql://"
> + host + "/" + database, username,
> password);
the value is replaced here. It's a best practice in Java to declare variables
within the scope of use, and close to the point of use. It doesn't make much
sense to assign a value to the variable that is never used, just thrown away.
Also that argument applies to the invocation of 'newInstance()'. The driver
registers itself with the driver manager on class initialization; the creation
of an instance does nothing but waste time. User code never directly uses
driver instances.
--
Lew
From | Date | Subject | |
---|---|---|---|
Next Message | Lew | 2010-03-11 01:48:11 | Re: org.postgresql.util.PSQLException: Protocol error. Session setup failed |
Previous Message | Kris Jurka | 2010-03-10 21:52:19 | Re: JDBC driver, client_encoding and a SQL_ASCII database in production |