Help with an error and more!

I keep getting the following error when trying to compile the code below:

MySite/MyBean.java:93: illegal start of expression

public List getvideo(String p1, String p2){

^

1 error

Where should i be putting this method? i tried it in the while loop but got the same. I am also trying to take out the servlet bits so this is a bean! i have made some notes on the servlet / bean if someone one could reply to those i would be very happy! Thanks for any help given

Below is a stripped down version of what i am working with.

package MySite;

import java.io.*;

import java.util.*;

import javax.servlet.*;// do i need this line

import javax.servlet.http.*;// do i need this line

import java.sql.*;

publicclass MyBeanextends HttpServlet{

private String Form1;

private String Form2;

public MyBean(String Form1, String Form2){

this.Form1 = Form1;

this.Form2 = Form2;

}

public String getForm1(){

return Form1;

}

public String getForm2(){

return Form2;

}

publicvoid setForm1(String For1){

Form1 = For1;

}

publicvoid setForm2(String For2){

Form2 = For2;

}

public String toString(){

return Form2 +"this is from MyBean to string";

}

publicvoid doGet(HttpServletRequest request,///////Can i get rid of this method?

HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Connection conn =null;

String For1Passed;

String For2Passed;

try{

// DATABASE CONNECTION BITS GO IN HERE

}

// Create select statement and execute it

try{

// Get the infor from REGISTER html from the input form

For1Passed = request.getParameter("Form1");

For2Passed = request.getParameter("Form2");

//////////////////////////////////////////////////////////////////TRYING TO HARD CODE HERE

public List getvideo(String p1, String p2){

ArrayList video =new ArrayList();

VideoBean v1 =new VideoBean();

V1.setArtist("some artist");// hard coded

// set rest later

music.add(v1);

return music;

}

/////////////////////////////////////////////////////////////////////////

String selectSQL ="Select artist_name" +/////////////////put rest in later!!!!!!!!!!

"from video_recordings" +"where category = " + For1Passed +" and price <= " + For2Passed +";";

Statement stmt0 = conn.createStatement();

ResultSet rs0 = stmt0.executeQuery(selectSQL);

videoBean collection =new videoBean();// TRIED HARD CODING HERE WILL INSERT FROM RESULT SET HERE

collection.setArtist("this is set in the bean");

}

stmt0.close();

conn.close();

}catch(SQLException se){

System.out.println(se);

}

}

}

[5466 byte] By [eastendersa] at [2007-11-27 0:50:50]
# 1

you are trying to declare a method inside of another method. Take

public List getvideo(String p1, String p2) {

ArrayList video = new ArrayList();

VideoBean v1 = new VideoBean();

V1.setArtist("some artist"); // hard coded

// set rest later

music.add(v1);

return music;

}

out of the public void doGet(HttpServletRequest request, HttpServletResponse response) method.

cjmosea at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 2

thanks that compiles, but i am still having trouble getting it to work I have another servlet twhich calls this bean(it will be a bean soon! lol) i call it like so:

MyBean bean = new MyBean(categoryForm, PriceForm);

request.setAttribute("valid", bean);

List video= MyBean.getvideo(categoryForm, Form);

What should happen is mybean gets created with two params passed in, mybean then does a queary and passes the results back to this servlet but when i compile the servlet i get:

MySite/Validator.java:56: cannot resolve symbol

symbol : class List

location: class MySite.Validator

List video = MyBean.getvideo(categoryForm, PriceForm);

^

MySite/Validator.java:56: non-static method getvideo(java.lang.String,java.la

ng.String) cannot be referenced from a static context

List video= MyBean.getvideo(categoryForm, PriceForm);

^

2 errors

Can anybody tell me why? or tell me which lines are safe to remove to get MyBean a bean instead of a weird bean/servlet!

eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 3

as cjmode said, you're trying to declare a method inside another method - can't be done

I see you're still inappropriately extending HttpServlet. I really really urge you not to do that. quite apart from not being at all correct, when you post code here for help, everyone will just assume it's an actual servlet, and the advice you get might well be horrifically wrong. for your own sake, stop your bean from extending HttpServlet. you say you're trying to do this, but all it takes is to remove the bit that says "extends HttpServlet" and a little tweak of the doGet method. oh, and get rid of the constructor that takes args, too

georgemca at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 4
thats what i wanted to know do i keep the doGet method or remove it? i tried removing it and ended up with loads of errors so thought i leave it till i can get it to compile
eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 5

> thats what i wanted to know do i keep the doGet

> method or remove it? i tried removing it and ended up

> with loads of errors so thought i leave it till i can

> get it to compile

remove it. in fact, what I really suggest, since you seem to be running before you can walk, is delete the class altogether, do a quick, basic tutorial, and start again. trust me, it'll be a lot quicker than trying to untangle the mess you're in right now!

get rid of doGet, HttpRequests have nothing to do with what this class does. your servlet - the real one I mean ;-) - can pass in exactly what is needed, whatever that may be

georgemca at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 6

Thank you, I am trying to get rid of the doGet method do i need to get rid of all this bit:

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException { // got rid of close } too

response.setContentType("text/html");

PrintWriter out = response.getWriter();

By doing that it says the first try is " illegal start of type" and there is a <identifier> expected near the bottom of the code although not sure why i only commented out one { and one }

If i get rid of that doGet do i replace it with another method? and put the try inside that?

Message was edited by:

eastenders

eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 7

> Thank you, I am trying to get rid of the doGet method

> do i need to get rid of all this bit:

>

> public void doGet(HttpServletRequest

> request,

>HttpServletResponse response)

> throws ServletException, IOException { // got rid of

> close } too

> response.setContentType("text/html");

> PrintWriter out = response.getWriter();

> By doing that it says the first try is " illegal

> start of type" and there is a <identifier> expected

> near the bottom of the code although not sure why i

> only commented out one { and one }

>

> If i get rid of that doGet do i replace it with

> another method? and put the try inside that?

> Message was edited by:

> eastenders

now you're just guessing at things. seriously, throw it all out and start again. you could've done it by now!

georgemca at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 8

If i could get this done today i swear i would faint with joy!!

I'll try and start again but surely there is not much i need to chnage i mean these bits:

private String Form1;

private String Form2;

public MyBean(String Form1, String Form2){

this.Form1 = Form1;

this.Form2 = Form2;

}

public String getForm1(){

return Form1;

}

public String getForm2(){

return Form2;

}

public void setForm1(String For1) {

Form1 = For1;

}

public void setForm2(String For2) {

Form2 = For2;

}

public String toString() {

return Form2 + "this is from MyBean to string";

}

--

Connection conn = null;

String For1Passed;

String For2Passed;

try{

// DATABASE CONNECTION BITS GO IN HERE

}

// Create select statement and execute it

try{

// Get the infor from REGISTER html from the input form

For1Passed = request.getParameter("Form1");

For2Passed = request.getParameter("Form2");

//////////////////////////////////////////////////////////////////TRYING TO HARD CODE HERE

public List getvideo(String p1, String p2) {

ArrayList video = new ArrayList();

VideoBean v1 = new VideoBean();

V1.setArtist("some artist"); // hard coded

// set rest later

music.add(v1);

return music;

}

/////////////////////////////////////////////////////////////////////////

String selectSQL = "Select artist_name" + /////////////////put rest in later!!!!!!!!!!

"from video_recordings" + "where category = " + For1Passed +" and price <= " + For2Passed +";";

Statement stmt0 = conn.createStatement();

ResultSet rs0 = stmt0.executeQuery(selectSQL);

videoBean collection = new videoBean(); // TRIED HARD CODING HERE WILL INSERT FROM RESULT SET HERE

collection.setArtist("this is set in the bean");

}

stmt0.close();

conn.close();

} catch(SQLException se) {

System.out.println(se);

}

Semm to be ok to keep right?

eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 9

everything below the -- line is a touch wonky

you need to move all that code into methods of your MyBean class (haven't looked over what the code is actually doing so it might not be necessary to keep...).

you're defining a method within a try/catch block, which, to the best of my knowledge, is not valid (although if you're not getting compiler errors from that then I guess I learned something new today). Since the code below the line is not in a method/constructor there isn't any way for it to run, e.g., once your object is instantiated, how exactly do you plan on that code being run/accessed?

(hint, move out all that functionality into methods - remember that methods can call other methods).

cjmosea at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 10

ok this:public List getvideo(String p1, String p2) {

ArrayList video = new ArrayList();

VideoBean v1 = new VideoBean();

V1.setArtist("some artist"); // hard coded

// set rest later

video.add(v1);

return video;

}

now goes right after my tostring() so bascially now do i just need to make a method which inside it as all of the try and connection details like before it was inclosed in doGet?

eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 11

> ok this:public List getvideo(String p1,

> String p2) {

>ArrayList video = new ArrayList();

> VideoBean v1 = new VideoBean();

> V1.setArtist("some artist"); // hard

> coded

>// set rest later

> video.add(v1);

>

> return video;

>}

> ght after my tostring() so bascially now do i just

> need to make a method which inside it as all of the

> try and connection details like before it was

> inclosed in doGet?

well, first of all, is your JSP working properly? I mean, does it get the data from your bean properly? worry about that before moving on to data access

georgemca at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 12

thats the biggest problem i have! i have hardcoded in some data but then i want to pass that back to the servlet that called this bean but so far i am having no luck passing the data back or if i am passing it back i don't seem to be able to see it! For the moment i am trying to get it to pass back and then display in a terminal window but again i have no joy!

In the servlet i call this bean like so:

MyBean bean = new MyBean(categoryForm, PriceForm);

request.setAttribute("valid", bean);

System.out.println(bean); // this print the params passed in so there def getting passed to the bean right?

List video= MyBean.getVideo(categoryForm, PriceForm);

but i just get a compile error trying it that way:

MySite/Validator.java:56: cannot resolve symbol

symbol : class List

location: class MySite.Validator

List video = MyBean.getVideo(categoryForm, PriceForm);

^

MySite/Validator.java:56: non-static method getVideo(java.lang.String,java.la

ng.String) cannot be referenced from a static context

List video = finderBean.getVideo(categoryForm, PriceForm);

^

2 errors

eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 13

ok, there are a couple of obvious errors there, but there's little point telling you what they are because you won't understand them. you've tried to run before you can walk. step back from this for a bit and do a really basic tutorial, completely. seriously, you've got some glaring gaps in your knowledge of fundamental java, that you need to understand before you go any further. the problems you're having are really really basic, if you can't sort them out, you're not ready to be writing servlets etc anyway. I know this isn't what you want to hear, but if we continue to spoon-feed you like this, you'll need spoon-feeding forever. if you do a basic tutorial, and understand it, you'll be able to fix this problem in about 5 minutes, trust me

I know it's hard to wait, and you want to do this now, but the truth is, you're not ready for something this advanced yet. telling you otherwise would be doing you a dis-service

georgemca at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 14
so if i do a basic tutorial i can get this whole thing working in five minutes? SOMEBODY GIVE ME A LINK LOL!! Thanks for all your help.p.s i probably will understand the errors lol I am good at rading java just not so good at writing it!Message was edited by:
eastendersa at 2007-7-11 23:21:19 > top of Java-index,Java Essentials,New To Java...
# 15

> so if i do a basic tutorial i can get this whole

> thing working in five minutes? SOMEBODY GIVE ME A

> LINK LOL!! Thanks for all your help.

yeh. seriously. this is very very simple stuff, as long as you've covered the basics. don't worry, loads of people get over-excited and dive straight in. sadly, java isn't quite that accommodating :-(

> p.s i probably will understand the errors lol I am

> good at rading java just not so good at writing it!

> Message was edited by:

> eastenders

georgemca at 2007-7-21 19:53:26 > top of Java-index,Java Essentials,New To Java...
# 16
just another thing, when you say its something simple are you refering to the bean? or the validator servlet? or both lol the way i declare the bean is right isn't it? I have done that before i am not so sure about the way i declare the list though.
eastendersa at 2007-7-21 19:53:27 > top of Java-index,Java Essentials,New To Java...
# 17
oook where did those last few messages go lol? guess if you act like idiot they delte them?
eastendersa at 2007-7-21 19:53:27 > top of Java-index,Java Essentials,New To Java...