Re: [U2] JDBC - Finally - Hitch

2010-03-24 Thread Brutzman, Bill
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 

Re: [U2] JDBC - Finally - Hitch

2010-03-24 Thread Ben Souther
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 

Re: [U2] JDBC - Finally - Hitch

2010-03-24 Thread Jeff Powell

Bill,

You're trying to reference the connection out of it's scope.

Try moving the declaration like the example below.


On 03/24/2010 10:49 AM, 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) {
 


Connection con=null;


 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{
   

*   if(con != null)*
 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);
}
}

}

Re: [U2] JDBC - Finally - Hitch

2010-03-24 Thread Jeff Powell
This text was supposed to be strikethrough to indicate that it needs to 
be removed since it's declared outside the outer try scope.



On 03/24/2010 05:40 PM, Jeff Powell wrote:

*Connection*  con = DriverManager ...

Should read

  con = DriverManager ...


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