Go Back   SitePoint Forums > Forum Index > Program Your Site > Java and JSP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Oct 23, 2009, 20:45   #1
chopsticks
SitePoint Enthusiast
 
Join Date: Nov 2006
Location: Sydney, Australia
Posts: 30
JSP - Using a class and passing the SQL connection to it

Hello,

I am new to JSP so I am having trouble coding my project site. Please excuse my bad coding practices.

Basically, I wanted to enclose all my methods related to an auction in one class which could then be accessed by all the other pages in the site by accessing the created object.

I was wondering though, is it possible to pass a connection like:

Java Code:
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");

to the class to give access to the database? The way I was previously doing created too much redundant code. I keep getting an SQLException whenever I try and use the createStatement() inside the class which otherwise works fine anywhere not in a class.

The code I am trying to use (it's inside my index.jsp file for now):

Java Code:
<%!
 
public class AuctionManager
{
    private Connection c;
    int auctionID;
 
    public AuctionManager(Connection c, int auctionID)
    {
        this.c = c;
        this.auctionID = auctionID;
    }
   
    public void placeBid(int memberID, int bidValue)
    {
   
    }
   
    public AuctionBid[] getBidList()
    {
        AuctionBid[] bidList = new AuctionBid[25];
 
        Statement s = this.c.createStatement();
 
        ResultSet result;
 
        String sql;
 
        sql =   "SELECT " +
                "   ab.auctionID, ab.memberID, ab.bidID, ab.bidValue, ab.bidTime, m.memberName " +
                "FROM " +
                "   auction_bid AS ab, member AS m " +
                "WHERE " +
                "   ab.auctionID = '" + auctionID + "' AND m.memberID = ab.memberID " +
                "ORDER BY " +
                "   ab.bidTime DESC " +
                "LIMIT " +
                "   25";
 
        result = s.executeQuery(sql);
 
        // iterate over each result and store in an "AuctionBid"
`
        //  - just for return's sake in testing
        bidList[0] = new AuctionBid(1,1,1,100000000,"Joe Blow");
       
        return bidList;
    }
}
 
%>

Having trouble finding anything useful anywhere...

Thanks in advanced for any help!
chopsticks is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 08:59.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved