• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Updated: "Invalid character constant", Help needed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen several examples how how to pull columns and rows using Resultset but I'm trying to sum a single column and cannot figure out the last part where I display the result of the sum.

Here is the part of my code that executes the query:



Then further down in the code I want to pull that result and place it where the following placeholder is <%***%>



I've tried several things and I am getting no where. Any help is greatly appreciated.


Updated: Changed my code and have a new issue

John Piper wrote:I've changed my code quite a bit and am now getting the following error in NetBeans:

unclosed character literal

not a statement

unclosed character literal



And this message on Apache when trying to compile:


org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 26 in the jsp file: /categories.jsp
Invalid character constant
23: <body>
24: <table border='5'>
25: <jsp:useBean id="StockBean" class="HWpackage.StockBean" scope="request">
26: <jsp:setProperty name="StockBean" property="totalcost" value="<%=request.getAttribute('StockBean')%>" />
27: </jsp:useBean>
28: <tr>
29: <th>Welcome <%UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser"));%><%= currentUser.getUsername()%></th>



I'm officially about to start pulling out my hair.

I know it has to do with this part on line 26:



I did have it like this originally and NetBeans liked it but Apache did not:



I figured out it had to do with the double quotes within double quotes which as to do with new vs old way of coding on Apache. But I just can't figure out how to correct it in a way that will allow the code to compile.

Thanks for the help everyone.




 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far you have the ResultSet. It has one row and one column which contains the number you wanted. So the missing code is the code which would read that row and extract that column.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ResultSet has two ways of retrieving data from the current record - using the column name and using the column index. Since you haven't given your sum column a name (using the "AS" keyword) that leaves you the column index to retrieve the value.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to write resultSet.get(1) or change the quey like

String QueryString = "SELECT sum(cost) as sum from stocks where username = 'currentUser.getUsername()'";

ResultSet costtotal = statement.executeQuery(QueryString);

Iterate the result set and get below value.
double x = costtoal.get("sum");

Hope this will solve your issue.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost right - there is no get(int) method in ResultSet. One of the type-specific methods is needed, like getString(int). That's not the right one though, because the result of the sum is not a String.
 
John Piper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've changed my code quite a bit and am now getting the following error in NetBeans:

unclosed character literal

not a statement

unclosed character literal



And this message on Apache when trying to compile:


org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 26 in the jsp file: /categories.jsp
Invalid character constant
23: <body>
24: <table border='5'>
25: <jsp:useBean id="StockBean" class="HWpackage.StockBean" scope="request">
26: <jsp:setProperty name="StockBean" property="totalcost" value="<%=request.getAttribute('StockBean')%>" />
27: </jsp:useBean>
28: <tr>
29: <th>Welcome <%UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser"));%><%= currentUser.getUsername()%></th>



I'm officially about to start pulling out my hair.

I know it has to do with this part on line 26:



I did have it like this originally and NetBeans liked it but Apache did not:



I figured out it had to do with the double quotes within double quotes which as to do with new vs old way of coding on Apache. But I just can't figure out how to correct it in a way that will allow the code to compile.

Thanks for the help everyone.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Piper wrote:And this message on Apache when trying to compile:


I'm sure that you mean Tomcat. When people use just "Apache" it means the Apache Web Server, which has nothing to do with Java or JSP.
 
John Piper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

John Piper wrote:And this message on Apache when trying to compile:


I'm sure that you mean Tomcat. When people use just "Apache" it means the Apache Web Server, which has nothing to do with Java or JSP.



Correct, Apache Tomcat. or just Tomcat.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic