andrew79
asked on
Using EL to convert string to float or integer
Hie guys,
I'm trying to write a jsp using JSTL and EL to update a Pointbase database but I'm having difficulties to update the database since I'm not really sure how to convert the string data obtain from HTML to FLOAT so that I can update my database. Anyone with help ?
Error message :-
javax.servlet.ServletExcep tion: insert into Video values('jstl','test','2333 ','23.34', '23'): Illegal cast operation between type: CHARACTER and type: REAL.
Source code :-
<%-- taglib directive --%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix ="fmt" %>
<html xmlns = "http://www.w3.org/1999/xhtml">
<sql:setDataSource dataSource="jdbc/myDB"/>
<sql:query var="result" sql="SELECT * FROM VIDEO"/>
<head>
<title>Video List</title>
</head>
<body>
<c:set var='titleIns' value='${param.title}' />
<c:set var='typeIns' value='${param.type}' />
<c:set var='yearIns' value='${param.year}' />
<c:set var='priceIns' value='${param.price}' />
<c:set var='quantityIns' value='${param.quantity}' />
<%-- is there a way to convert price to float type and convert quantity to int type ? --%>
<sql:update var="query1" dataSource="jdbc/myDB"
sql="insert into Video values('${titleIns}','${ty peIns}','$ {yearIns}' ,'${priceI ns}','${qu antityIns} ')" >
</sql:update>
<p style = "font-size: 2em">Video List</p>
<table width="100%" border="1">
<tr align="left">
<th>Title</th><th>Type</th ><th>Year< /th><th>Pr ice</th><t h>Quantity </th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.title}"/></td >
<td><c:out value="${row.type}"/></td>
<td><c:out value="${row.year}"/></td>
<td><c:out value="${row.price}"/></td >
<td><c:out value="${row.quantity}"/>< /td>
</tr>
</c:forEach>
</table>
</body>
</html>
I'm trying to write a jsp using JSTL and EL to update a Pointbase database but I'm having difficulties to update the database since I'm not really sure how to convert the string data obtain from HTML to FLOAT so that I can update my database. Anyone with help ?
Error message :-
javax.servlet.ServletExcep
Source code :-
<%-- taglib directive --%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix ="fmt" %>
<html xmlns = "http://www.w3.org/1999/xhtml">
<sql:setDataSource dataSource="jdbc/myDB"/>
<sql:query var="result" sql="SELECT * FROM VIDEO"/>
<head>
<title>Video List</title>
</head>
<body>
<c:set var='titleIns' value='${param.title}' />
<c:set var='typeIns' value='${param.type}' />
<c:set var='yearIns' value='${param.year}' />
<c:set var='priceIns' value='${param.price}' />
<c:set var='quantityIns' value='${param.quantity}' />
<%-- is there a way to convert price to float type and convert quantity to int type ? --%>
<sql:update var="query1" dataSource="jdbc/myDB"
sql="insert into Video values('${titleIns}','${ty
</sql:update>
<p style = "font-size: 2em">Video List</p>
<table width="100%" border="1">
<tr align="left">
<th>Title</th><th>Type</th
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.title}"/></td
<td><c:out value="${row.type}"/></td>
<td><c:out value="${row.year}"/></td>
<td><c:out value="${row.price}"/></td
<td><c:out value="${row.quantity}"/><
</tr>
</c:forEach>
</table>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For example if someone enter Three hundred for quantity instead of 300 or a person enters Two Hundred Dollars instead of 200.00
I'm increasing the points for the answer. Should have ask earlier ...sorry
I'm increasing the points for the answer. Should have ask earlier ...sorry
ASKER
Thanks bloodredsun
Managed to catch the error but couldn't figured out how to use it to print a stack trace (if its possible doing so using JSTL , but I remembered reading somewhere that JSTL catch is simplified )
Managed to catch the error but couldn't figured out how to use it to print a stack trace (if its possible doing so using JSTL , but I remembered reading somewhere that JSTL catch is simplified )
To catch a stacktrace and print the exception
<c:catch var="ex">
//code
</c:catch>
<c:if test="${not empty ex}">
<c:out value="${ex.message}"/>
</c:if>
<c:catch var="ex">
//code
</c:catch>
<c:if test="${not empty ex}">
<c:out value="${ex.message}"/>
</c:if>
ASKER
is there anyother way of to achive this ? I thought there is a way of using param type ?
I was also hoping to convert it to int / float so that I can use it to catch error exception