Problematic Code.... Please correct me.

foongkim2
foongkim2 used Ask the Experts™
on
Hi dear, below is my java code which I have problem with. Hope somebody can correct me.

     public String getStk1()     {
     try {
                Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
                con = DriverManager.getConnection("jdbc:db2:*local");
             
                 String queryString =("SELECT * FROM ST " +
                                      "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk1+"' ");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(queryString);

             if (rs.next()) {
               String ab = rs.getString("TZ1011");                                
                  stk1 = ab;
                  return stk1;
          }
          } catch (SQLException sqle) {
                        error = ("SQLException.");
                        throw new SQLException (error);
             } catch (Exception e) {
                        error = ("Exception : An Unknown error occur.");
                        throw new Exception (error);
             }
     }

A lot of problem in my code right? Actually what I plan to do is retrieve the TZ1011 from the table of ST table. Yet when I complied my code that's prompt me error.

And another things is how can I catch my exception so that it'll throw the respective exception to me instaed of error = "Exception : An Unknown error occur.".

Thank you

:-)

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Soory... my own correction...

I should decllre the con first.

    public String getStk1()     {
    try {
               Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
               Connection con = DriverManager.getConnection("jdbc:db2:*local");
           
                String queryString =("SELECT * FROM ST " +
                                     "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk1+"' ");
               Statement stmt = con.createStatement();
               ResultSet rs = stmt.executeQuery(queryString);

            if (rs.next()) {
              String ab = rs.getString("TZ1011");                                
                 stk1 = ab;
                 return stk1;
         }
         } catch (SQLException sqle) {
                       error = ("SQLException.");
                       throw new SQLException (error);
            } catch (Exception e) {
                       error = ("Exception : An Unknown error occur.");
                       throw new Exception (error);
            }
    }
Java Developer
Top Expert 2010
Commented:
Try the following, I'm assuming stk1 and error are declared as member variables.


public String getStk1() throws Exception {
   try {
     Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
     Connection con = DriverManager.getConnection("jdbc:db2:*local");
           
     String queryString = "SELECT * FROM ST " +
        "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk1+"' ";
     Statement stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery(queryString);

     if (rs.next()) {
        stk1 = rs.getString("TZ1011");
        return stk1;
     }
  } catch (SQLException sqle) {
     error = "SQLException.";
     throw new Exception (error);
  } catch (Exception e) {
     error = "Exception : An Unknown error occur.";
     throw new Exception (error);
  }
}
Mick BarryJava Developer
Top Expert 2010

Commented:
You probably also want to close your statement and connection once you are done.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
object, tthis is my latest proven can run. (Not your code) But it's seems if I have 10 stk, mean stk1 - stk10, then I have to put in 10 time in 10 different getStk1(). ANy ideas to correct or make it more nice.


public String getStk1()     {
   try {
              Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
              Connection con = DriverManager.getConnection("jdbc:db2:*local");
           
               String queryString =("SELECT * FROM ST " +
                                    "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk1+"' ");
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery(queryString);

           if (rs.next()) {
             String ab = rs.getString("TZ1011");                                
                stk1 = ab;
        }
        } catch (SQLException sqle) {
                      error = ("SQLException.");
                      throw new SQLException (error);
           } catch (Exception e) {
                      error = ("Exception : An Unknown error occur.");
                      throw new Exception (error);
           }
                return stk1;

   }


public String getStk2()     {
   try {
              Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
              Connection con = DriverManager.getConnection("jdbc:db2:*local");
           
               String queryString =("SELECT * FROM ST " +
                                    "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk2+"' ");
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery(queryString);

           if (rs.next()) {
             String ab = rs.getString("TZ1011");                                
                stk2 = ab;
        }
        } catch (SQLException sqle) {
                      error = ("SQLException Got Problem.");
                      throw new SQLException (error);
           } catch (Exception e) {
                      error = ("Exception : An Unknown error occur.");
                      throw new Exception (error);
           }
                return stk2;

   }

....
....
....

Look awfull right?  Another things, last time u did teach me how to catch the error and let the system automatically thown the system for me to see. In my case here, I throw  "SQLException Got Problem." and if normal exception, I will throw the "Exception : An Unknown error occur." This is not the correct way. I should let the system it self tell me what;'s went wrong. COrrect? PLease teach me... TQ/
Mick BarryJava Developer
Top Expert 2010

Commented:
String[] stk = new String[10];
// and initiaze if necessary

public String getStk(int n) throws SQLException {
  try {
    Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
    Connection con = DriverManager.getConnection("jdbc:db2:*local");
         
    String queryString = "SELECT * FROM ST " +
       "WHERE TZ1001='STOCK-CODE' and TZ1002='"+stk[n]+"' ";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(queryString);

    if (rs.next()) {
       stk[n] = rs.getString("TZ1011");
       return stk[n];
    }
  } finally {
    // Close result set, statement and connection here
  }
}

Author

Commented:
objects, the

public String getStk(int n) throws SQLException {
 try {
     ...
     ...
  }finally { ... }

will prompt the respective error from the system right without human intereaction right?

*********************

String[] stk = new String[10];

After I declare this one already, is it necessary for me to declare the

private String stk1="";
private String stk2="";
.
.
.
private String stk10="";

anymore??

Author

Commented:
I am testing it now...

Author

Commented:
How will be the "n" to be looped??
Mick BarryJava Developer
Top Expert 2010

Commented:

> will prompt the respective error from the system right
> without human intereaction right?

It will casue any exception to be passed up for the method caller to handle.

> After I declare this one already, is it necessary for me > to declare the

No.

Instead of having 10 seperate int variables, or instead have an array containing 10 int's.

Author

Commented:
How will be the "n" to be looped??

Author

Commented:
Object, how about the "Set" methods??

public void setStk[n] (String s[n]) {
     stk[n] = s[n];
}

It's give me error, pointing at setStk[n] saying "(" required.

I need to set the value first before I can make a query right??
Mick BarryJava Developer
Top Expert 2010

Commented:
public void setStk(int n, String s) {
    stk[n] = s;
}

Author

Commented:
Hm... OK.

Thank you.
Mick BarryJava Developer
Top Expert 2010

Commented:
Anytime :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial