Solved

JSP database Class

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Original post  on Monitis Blog. Web performance monitoring is broken into two camps: passive and active. Passive monitoring is defined as looking at real-world historical performance by monitoring actual log-ins, site hits, clicks, requests for…
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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