Thanks to Ben and Ross but...

Of course the Class.forName connection string has to be invoked first.
My brain hurts.

--Bill

-----Original Message-----
From: u2-users-boun...@listserver.u2ug.org
[mailto:u2-users-boun...@listserver.u2ug.org] On Behalf Of Ben Souther
Sent: Wednesday, March 24, 2010 1:24 PM
To: u2-users@listserver.u2ug.org
Subject: Re: [U2] JDBC - Finally - Hitch

In Java, variables are local to the block in which they were declared.

So, in order to reach your 'con' variable from outside the 'try' block
it needs to be declared outside of that block.


Connection con = null;  // declare con outside of try block.
try{
    con = DriverManager.......
    // do stuff with the connection
}catch(Exception e){
    // handle any exceptions
}finally{
    try{
       con.close(); 
    }catch(Exception ignored){
    }
}










On Wed, 2010-03-24 at 11:49 -0400, Brutzman, Bill wrote:
> Ben:
> 
> 1. I am glad to find out about the "finally" command.  I was not aware

> of this feature in Java.
> 2. The new code below yields an IDE error...  con cannot be resolved 
> 3. Connection con was already defined... it gives an error when I do a

> con = null; 4. IDE does not accept  con.close; 5. While I am happy to 
> look into this and figure it out... right now I do not know the fix.
> 
> Regards,
> 
> --Bill
> 
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
> 
> public class JDBC_1001 {
> 
>     public static void main(String[] args) {
>         try {
> 
>             try {
>               Class.forName("com.ibm.u2.jdbc.UniJDBCDriver"); 
>             }
>             catch(Exception x){
>               System.out.println( "Here " + x );
>             }         
>                                                   String url = 
> "jdbc:ibm-u2://192.168.0.102/SHIPPING";
>                                                   String      userid
> = "ups";
>                                                   String passWord  = 
> "brown";
> 
>             Connection con = DriverManager.getConnection(url, userid, 
> passWord);
>  
>             Statement stmt = con.createStatement();
> 
>                                       String sql = "select @ID, NAME, 
> CITY, STATE from PACKSLIPS.X";
>             ResultSet rs = stmt.executeQuery(sql);
> 
>             int i = 1;
>             while (rs.next() && i < 6)
>             {
>                 System.out.println("\nRecord "+ i +" :");
>                 System.out.println("\...@id : \t" + rs.getString(1));
>                 System.out.println("\tNAME :\t" + rs.getString(2));
>                 System.out.println("\tCITY :\t" + rs.getString(3));
>                 System.out.println("\tSTATE :\t" + rs.getString(4));
>                 i++;
>             }
> 
>             rs.close();
>             stmt.close() ;
>             System.out.println("\n\t*--- QUERY test is done successful
> ---*\n");        
>         
>         }
>         catch (SQLException e ) {
>             System.out.println(e);
>         }
>         finally{
>               try{
>                       con.close();
>             }
>               catch(Exception ignored){}
>         }
>     }
> 
> }
> 
> -----Original Message-----
> From: u2-users-boun...@listserver.u2ug.org
> [mailto:u2-users-boun...@listserver.u2ug.org] On Behalf Of Ben Souther
> Sent: Tuesday, March 23, 2010 5:34 PM
> To: U2 Users List
> Subject: Re: [U2] JDBC - ClassPath - Victory
> 
> Glad it's working.
> 
> Tip:  You should always put the con.close() statement in a finally 
> block.
> The close method itself can throw an exception so it needs to be in a 
> nested try/catch.
> 
> Putting in in a finally block insures that come Hello or high water 
> the connection gets closed.
> One of the most common causes for memory leaks in JDBC applications is

> the build up of unclosed database connections.
> 
> 
> Connection con = null;
> try{
>       // establish connection and do stuff
> }catch(Exception e){
>       // deal with any problems
> }finally{
>       try{
>               con.close();
>         }catch(Exception ignored){}
> }
> 
> 
> 
> 
> 
> 
> 
> On Mar 23, 2010, at 3:33 PM, Brutzman, Bill wrote:
> 
> > The following code works ok...
> > 
> > Thanks to all those who responded especially John, Jeff, Bruce, 
> > Charles, Ben, and Mike.
> > 
> > --Bill
> > 
> > import java.sql.*;
> > 
> > public class Uni_101 {
> > 
> >    public static void main(String[] args) {
> >        try {
> > 
> >            try {
> >             Class.forName("com.ibm.u2.jdbc.UniJDBCDriver"); 
> >            }
> >            catch(Exception x){
> >             System.out.println( "Here " + x );
> >            }         
> >                                                  String url = 
> > "jdbc:ibm-u2://192.168.0.102/SHIPPING";
> >                                                  String      userid
> > = "ups";
> >                                                  String passWord  = 
> > "brown";
> > 
> >            Connection con = DriverManager.getConnection(url, userid,

> > passWord);
> > 
> >            Statement stmt = con.createStatement();
> > 
> >                                      String sql = "select @ID, NAME,

> > CITY, STATE from PACKSLIPS.X";
> >            ResultSet rs = stmt.executeQuery(sql);
> > 
> >            int i = 1;
> >            while (rs.next() && i < 6)
> >            {
> >                System.out.println("\nRecord "+ i +" :");
> >                System.out.println("\...@id : \t" + rs.getString(1));
> >                System.out.println("\tNAME :\t" + rs.getString(2));
> >                System.out.println("\tCITY :\t" + rs.getString(3));
> >                System.out.println("\tSTATE :\t" + rs.getString(4));
> >                i++;
> >            }
> > 
> >            rs.close();
> >            stmt.close() ;
> >            System.out.println("\n\t*--- QUERY test is done
successful
> > ---*\n");        
> > 
> >        }
> >        catch (SQLException e ) {
> >            System.out.println(e);
> >        }
> >    }
> > 
> > }
> > _______________________________________________
> > U2-Users mailing list
> > U2-Users@listserver.u2ug.org
> > http://listserver.u2ug.org/mailman/listinfo/u2-users
> > 
> 
> _______________________________________________
> U2-Users mailing list
> U2-Users@listserver.u2ug.org
> http://listserver.u2ug.org/mailman/listinfo/u2-users
> _______________________________________________
> U2-Users mailing list
> U2-Users@listserver.u2ug.org
> http://listserver.u2ug.org/mailman/listinfo/u2-users
> 
--
Ben Souther
Manager, Web and Web Service Development bsout...@fwdco.com |
508.927.8147 FWDavison & Company, Inc.
10 Cordage Park Circle, Suite 200
Plymouth, MA 02360-7318

CONFIDENTIALITY NOTICE:  This e-mail message, and any accompanying
documents, is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized
review, use, disclosure, distribution or copying is prohibited. If you
are not the intended recipient, please contact our office by e-mail or
by telephone at (508) 747-7261 and immediately destroy all copies of the
original message.

_______________________________________________
U2-Users mailing list
U2-Users@listserver.u2ug.org
http://listserver.u2ug.org/mailman/listinfo/u2-users
_______________________________________________
U2-Users mailing list
U2-Users@listserver.u2ug.org
http://listserver.u2ug.org/mailman/listinfo/u2-users

Reply via email to