• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updated: "Invalid character constant", Help needed

 
John Piper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.



 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic