Solved

JSP database Class

Posted on 2004-04-21
5
249 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A procedure for exporting installed hotfix details of remote computers using powershell
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now