javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

How do you redirect from the servlet to the jsp?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
jTextField1.setText(rs.getString(String.valueOf("column name from SQL table")));

That is not going to solve the problem. It is exactly the same with the code aldeene posted.

aldeene - If you haven't solved your problem you can post some more of your code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

ahh.. i see.. still can't do it.

is there anyway that I can use netbeans act like visual studio?,
i mean as easy as visual studio, because I don't think I can display data on textfields directly, unlikely in visual studio

What have you tried? Is jTextField1 static or not. It shouldn't be static. The gui builder has nothing to do with the error. You write the code for executing the query.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Probably because the variable jTextField1 is not static and the method where you put that code is static: This is wrong:

class A {
  private int a = 5; // non static variable

  private static void method() {
     System.out.println(a);
  }
}

The method is static, so you cannot use the non-static variable.
There are 2 good solutions:
Make the method non-static:

class A {
  private int a = 5; // non static variable

  private void method() {
     System.out.println(a);
  }
}

Leave it static and add an argument to the method and then call it with that argument

class A {
  private int a = 5; // non static variable

  private static void print(int i) {
     System.out.println(i);
  }


  // calling the method from somewhere else:
  {
    print(a);

  }
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First, use code tags. Press the button CODE and put your code between the tags.
Also I tried running your code and it didn't work, even though it is correct. It behaves very strange and the debug gives me strange behavior. Finally I found the problem. When you write (me) something a lot of times, when you see it written you assume that it is correct, but in your case it is wrong:

I write:

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

}

You wrote:

for(ctr = 0; ctr < sentence.length(); ctr++);
{

}

Can you spot the difference:

for(ctr = 0; ctr < sentence.length(); ctr++);

We use the ';' to terminate commands. So when you put the ';' at that place you terminated the for loop. So only this got executed N times:

for(ctr = 0; ctr < sentence.length(); ctr++);

And then after ctr changed frm 0 to sentence.length() the following command executed outside the loop:

System.out.println(sentence.charAt(ctr));

So basically when you put that ';' at the end of the for loop you practically wrote something like this:

for(ctr = 0; ctr < sentence.length(); ctr++) {}
{
  System.out.println(sentence.charAt(ctr));
}

The for executes with no body. Then when the ctr is equals to sentence.length() exits the loop and executes one System.out.println(sentence.charAt(ctr)) which gives you an error.

Try running this and see what happens:

int i=0;
for (i=0;i<length;i++);
 System.out.println("i: "+i);

Then write this with no ';'

int i=0;
for …
Ezzaral commented: Helpful explanation. +15
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What do you mean generic? Will the getSt be inherited from the subclasses?
What do you mean: "so I can extend a single copy of this method" ?
You don't extends methods. If getSt is only in the SuperClass then both A,B will have access to the same code.
If the getSt method has different implementation in the A, B classes then there is this code you can use, but again I don't know if it is correct, since you need to post more details:

SuperClass obj_1 = new A();
SuperClass obj_2 = new B();

obj_1.getSt(); // this will call the method from A class
obj_2.getSt(); // this will call the method from B class

According to the above you can do this: It is normally used when the SuperClass is abstract or an interface:

public String methodGeneral(SuperClass sc) {
  return sc.getSt();
}

// ------------------------------------------

A obj_1 = new A();
B obj_2 = new B();

String s1 = methodGeneral(obj_1);
String s2 = methodGeneral(obj_2);

// ------------------------------------------
// OR
SuperClass [] array = SuperClass[2];
array[0] = new A();
array[1] = new B();

// in a loop:
String s1 = methodGeneral(array[0]); // A.getSt
String s2 = methodGeneral(array[1]); // B.getSt

The methodGeneral takes as argument a SuperClass, but if you pass as argument an instance of the A or B class inside the methodGeneral the getSt method of the A or B class will be called.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

For columns, shouldn't you be doing:

for (int j = 0; j < N; j++){
  sum += matrix[j][index];
  }

With N.
When you enter values to the array you do:
>> for (int i = 0; i < N; i++)
>> for (int j = 0; j < M; j++)
>> matrix[j] =

So the N is when you access the first dimension(matrix[][]) and the M when you access the second dimension(matrix[][])


Also I would advise you to:
1)

// we use 0-based arrary types
        Scanner sc = new Scanner(System.in);    
        N = sc.nextInt();
        M = sc.nextInt();
int [][]matrix = new int[N][M];

2) For easier use:

System.out.println("Enter operator:");
operator = sc.next();
System.out.println("Enter Index:");
index = sc.nextInt();
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The '=' is used for assignments. The '==' is used for comparison:

if(Btn2.getSelected() == true){
       
}

Or better if the Btn2.getSelected() returns boolean:

if ( Btn2.getSelected() ){
       
}

Edit: In your code you use Btn2.setSelected() . Perhaps you meant getSelected, because normally with set you pro-grammatically change the value

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You cannot run it because you haven't compiled it:
Uncompilable source code - cannot find symbol. symbol: class ResultsModel

You need to import the ResultsModel or add to the classpath the jar that contains the ResultsModel, then compile and run

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Don't use random methods. This isn't the lottery. Use logic.
Since you are writing jsps, you know how to loop an array:

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

Why you set i to be 0 and i<length ? You pass the counter at the request and you use it as argument at the get method.
The list has elements from 0 till size-1, so the counter must be within that range. So display the next, previous links only when the value at the Counter parameter of the links is within that range

peter_budo commented: One for sticking with this guy, and hard time spoon-feeding :) +16
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Almost. You need to get the logic right. By now you must know that the index that you pass at a list must be between 0 and list.size-1
So you must make sure that when you display the links, what you send is between that range.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
<%
if () {
%>
// next link
<%
}
%>

<%
if () {
%>
// previous link
<%
}
%>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In order to avoid the exception use if statements. If the counter+1 exceeds the size of the list don't display the NEXT.
The same for PREVIOUS. If counter-1 is negative don't display the PREVIOUS

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Assuming that you read the input: 11100000 as a String. You will create an int array with size the length of the input. Check the API of the String class. There are methods that return its length as well as each character of the String (charAt(index))
Loop the input, take each character convert it to a String, then to an int and put it into the array:

String s = "11100000";
for (int i=0;i<s.length();i++) {
  char ch = s.charAt(i);
}

Convert the ch to a String String s2 = ch+""; Then put that s2 into the int array.

Then you can apply the algorithm.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The Person class seems ok. With the following corrections. You need to pass the name at the constructor as well. And you need to add get/set methods:

Example:

public double getHeight() {
  return height;
}

This is wrong:
Person.name
You need to declare an instance of the Person and call the constructor:

String name = scan.next(); // if you use here nextLine it will read the whole line not just the name!
double height = scan.nextDouble();
double weight = scan.nextDouble();

Person per = new Person(name, height, weight);
System.out.println(per.computeBMI());

I don't believe that the computeBMI is correct. You need to find the correct formula.

Also after you get the count, you need to create an array of Person objects, loop it and give each element a value:

int count = scan.nextInt();
scan.nextLine();
Person [] persons = new Person[count];

for (int i=0;i<persons.length;i++) {
  System.out.println("Enter Data "+i);

  String name = scan.next();
  double height = scan.nextDouble();
  double weight = scan.nextDouble();
  scan.nextLine();

  Person per = new Person(name, height, weight);
  persons[i] = per;

  // OR persons[i] = new Person(name, height, weight);
}

After you have the array, you can loop it, take each element: persons, from it take the height and apply any standard algorithm for taking the max/min.
You will use the height of each element for the comparison but you will be saving the whole person object at the temp variables:

Person maxPerson;

Also you will notice that I have added a few extra …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

No you didn't follow my instructions. In my exact previous post I stated the error and told you what you need to use. Descriptions of the logic has been given in another previous post.
The links pass at the request the increased/decreased value of the counter, which you don't use it, as stated many times.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

it shows the in the address bar that Counter = 1 but does not display the next record.

It doesn't display the next record because you don't make it display it, nor you read the next record. Look at the code you have written. You always set the java variable Counter to 0. You never change its value, so of course it always displays the first record. Understand what you are writing:

int Counter = 0;
BookRecordViews Book = (BookRecordViews)GetBookRecord.get(Counter);

Counter is always 0. You never change its value.

Also the links are wrong. You haven't written correctly one of them.
When you submit to the same page, you pass the Counter(address bar) for a reason, but you don't use it when you take it from the request.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From what I understand, the fraSearch is the frame where you have the code tha displays, and when you click the link, you put the emplUpdate in the request and determine if the javascript code will execute or not?

Well for that javascript to execute, you need to have it in a <script></script>
The java code executes and creates the javascript code you wrote. But that doesn't mean that the javascript will execute. It must be in the body and in the script tag.

I am not very familiar with frames so I wouldn't advice you use them.
Usually when you update the database and load the page again, you can read the database and display.
After seeing your code it depends where the code that updates is and then how you execute the page where you have the window.parent.frames("fraSearch").window.location.reload(); Can you post more code that shows how you update, how you display, where those happen and in general your jsps structure?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You will display only one element, so you don't need a loop. Just display only one element from the list. Which one will be determined from the links "NEXT", "BACK".
They will submit to the same page you will get what is in the request (the index(counter) of the element in the list ) and display it.

How you will write those links and how you will get the index from the request it has been described.

If the counter is not in the request (if the request.getParameter returns null) you will display the first element and use that as starting point.
Else just convert what you get from the request to an int and use that as index.

String count = request.getParamer("counter");
int counter = ...

// code that has been described

<a href="ViewBooks.jsp?counter=<%=counter-1%>" >
BACK
</a>

<a href="ViewBooks.jsp?counter=<%=counter+1%>" >
NEXT
</a>

if "count" is null
you will set the int java variable counter to 0,
else you will set it to what is inside the "count" (from the request)

So if next was clicked you have the previous counter+1, if back was clicked you have the previous counter-1.
Get the element, display it, and the rest of the code displays the links with the new values calculated to point to the next, previous element.

Additional description has been given in one of my previous posts.
Also I don't remember if it is against the rules or not, but …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Are you sure that the values are updated to the table. Do you query it and see them updated?
You cannot use javascript to run java code and update the table. Javascript has nothing to do with the java we run here.

If the values are updated, reload the page.

Post code at the JSP forum.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I am interested in a currency converter made from JAVA. I'm a beginner in learning JAVA programming language.

I am thinking well I want the code to go out and get the current value of the US dollar and the current value of the EURO.

Next I want it to convert US dollars into EURO and have the end output to the monitor give us a run down on what was entered for the US and EURO, the total US dollar amount they entered and what it converted too.

I also want to have the software to exist when we enter a dollar amount of $0.

What exactly in your thought process on the PSEUDO CODE as you think it out?
I've told you my thoughts.
What exactly does one need to indicate in a UML Diagram?
And I am stipulating that I am using a PRIMARY aka Main class aka driver class,
and a class to see how these function.


I know I will need a If Then Else loop.

This project already has come and gone in my class, but I am curious what the other programmers are thinking about when the delve into this project. I feel like we all
played blind mans bluff figuring this project out.

Please tell me? Thank you,

Start a new thread.
As a hint, you can have constant variables with the values to convert euros to USD and USD to euros.
Ask …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you were using the Iterator to get each element you were doing this: Book = (BookRecordViews)i.next(); With the way you declare the ArrayList you have it contain Objects. So you need to cast them to BookRecordViews when you use the get method like above.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The declaration is wrong. There is no such thing as int void.
It is either int or void. And in this case it should be int:

public int countTokens(String s)
{
StringTokenizer st= new StringTokenizer();
return st.countTokens();
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You should better remove the initialization of BufferedWriter outside the loop. Why you need it in? Each time you create a new one, you close it then create again a new one?
Put it outside the loop, in the loop call only the write method and close after the loop.
Understand what your code does.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I cant use a radio button, I have to use a checkbox because I have to select multiple products. So when I say I check 4 checkboxes with 4 different products, I would like to get the qty for only those 4 . How can I do that?

In the way I described. Only this time you will use the method that returns an array, you will loop the array in order to have the catNo and use the parametetric request.getParameter in order to get the rest for each row:

String [] catNos = request.getParameterValues("catNo"); // this time catNo is the name of the check box
     
for (int i=0;i<catNos.length;i++) {
  String qty1 = request.getParameter("qty_"+catNos[i]);
  String name1 = request.getParameter("name_"+catNos[i]);
}

Each element of the catNos will have the CATALOGNO of each row selected and in the loop you will get the values of the each of the other elements, provided that you declared the input tags as described.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you get the exception, you also get a message that says where the error happened. Probably at the paint where you call the Integer.parseInt. If what you pass as parameter cannot be converted to an int you get the error. So make sure that inputxend.getText() and the others are numbers before calling repaint()
Inside a try-catch call what you have in the paint method. If no exception happens then you can call repaint.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

So you are saying that when you debug the messages:
Will read file , Done reading file appear
but when you run it, only the first one appears?

I would suggest the following. More debug messages:

File file = new File("projects");
// print the file
// print if the file exists: file.exists()
JOptionPane.showMessageDialog(null, "Will read file", "clear.", WIDTH);
FileReader input = new FileReader(file);
JOptionPane.showMessageDialog(null, "Done reading file", "clear.", WIDTH);

And at the end add another exception:

private void readFileButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        try {
            // ...........
        } catch (ArrayIndexOutOfBoundsException e) {
            /* If no file was passed on the command line, this expception is
            generated. A message indicating how to the class should be
            called is displayed */
            JOptionPane.showMessageDialog(null, "Unable to open Project file. (IOOB)", "Error: Contact Programmer.", );

        } catch (IOException e) {
            // If another exception is generated, print a stack trace
            JOptionPane.showMessageDialog(null, "Unable to open Project file. (IO)", "Error: Contact Programmer.", );
            e.printStackTrace();
        } 
// add this
catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Final error: "+e.getMessage(), "Final Error", );
            e.printStackTrace();
        }
    }

You will also need to check the console or the IDE output when you run it in order to see the printStackTrace or any other messages you print using the System.out.println

JOptionPane.showMessageDialog(null, "Unable to open Project file. (IO)", "Error: Contact Programmer.", WIDTH);

The WIDTH is not correct. Just because it got auto completed from the IDE doesn't mean you should blindly follow it. Check the API of JOptionPane class and check that …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you post the logic beind the if? Maybe you meant to use the >= instead of the ==

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all the Chemical class is not correct.
You need to make it more general. What if you want to create an instance of that object with values from somewhere else. Not a ResultSet.
Add another constructor with no arguments and get/set methods

public float getPrice() {
  return price;
}

public void setPrice(float pr) {
  price = pr;
}

At the toTableString method the input tag is wrong. You need to have quotes at the value attribute like this:

<input type = \"tex\" name = "qty[]" size = \"5\" value = \"1\">

What you do with size and value needs to be done with name as well: name = "qty"
Also:
"<input type = \"checkbox\" name = "chem" value = \""+catNo+"\" >"

Although the above advice will not be used because you will delete the toTableString method. Never write that kind of code.
Put it into a jsp!

<input type = "checkbox" name = "chem" value = "<%=catNo%>" >

See. Much easier without having to worry aboyut escape characters for the ""
If you don't know what this is <%=catNo%> then you need a lot more studying to do.

You will also need to delete the displayHTML. Don't use servlets to display HTML code. Put it in a jsp. It is a lot easier
Also there is no such thing as a type="textBox". Better study some html. Look at this:
http://www.w3schools.com

About your problem. If …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The method must return a list with all the elements. And at the jsp you will display only one. The one selected. Now even if you return the first element how will you get the ith element and how will you know that would be the value of i?


The solution has already been given in previous posts. I will try to give you a more simpler one.


You will have a java variable counter which will tell you which element from the list you will display:

int counter = 0;

// more code here

Book = GetBookRecord.get(counter);
//display only Book. One element.

The counter's value will be taken from the request. In order to do that the links will redirect to the same page. They will have one parameter. The value of the counter that needs to be displayed

<a href="ViewBooks.jsp?counter=<%=counter-1%>" >
BACK
</a>

<a href="ViewBooks.jsp?counter=<%=counter+1%>" >
NEXT
</a>

When those links are pressed you will go to the same page, but this time the counter would be in the request and it will not have the same value. It will be increased or decrased by one so it will point to the next or previous element.

So:
At the begining of the page you will always get the "counter" parameter:
request.getParameter("counter")

If it is null, it means that this is the first time you come to this page so you must display the first element, so you will …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you post the code you have at the jsp page?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know how to run queries with java and how in general to insert data in a database?
There are several ways so can you post the code that use to insert normal data, so we can give you a suggestion similar to your code style?
Do you use PreparedStatements or jua Statements to create your query?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

It is a given that you will not loop this time.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Since you call the GetBookRecord in the jsp then this is the easy, but not so efficient way:

In the jsp have an int variable let's say it counter. At first display the 1st record (counter=0) GetBookRecord.get(counter); Have NEXT, BACK buttons and put the counter value in a hidden field.

Once you click any of the buttons submit to the same page. Then determine which button was clicked. If NEXT was clicked, take the counter from the request, increase its value by one and display that BookView from the list. If BACK was clicked, decrease the counter by one and do the same. If none of the buttons were clicked then that means that you went to page from somewhere else so display the (counter=0)

You can put hidden fields and when you click the button update the field with javascript in order to determine which button was clicked.

<form action="">

-- buttons

<input type="hidden" name="counter" value="<%=counter%>" />
<input type="hidden" name="butPressed" id="butPressed" value="" />
</form>

When you click the buttons, change the value of the butPressed field to whatever you want depending on which button was clicked and then submit the form
Take from the request the butPressed and the counter. If the butPressed is not null, it means that one of the buttons was clicked. Determine which one it was and follow the instructions

int counter = 0;
if (NEXT) {
  counter = <counter from the request> + 1;
} else if (BACK) {
  counter …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can do it with ajax, and you will also need to learn javascript.
It is easier to use the jQuery library to make an ajax call.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Where are the images located? Are they at same folder as the ButtonFrame class file is?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From the screenshot, I assume that you took all the files form somewhere else and put them in eclipse? If that is the case, then I see that you took the .java files and the .class files and put them in same folder src/
I think, without knowing your settings, that since the .class files are in the src/ folder it doesn't find them because you haven't set the classpath to see them.
Even if changing the classpath to look at the src folder might make you project compile it is not the right way. The .class files should be put in a different folder

Also if you have code that works but what you have written that uses that, doesn't compile is probebly because you haven't set the classpath to "see" it.
Go to the Project properties and look at the java build path or search for tutorials about how to set the classpath with eclipse.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Firstly the requirements say that you shouldn't append the results to the new files so don't use the true option:
FileWriter fstream = new FileWriter(filename,true)

Second if you have been taught to use the PrintWriter then use that.

You can use the BufferedReader to read from a file or you can also use the Scanner class. if you pass as argument to its contructor a File object.

As far as your Studnet class: JPleasant_Project#1
You have been told to create a Student class not a JPleasant_Project#1 class.
Also the name,hours,grade need to be the attributes of the class, not local methods inside of a main. Don't put a main inside the Student class. Just the attributes with get/set methods. Surely you have been taught how to create custom classes.

You will use code to write in the new files. That code would be put in the if-else statements and according to each case you will write to the file described by your requirements.
Create different writers outside the loop with the students and use their instances to write to each file.

The code provided by jeffery12109 was just an example. you need to make the right modifications or whatever way you have been told to use

------------------
If your teacher hasn't told you how to create custom classes then things are to become more complicated, because you need in the same main, do the reading and while reading writing …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Okay is there any steps you can give me to work on I know I have to create the candidates.txt, cumlaude.txt, magna.txt, summa.txt, noGrad.txt, hoursError.txt and gpaError.txt. Do I put the if statments above by name into these cumlaude.txt and what more do I need to assign to String name; double hours; double grade;... We get all this from the user so its unknown correct? I just need help. Steps, procedures, anything..

You have already been given the first steps to start at my first post.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Delete the jsp.
Remove the execution of the query from the jsp.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You don't give values to the variables hours, name, grade. Also that is not how if statements work. Review your notes.

if () {

} else if () {

} else if () {

} else {

}

Also you will need to read first the file with the candidates and have a list with students, which you will loop.

You will also need to be more specific with your checks.
For example:
You check if the student will graduate:
(hours>=120 && grade>=2.0)
And the you check if the student is Cum laude
(hours >= 120 && grade >= 3.5 && grade <= 3.7)

But with the way you wrote it:

if (hours>=120 && grade>=2.0) {

} else if (hours >= 120 && grade >= 3.5 && grade <= 3.7) {

}

The code will always be going in the first if and it will never enter the second.
And you cannot use different ifs because then you will save the same data to the same file.
You must put the more specific ifs at the top.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Don't put everything in the main method and don't write everything at once.
First create a Student class with attributes: name, hours, grade.

Then have a single method:
In that method read the file with the students and have the method return a list with the data. The List will have Student objects

First write that method test it and then you can call it, loop the list and have if statements in order to determine which student to write to which new file.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

so text boxes cant give data after space from database i guess

Not true. As the guy in the White House said:

Yes they can!

Use:

<input type="text" value="<%=name%>" name="username">

Instead of:

<input type="text" value=<%=name%> name="username">

If the java variable has value: "hello world" then after the loading of the page the html code rendered would be for the above cases:


Use:

<input type="text" value="hello world" name="username">

Instead of:

<input type="text" value=hello world name="username">

So the second part will display only the "hello":
value=hello world
Because it will assume that the hello is the value and the world is some other attribute the the <input /> tag

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

a user told me "x will never happen" and then it did, after I shipped the code

Same here.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From what I see you know some java. How to run queries. Just put that code to a separate method in another class. You already have written the code. You don't need to write anything else. Just take that code put it in another method and have it return the list with the data. You should know how to create custom objects with attributes. And how to use lists (ArrayList, Vector).


Also there are plenty examples and books to help you. Simply copying some code I might post here without having the background to understand it will not help you learn.

And I cannot become an online tutor. You can easily read any book and then post your questions here.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Run the query (SELECT * FROM log) from a different method. Create a custom object, run the query, put the results inside the object and have that method return a list with the data.
Call that method form the servlet.

The servlet will have only that call. Once you get the list, put it into the request and send it to the page you want it displayed. And I believe that, that is the problem. In the servlet, you don't specify where you want to display the data. You should declare the jsp where the out.print will print.

Also never put html code in a servlet. Send the data to a jsp page and use html tags for displaying.

In general delete everything you have written. Try to find a book about JSPs and learn HTML. Use objects and separate methods.

You must be able to know very good java before going to JSPs. Meaning that should know how to use objects and most important you should close the Connection and everything else in a finally block, which you don't.

public void doGet(HttpServletRequest inRequest,
HttpServletResponse outResponse) throws ServletException,
IOException {

  try {
     ArrayList<CustomObject> list = .... call a method from another class

     // put the list in the request
     // send it to a jsp
  } catch (Exception e) {
     // put the error in the request
     // send it to a jsp
  }

}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

tahnks for u r suggestion,
will try
but i am not getting the data after the sapce in front end though i implemented what u said,
i think there is an error in "<rs.getstring(1)>"
plzzz check in that way

If you implemented what I said then you shouldn't be using this:
<%= rs.getstring(1)%>
Remove all the connection/database code from the jsp. Only call a method from a class that returns a list with the data. Then loop that list and display the data

<%
MyClass cl = new MyClass();
ArrayList<String> list = cl.getUsers(); 

int size = list.size();
for (int i=0;i<size;i++) {
%>

  <form action="detail.jsp" method="post">
   <input type="text" value=[B]......[/B] name="username"></td>
   <input type="submit" value="submit" name="submit">
  </form>

<%
}
%>

Do that, and we will talk what needs to be in the dots. Remember, first write the method. It must be independent. Meaning that you should be able to run it in a main method for example. The jsp part should only call that method. No java code other than what is needed for displaying.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
<FORM ACTION="fetch1.jsp" >

You submit to the fetch1.jsp and you try to get the yearenrol, ... but those fields are not in the above form. The form I have posted only has the fields: username.

Also this is wrong: String section = request.getParameter("section").toString(); You don't need the toString() . The request.getParameter already returns a String. But if no section was submitted then the request.getParameter("section") would be null so you will get a NullPointerException.

So when you submit a form only the fields inside it get sent. So if you submit the
<FORM ACTION="fetch1.jsp" >
Or the
<FORM ACTION="fetch.jsp" >
Make sure to read inside those jsps only the fields that are inside them:

<FORM ACTION="[B]fetch1.jsp[/B]" METHOD="POST" onsubmit="return validateForm(this);">
<tr>
<td><b><font size=5>Id No.</b></font></b></td>
<td>[B]<input type="text" name="username">[/B]</td></tr>
<br><td><button type="submit"><img src="Tick-icon.png" width="30px"></td></br>
</form>

The validateForm function is wrong. You use it at both forms but only one of them has the username: else if(theForm.username.value=="") So if you try to submit the other form it will give you an error. Use 2 different function for each form.

Delete the entire fetch1.jsp . Like I said, put all of your code that connects to the database to a separate method. Pass the appropriate arguments execute the query and have that method return the results. Then call that method from the jsp and display them.
The method should return a list of objects.

------------------------------
Read carefully:
If you want to write that kind of code …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you post the code you used?

Or you can search this forum for examples on how to run queries at a database and get the results.
Remember, put that code in a separate method in a class and call that method from wherever you want. That method should return the data you want to display.

And try to avoid roseindia

kvprajapati commented: +1 Good suggestion. +15
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you post the method and only the method that you use to read the rows from the database?