?
Solved

JSP database Class

Posted on 2004-04-21
5
Medium Priority
?
255 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The Summer 2017 Scholarship Winners have been announced!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 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