Help making this into a bean

I tried making a bean that connects to a db and runs a queary then returns the results in a list, the problem is i started out using a exsiting servlet which did something simarliar! Stupid to do i know!! as now i am having all sorts of problems below is the code can somebody please help me get rid of the bits i don't need? I know i don't need to extend http and i can get rid of the doGet but by doing that i get errors with the try bits can somebody please help with this? Also everything as to be in one bean not spiltting it up into a number of beans or servlets to do connection, queries etc

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

publicclass MyBeanextends HttpServlet{

private String Category;

private String Price;

public MyBean(String Category, String Price){

this.Category = Category;

this.Price = Price;

}

public String getCategory(){

return Category;

}

public String getPrice(){

return Price;

}

publicvoid setCategory(String Cat){

Category = Cat;

}

publicvoid setPrice(String Price){

Price = Price;

}

public String toString(){

return Price;

}

public List getVideo(String p1, String p2){

ArrayList Vids =new ArrayList();

VideoBean v1 =new VideoBean();

v1.setArtist("some artist");

v1.setTitle("this has worked");

vids.add(v1);

return vids;

}

publicvoid doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Connection conn =null;

String CatPassed;

String PricePassed;

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

}catch(Exception e){

System.out.println(e);

}

try{

conn = DriverManager.getConnection

("my url goes in here");

}

catch(SQLException se){

System.out.println(se);

}

try{

CatPassed = request.getParameter("Category");

PricePassed = request.getParameter("Price");

String selectSQL ="Select artist_name, title" +

"from vids" +

"where category = '" + CatPassed +"'" +" and price = " + PricePassed;

Statement stmt0 = conn.createStatement();

ResultSet rs0 = stmt0.executeQuery(selectSQL);

System.out.println(selectSQL);

List collection =new ArrayList();

while(rs0.next()){

VideoBean videos =new VideoBean();

videos.setArtist(rs0.getString("artist_name"));

videos.setTitle(rs0.getString("title"));

collection.add(videos);

}

stmt0.close();

conn.close();

}

catch(SQLException se){

System.out.println(se);

}

}

}

Any Help at all would be excellent!

[5429 byte] By [eastendersa] at [2007-11-27 1:03:54]
# 1

Ok the following code is just a dummy version (just to provide something to what you requested) and it's not intended for production use. Also currently it doesn't compile since you didn't provide the code for VideoBean.

Few notes about the following code:

1) The use of raw types is descouraged starting from Java 5 and instead parametrized types (in the form of List<VideoBean>) should be used. Raw types should only be used in interaction with legacy code.

2) The business method gets a connection, does the business, releases the connection. In an ideal world, you would have this code in a helper class or better by using some utility functionalities to pool the connections, or better by using an application server (in a EE world) with datasources, etc

3) A JavaBean must follow some naming conventions. Amongst which there is the convention that an instance variable name should start with lowercase and then follow a camel lower upper case, so that a dosomethinguseful variable should be named doSomethingUseful. Each instance variable should then have a get/set method (called getters/setters). In Java usually variables start with lowercase and classes start with upper case.

This is the JavaBean code with a business method to run your stuff (untested!):

package uk.co.jemos.experiments;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

public class MyBean {

private String category;

private String price;

public MyBean(String category, String price){

this.category = category;

this.price = price;

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch(Exception e) {

System.out.println(e);

}

}

public String toString() {

return price;

}

public List getVideo(String p1, String p2) {

ArrayList vids = new ArrayList();//only ok in JDK < 1.5

VideoBean v1 = new VideoBean();

v1.setArtist("some artist");

v1.setTitle("this has worked");

vids.add(v1);

return vids;

}

public List runMyBusiness() throws IOException {

Connection conn = null;

//The return value

List collection = new ArrayList();

try{

conn = DriverManager.getConnection

("my url goes in here");

String selectSQL = "Select artist_name, title" +

"from vids" +

"where category = '" + category + "'" + " and price = " + price;

Statement stmt0 = conn.createStatement();

ResultSet rs0 = stmt0.executeQuery(selectSQL);

System.out.println(selectSQL);

while(rs0.next()) {

VideoBean videos = new VideoBean();

videos.setArtist(rs0.getString("artist_name"));

videos.setTitle(rs0.getString("title"));

collection.add(videos);

}

stmt0.close();

}

catch(SQLException se) {

System.out.println(se);

//Always be sure of closing connections

} finally {

if (null != conn) {

conn.close();

}

}

return collection;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getPrice() {

return price;

}

public void setPrice(String price) {

this.price = price;

}

}

And this is a client to test your application (untested!):

package uk.co.jemos.experiments;

import java.io.IOException;

import java.util.List;

public class MyBeanClient {

public MyBeanClient() {

// TODO Auto-generated constructor stub

}

/**

* @param args

*/

public static void main(String[] args) {

MyBean myBean = new MyBean("Category", "Price");

try {

//Note that raw types like List should only

//be used by JDK versions prior to 1.5, otherwise

//parametrized types should be used

List videoBeans = myBean.runMyBusiness();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

The package is about my directory structure but I didn't want to create an ad-hoc one (it can always be changed to something else).

mtedonea at 2007-7-11 23:38:59 > top of Java-index,Java Essentials,New To Java...