Jump to content

retrieving SQL result with javascript


Nervosa

Recommended Posts

Hello!

 

I've been having a lot of trouble getting the results back from my database using javascript.

Basically my assignment was to store grades of a classmember - where a classmember is a certain ID - and get the average grade number back on the screen in a webbrowser - of a row.

It should be working I'm just having trouble putting it out on the browser.

public void Average(int Id, PrintWriter out)    {        String query ="select avg( math + phy + litr + chem + biolo)/5.0 from grades where Id=?";        try{            PreparedStatement preparedStmt = conn.prepareStatement(query);            ResultSet rs = preparedStmt.executeQuery();                     while (rs.next()) {                        String  = Float.toString(rs.getFloat(1));                        System.out.println(resultofgrades);                        out.print("Average of the student's grade" + resultofgrades);                            			}                          }catch(Exception se)        {         se.printStackTrace();        }            }
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html;charset=UTF-8");        PrintWriter out = response.getWriter();        String str=request.getParameter("Id");        SQL2 s=new SQL2();        s.Average(Integer.parseInt(str),out);        s.Close();    }

I think this is all the relevant information to the question, if you need the entire code I can pastebin it but I do not think it's needed.

 

I could make the SQL store the data I put in and print it on the browser.

I checked my avg() in phpMyAdmin and it returns the correct value in there but not on my webbrowser screen, its blank.

My guess what's wrong would be line 69:

 

String = Float.toString(rs.getFloat(1));

But unfortunately I cannot seem to fix it.

 

 

Hope you can help me out with this one:

-nerv

Edited by Nervosa
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...