connecting to database help

First let me say sorry as i have already started this post in another thread, but know one seems to be replying to it any more. the other thread can be found out: http://forum.java.sun.com/thread.jspa?messageID=9413573

package coreservlets;

publicclass Catalog{

privatestatic CatalogItem[] items =

{new CatalogItem

("hall002",

"<I>Core Web Programming, 2nd Edition</I> " +

"by Marty Hall and Larry Brown",

"One stop shopping for the Web programmer. " +

"Topics include \n" +

"<UL><LI>Thorough coverage of Java 2; " +

"including Threads, Networking, Swing, \n" +

"Java 2D, RMI, JDBC, and Collections\n" +

"<LI>A fast introduction to HTML 4.01, " +

"including frames, style sheets, and layers.\n" +

"<LI>A fast introduction to HTTP 1.1, " +

"servlets, and JavaServer Pages.\n" +

"<LI>A quick overview of JavaScript 1.2\n" +

"</UL>",

49.99),

new CatalogItem

("lewis001",

"<I>The Chronicles of Narnia</I> by C.S. Lewis",

"The classic children's adventure pitting " +

"Aslan the Great Lion and his followers\n" +

"against the White Witch and the forces " +

"of evil. Dragons, magicians, quests, \n" +

"and talking animals wound around a deep " +

"spiritual allegory. Series includes\n" +

"<I>The Magician's Nephew</I>,\n" +

"<I>The Lion, the Witch and the Wardrobe</I>,\n" +

"<I>The Horse and His Boy</I>,\n" +

"<I>Prince Caspian</I>,\n" +

"<I>The Voyage of the Dawn Treader</I>,\n" +

"<I>The Silver Chair</I>, and \n" +

"<I>The Last Battle</I>.",

19.95),

new CatalogItem

("alexander001",

"<I>The Prydain Series</I> by Lloyd Alexander",

"Humble pig-keeper Taran joins mighty " +

"Lord Gwydion in his battle against\n" +

"Arawn the Lord of Annuvin. Joined by " +

"his loyal friends the beautiful princess\n" +

"Eilonwy, wannabe bard Fflewddur Fflam," +

"and furry half-man Gurgi, Taran discovers " +

"courage, nobility, and other values along\n" +

"the way. Series includes\n" +

"<I>The Book of Three</I>,\n" +

"<I>The Black Cauldron</I>,\n" +

"<I>The Castle of Llyr</I>,\n" +

"<I>Taran Wanderer</I>, and\n" +

"<I>The High King</I>.",

19.95),

new CatalogItem

("rowling001",

"<I>The Harry Potter Series</I> by J.K. Rowling",

"The first five of the popular stories " +

"about wizard-in-training Harry Potter\n" +

"topped both the adult and children's " +

"best-seller lists. Series includes\n" +

"<I>Harry Potter and the Sorcerer's Stone</I>,\n" +

"<I>Harry Potter and the Chamber of Secrets</I>,\n" +

"<I>Harry Potter and the " +

"Prisoner of Azkaban</I>,\n" +

"<I>Harry Potter and the Goblet of Fire</I>, and\n" +

"<I>Harry Potter and the "+

"Order of the Phoenix</I>.\n",

59.95)

};

publicstatic CatalogItem getItem(String itemID){

CatalogItem item;

if (itemID ==null){

return(null);

}

for(int i=0; i<items.length; i++){

item = items[i];

if (itemID.equals(item.getItemID())){

return(item);

}

}

return(null);

}

}

Using the above servlet and this one:

package coreservlets

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

publicclass browserextends HttpServlet{

publicvoid doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Connection conn =null;

try{

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

}catch(Exception e){

System.out.println(e);

}

// connecting to database

try{

conn = DriverManager.getConnection

("jdbc:mysql://MY WEB ADDRESS FOR THE SQL TABLES GOES HERE");

// System.out.println("Connection to database successful.");

}

catch(SQLException se){

System.out.println(se);

}

try{

// Get the category from the input form

String categoryString = request.getParameter("category");

// check if no category

if (categoryString =="") categoryString ="Action & Adventure";

String selectSQL ="select title, director, rating, year_released, price, stock_count, image_name "+

"from video_recordings "+

"where category = '" + categoryString +"'";

Statement stmt = conn.createStatement();

ResultSet rs1 = stmt.executeQuery(selectSQL);

// output html headers

String title ="Films in the " + categoryString +" genre" ;

out.println(ServletUtilities.headWithTitle(title) +

"><BODY BGCOLOR=\"#a00e0e\">\n" +

"<center><img src=\"http://localhost:8080/examples/pic.jpg\" width=350 height=200/></center>\n" +

"<H1 ALIGN=\"CENTER\">" + title +"</H1>\n");

out.println("<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +

"<TR BGCOLOR=\"#FFAD00\">\n" +

" <TH>Title\n" +

" <TH>Director\n" +

" <TH>Rating\n" +

" <TH>Year Released\n" +

" <TH>Price\n" +

" <TH>Number in stock\n" +

" <TH>image name"

);

// Retrieve the results

while(rs1.next()){

out.println("<TR>" +

"<TD>" + rs1.getString("title") +"</TD>" +

"<TD>" + rs1.getString("director") +"</TD>" +

"<TD>" + rs1.getString("rating") +"</TD>" +

"<TD>" + rs1.getDouble("year_released") +"</TD>" +

"<TD>" + rs1.getString("price") +"</TD>" +

"<TD>" + rs1.getString("stock_count") +"</TD>" +

"<TD>" + rs1.getString("image_name") +"</TD>\n");

"<TD> <IMG SRC=\"../images/music/" + image_name +"\">"

}

// close the html

out.println("</TABLE></BODY></HTML>");

// Close the stament and database connection

stmt.close();

conn.close();

}catch(SQLException se){

System.out.println(se);

}

}

}

How do i merge these two together so that a catalogItem is created by connecting to the database and uses the select statement? If any one can help me here i would be really grateful as i am having real trouble with this.

[11973 byte] By [ajrobsona] at [2007-11-26 13:35:22]
# 1
anybody?
ajrobsona at 2007-7-7 22:18:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Are you trying to SPAM on the forum?If so please do not Post such huge codes and show that you are such a big dumb.
reflex2javaa at 2007-7-7 22:18:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I was answering the old thread, but I gave up as I do not have the skills to answer that kind of question. Let me give it one more try.

1. Toss out the first hunk of code completely. If you're going to have catalog items in your database then there is no reason to have hard-coded data like that.

2. Explain why you need to create catalog items at all. The second hunk of code seems to produce not-completely-useless HTML. (You're missing a </TR> and some other things but browsers will generally patch up things like that.)

Basically I don't understand what you are trying to do and why you are trying to do it.

DrClapa at 2007-7-7 22:18:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

ok lets forget about the code, i have a mysql database what i need is a servlet that will list the items so that a user can click a button and add them to a shopping cart.

the first bit of code was from www.coreservlets.com so i was trying to merge both codes so instead of having to have each item hard-coded into the servlet it would go and select each item from the database.

ajrobsona at 2007-7-7 22:18:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
come on somebody must be able to help me, all i want is a catalog / shopping cart lol
ajrobsona at 2007-7-7 22:18:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Below is what i have come up with (I cut out the code that just gets in the way for this post, eg the connect to database and import statements) My problem is this would only work once has i need to add the item to the array addedItem in the first for loop, can anybody help me with this has i have tried a number of ways but can't get it to compile.

private static CatalogItem[] addedItem

private static CatalogItem[] ItemsInCat;

CatalogItem item;

CatalogItem details;

int recordingidDB;

String directorDB;

String titleDB;

String categoryDB;

String imageDB;

int durationDB;

String ratingDB;

String yearDB;

float priceDB;

int StockDB;

// Create select statement and execute it

try{

String selectSQL = "select title, director, rating, year_released, price, stock_count, image_name "+

"from video_recordings " + "'";

Statement stmt = conn.createStatement();

ResultSet rs1 = stmt.executeQuery(selectSQL);

while(rs1.next()){

recordingidDB = rs1.getInt("recording_id");

directorDB = rs1.getString("director");

titleDB = rs1.getString("title");

categoryDB = rs1.getString("category");

imageDB = rs1.getString("image_name");

durationDB = rs1.getInt("duration");

ratingDB = rs1.getString("rating");

yearDB = rs1.getString("year_released");

priceDB = rs1.getFloat("price");

stockDB = rs1.getInt("stock_count");

item =

new CatalogItem

(recordingidDB, directorDB,titleDB, categoryDB, imageDB, durationDB, ratingDB, yearDB, priceDB, stockDB);

}

String title = "Catalog Items";

out.println(ServletUtilities.headWithTitle(title) +

"<BODY BGCOLOR=\"#a00e0e\">\n" +

"<center><img src=\"http://localhost:8080/examples/img.jpg\" width=350 height=200/></center>\n" +

"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");

// loop to go over each item in the array of catalogItem

for (int i=0; i < ItemsInCat.length; i++) {

out.println("__");

details = item[i];

if (details == null) {

out.println("SORRY THERE HAS BEEN AN ERROR ");

} else {

addedItem[] // needd to store item in array here

out.println(titleDB + "\n" + priceDB + "\n");

out.println("</BODY></HTML>");

}

}

public static CatalogItem getItem(int recordingidDB) {

CatalogItem item;

for(int i=0; i<itemsInCat.length; i++) {

item = itemsInCat[i];

if (recordingidDB.equals(item.getItemID())) {

return(item);

}

}

return(null);

}

// Close the stament and database connection

>

ajrobsona at 2007-7-7 22:18:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
bump
ajrobsona at 2007-7-7 22:18:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...