catalog

I am trying to make a catalog using a java servlet i found this example on coreservlets.com

package coreservlets;

/** A catalog that lists the items available in inventory.

* <P>

* Taken from Core Servlets and JavaServer Pages 2nd Edition

* from Prentice Hall and Sun Microsystems Press,

* http://www.coreservlets.com/.

* © 2003 Marty Hall; may be freely used or adapted.

*/

publicclass Catalog{

// This would come from a database in real life.

// We use a static table for ease of testing and deployment.

// See JDBC chapters for info on using databases in

// servlets and JSP pages.

privatestatic CatalogItem[] items =

{new CatalogItem

("hall001",

"<I>Core Servlets and JavaServer Pages " +

"2nd Edition</I> (Volume 1)" +

" by Marty Hall and Larry Brown",

"The definitive reference on servlets " +

"and JSP from Prentice Hall and \n" +

"Sun Microsystems Press.<P>N),

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);

}

}

Can anyone give me advice on how i would use this to connect to mysql tables to get items from that? I have already made a servlet that uses select statements to bring back a type of book, so i know how to connect and get info from my database but how would i put the select statements in to this?>

[7456 byte] By [ajrobsona] at [2007-11-26 13:27:21]
# 1
anyone? come on make my christmas lol and earn some dukes!!
ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
anyone?
ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Can anyone give me advice on how i would use this to connect to mysql tables to get items from that?

I don't understand the question at all. You have some data classes there. You just wouldn't use them to connect to database tables at all. The question doesn't make any sense.

You might well write code that reads from a database table and creates CatalogItem elements, but you say you already know how to read from a database table. So why not just do that?

DrClapa at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Yes that is what i want to dom use this statement:

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);

but when i tried to make that a catalogItem it did not work. categorystring comes from a form, so i want to edit the servlet so instead of using a static table it uses this select statement for to add catalogItems. can anybody help me out?

Merry christmas!!

ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> but when i tried to make that a catalogItem it did not work.

I don't understand that either. You wouldn't make that code into a CatalogItem anyway, I can't even imagine what that means. Not to mention that you didn't post the code that "did not work" -- unless that was it?

What you do is to read through the ResultSet. Each row in it would represent a CatalogItem, so you would create one CatalogItem for each row by calling a suitable constructor and/or suitable setter methods.

DrClapa at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Ok maybe this will make it easier to understand http://volume1.coreservlets.com/archive/Chapter9.html I want to know how i can use these to connect to my database instead of using a static table like in the examples.
ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> Yes that is what i want to dom use this statement:

>

> String selectSQL = "select title, director, rating,

> year_released, price, stock_count, image_name "+

>"from video_recordings "+

> "where category = '" +

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

>Statement stmt = conn.createStatement();

> ResultSet rs1 = stmt.executeQuery(selectSQL);

>

> but when i tried to make that a catalogItem it did

> not work. categorystring comes from a form, so i want

> to edit the servlet so instead of using a static

> table it uses this select statement for to add

> catalogItems. can anybody help me out?

>

> Merry christmas!!

In the above I dont see where you get the database connection?

for jdbc,

step 1: Class.forName("com.mysql.jdbc.Driver");

step 2: String url =

"jdbc:mysql://localhost:3306/mysql"

(for localhost use the correct host name/port)

step 3: Connection con =

DriverManager.getConnection(

url,"root", "")

if ypu use the above the connection will be obtained and then you can work on the connection. further more please put the mysql jar file on the classpath.

reflex2javaa at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

package coreservlets;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class browser extends HttpServlet {

public void 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://LOCATION OF MYSQL TABLES");

}

catch(SQLException se) {

System.out.println(se);

}

try{

// Get the category from the input form

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

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

// Build up the SQL statement from our data requirements

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/LOGO.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 +"\">"

}

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

stmt.close();

conn.close();

} catch(SQLException se) {

System.out.println(se);

}

}

}

Thats a servlet i made before which can bring data back from a table, how would i get that working with the catalogitem servlet from www.coreservlets.com so that the data in this servlet (browser.java) would be a catalogItem?

ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Try the following

// connecting to database

try{

conn = DriverManager.getConnection

("jdbc:mysql://localhost:3306/mysql");

}

reflex2javaa at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

// connecting to database

try{

conn = DriverManager.getConnection

("jdbc:mysql://localhost:3306/mysql");

}

How would that help? in quotes i put the address of the site where my tables are i just took them out for security when i posted them here.

ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
What error you are getting?Put your stack trace and check if you have the driverfile on the classpath!
reflex2javaa at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
I do not get an error i just want to know how i would go about using these two servlets to make a single servlet that read data from the table and makes it into a catalog item so that each item can then be added to a cart,
ajrobsona at 2007-7-7 20:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
any body have a idea?
ajrobsona at 2007-7-7 20:28:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
If i know how to and have a servlet that selects from a database can i just merge the two servelts together? and in the catlog item method just put the select and results set part from my browse servlet?
ajrobsona at 2007-7-7 20:28:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
bump
ajrobsona at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16

Ok i'll try one more post to see if any one can help me.

package coreservlets;

/** A catalog that lists the items available in inventory.

* <P>

* Taken from Core Servlets and JavaServer Pages 2nd Edition

* from Prentice Hall and Sun Microsystems Press,

* http://www.coreservlets.com/.

* © 2003 Marty Hall; may be freely used or adapted.

*/

public class Catalog {

// This would come from a database in real life.

// We use a static table for ease of testing and deployment.

// See JDBC chapters for info on using databases in

// servlets and JSP pages.

private static 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)

};

public static 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.*;

public class browser extends HttpServlet {

public void 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/LOGO.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.

ajrobsona at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17
Give me all 10 duke dollars I will do it for you.
reflex2javaa at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18
really? don't joke with me lol this is driving me crazy!!
ajrobsona at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19
reply if you want my dollars lol and will do the servlet for me!
ajrobsona at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 20

@ajrobson

Kindly answer the following,

1. Why are you posting multiple threads for the same problem?

2. What is the problem with you?

If you are new to database/servlets please read the documentation first. Therer are thousands of documents available on the internet. Instad donj just post new threads and you stop replying to your own post.

My kind advice at this point of time is please keep quite and READ ON HOW AND WHAT? KIND OF documents you wil surly get a way out.

For heven sake please dont reply to your own post.

This is another thread I found similar to your request with diffrent subject:

http://forum.java.sun.com/thread.jspa?threadID=5119322&tstart=0

reflex2javaa at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 21

Seasons greatings to you to!! if you spent a minute looking at the other thread you would see " 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 " I have seen many instances of people doing this and i posted a link to the other thread, i hardly think two threads from me is going to cause this site to come crashing down.

"2. What is the problem with you?" thats a bit vague and i don't really see the point of that question, i suggest you not post rubbish on these threads i mean if your having a go at me for posting two threads (the horror imagine posting the same question twice and telling people about it!!) i think its very ironic that you come and talk rubbish on them.

"If you are new to database/servlets please read the documentation first. Therer are thousands of documents available on the internet. Instad donj just post new threads and you stop replying to your own post."

The whole point of forums like this one is to ask for help i have searched the internet and could not find what i needed, the whole point of this forum is to ask advice from people, so what your saying is lets not bother using this any more if all your going to do is ask for help that is already on the internet? well the internet is pretty big so what your saying is get rid of forums becuase you can find the info else where! Do you know what happens when you reply to your own post? it get bumped up to the top of the thread list this means that anyone searching the forums will see my thread at the top, again this is something that people on these forums do many times do you complain to each of them?

"My kind advice at this point of time is please keep quite" Please take your own advice your clearly not helping with this thread so please do not reply unhelpful posts.

Now can anybody help me or is this thread just going to turn into a waste of space.....I might have to start a 3rd thread!!!!!

ajrobsona at 2007-7-21 15:51:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...