Solved

JSP database Class

Posted on 2004-04-21
5
254 Views
Last Modified: 2010-04-01
I am trying to create a basic database class that I can use in my pages that will allow me to easily open a connection, query (returning a Resultset) and close the connection.... I don't need a connection pool...etc as I can plug that in myself.

I thought I'd ask if there is already one written...

Luke
0
Comment
Question by:chambers777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 125 total points
ID: 10881142
See if this will work,
Its connecting to a DB2/400 database..


package com.csc.pt.access;
import java.sql.*;
import com.ibm.as400.access.*;
public class DbBean
{

    public DbBean(String address)
    {
        dbURL = "jdbc:as400://" + address;
        dbDriver = "com.ibm.as400.access.AS400JDBCDriver";
    }

    public void close()
        throws SQLException
    {
        dbCon.close();
            connected = false;
    }

    public boolean connect(String usr, String pwd)
        throws ClassNotFoundException, SQLException
    {
            if (!connected)
            {
            Class.forName(dbDriver);
                             dbCon = (AS400JDBCConnection)DriverManager.getConnection(localdbURL, usr, pwd);
                      connected = true;
                          return true;
            }//end if
            else
            {
                  return true;
            }
    }

    public ResultSet execSQL(String s)
        throws SQLException
    {
        Statement statement = dbCon.createStatement();
        ResultSet resultset = statement.executeQuery(s);
        return resultset != null ? resultset : null;
    }
      
      public boolean execSQLUpdate(String s)
        throws SQLException
    {
        Statement statement = dbCon.createStatement();
        int result = statement.executeUpdate(s);
        if(result>0)
            return true;
            else
            return false;      
    }

    public ResultSet execStrSQL(String s)
        throws SQLException
    {
        java.sql.CallableStatement callablestatement = dbCon.prepareCall(s);
        ResultSet resultset = null;
        try
        {
            resultset = callablestatement.executeQuery();
        }
        catch(SQLException sqlexception)
        {
            int i = sqlexception.getErrorCode();
            System.out.println("Error code is " + i + sqlexception.getMessage());
        }
        catch(NullPointerException _ex)
        {
            System.out.println("Caught Null p");
        }
        finally
        {
            return resultset != null ? resultset : null;
        }
    }

    public String getYr(String s)
    {
        if(s.substring(0, 1).equals("1"))
            return "20" + s.substring(1, 3);
        else
            return "19" + s.substring(1, 3);
    }

    public boolean isConnected()
    {
        return connected;
    }

    public int updateSQL(String s)
        throws SQLException
    {
        Statement statement = dbCon.createStatement();
        int i = statement.executeUpdate(s);
        return i != 0 ? i : 0;
    }

    String dbURL;
    String dbDriver;
    String libl;
    private boolean connected;
    private AS400JDBCConnection dbCon;
}
0
 

Author Comment

by:chambers777
ID: 10883109
Gotcha... looks good..
Now if I return a resultset, do I have to close it in the calling statement?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10883120
You can but then you will have to open them again next time around..
I just don't close it because I may use it multiple times on the same page.....
0
 

Author Comment

by:chambers777
ID: 10884112
Thanks! Works Like a charm!
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10891971
Glad to be of help...:-)
Don't forget to close the question..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question