Solved

JSP database Class

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
servlet init param and contect param 3 66
Reading data  from excel sheet and displaying 1 89
maven j2ee examles 2 60
spring example non maven 4 92
Is your phone running out of space to hold pictures?  This article will show you quick tips on how to solve this problem.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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