You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Lorenzo Sicilia <ar...@kemen.it> on 2004/08/16 15:09:25 UTC

jstl & BINARY field with mysql

Hi to all,

today is my tests day :)

IN this page I make a query to mysql where the field "nick_name" is BINARY.
With jstl I get this output:
[B@19e681c
[B@c50e46
[B@12c4b66

otherwise with jsp  I get this:
admin
arnor
nobody

My sql table:
CREATE TABLE `user_comunity` (
   `id_user` int(11) unsigned NOT NULL auto_increment,
   `nick_name` varchar(20) binary NOT NULL default '',
   PRIMARY KEY  (`id_user`),
   UNIQUE KEY `nick_name` (`nick_name`)
) TYPE=MyIsam;

Follow the example page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page language="java"  import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.DataSource" %>
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld" %>
<%@ taglib prefix="sql" uri="/WEB-INF/tld/sql.tld" %>

<sql:query var="myQuery">
	SELECT * FROM user_comunity
</sql:query>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>sql BINARY field wrong output</title>
</head>
<body>
	<!-- jstl output -->
	<c:forEach items="${myQuery.rows}" var="row">
		<c:out value="${row.nick_name}"/><br />
	</c:forEach>
	<!-- jsp output -->

<%
	// init connection db
	DataSource ds;
	ResultSet rs = null;
	Statement stmt = null;
	String sql = "";
	// search datasource
	 try {
		Context context = new InitialContext();
  		ds = (DataSource) context.lookup("ds_croxit");
	 } catch (NamingException e) {
  		ds = null;
	 }

	Connection conn = ds.getConnection();
	ResultSet m_rs = null;
	PreparedStatement m_stmt = null;

	try{
		// query
		m_stmt = conn.prepareStatement("SELECT nick_name FROM user_comunity");
		m_rs = m_stmt.executeQuery();
		// print result set
		while (m_rs.next() ) {
		// Retrieve the BINARY field
		String fieldValue = m_rs.getString("nick_name");
		out.println(fieldValue +"<br/>");
	}
	// Close
	m_rs.close();
	m_stmt.close();
	conn.close();
	}catch (Exception ex){
	response.getWriter().println(ex.toString());
	ex.printStackTrace();
	}
%>
	</body>
</html>

I use jrun4, jstl 1.0.5, mysql 4.0.x and sdk SunJava2-1.4.2

Can you confirm that?

regards Lorenzo Sicilia

--
KEMEN srl
http://www.kemen.it

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: jstl & BINARY field with mysql

Posted by Lorenzo Sicilia <ar...@kemen.it>.
Kris Schneider wrote:
> The major difference between what JSTL does to populate the rows of the
> "myQuery" variable and your scriptlet code is that JSTL uses
> ResultSet.getObject while your scriptlet code uses ResultSet.getString. 
Thanks Kris for the answer.
I understand  better. I change my query,now:
SELECT CONVERT( nick_name, CHAR ) as nick_name FROM `user_comunity`

The jstl output it is right :)

regards Lorenzo


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: jstl & BINARY field with mysql

Posted by Kris Schneider <kr...@dotech.com>.
The major difference between what JSTL does to populate the rows of the
"myQuery" variable and your scriptlet code is that JSTL uses
ResultSet.getObject while your scriptlet code uses ResultSet.getString. So, if
you change this:

String fieldValue = m_rs.getString("nick_name");
out.println(fieldValue +"<br/>");

to this:

Object fieldValue = m_rs.getObject("nick_name");
out.println(fieldValue +"<br/>");

You should see the same output.

Quoting Lorenzo Sicilia <ar...@kemen.it>:

> Hi to all,
> 
> today is my tests day :)
> 
> IN this page I make a query to mysql where the field "nick_name" is BINARY.
> With jstl I get this output:
> [B@19e681c
> [B@c50e46
> [B@12c4b66
> 
> otherwise with jsp  I get this:
> admin
> arnor
> nobody
> 
> My sql table:
> CREATE TABLE `user_comunity` (
>    `id_user` int(11) unsigned NOT NULL auto_increment,
>    `nick_name` varchar(20) binary NOT NULL default '',
>    PRIMARY KEY  (`id_user`),
>    UNIQUE KEY `nick_name` (`nick_name`)
> ) TYPE=MyIsam;
> 
> Follow the example page.
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <%@ page language="java"  import="java.sql.*" %>
> <%@ page import="javax.naming.*" %>
> <%@ page import="javax.sql.DataSource" %>
> <%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld" %>
> <%@ taglib prefix="sql" uri="/WEB-INF/tld/sql.tld" %>
> 
> <sql:query var="myQuery">
> 	SELECT * FROM user_comunity
> </sql:query>
> 
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> 	<title>sql BINARY field wrong output</title>
> </head>
> <body>
> 	<!-- jstl output -->
> 	<c:forEach items="${myQuery.rows}" var="row">
> 		<c:out value="${row.nick_name}"/><br />
> 	</c:forEach>
> 	<!-- jsp output -->
> 
> <%
> 	// init connection db
> 	DataSource ds;
> 	ResultSet rs = null;
> 	Statement stmt = null;
> 	String sql = "";
> 	// search datasource
> 	 try {
> 		Context context = new InitialContext();
>   		ds = (DataSource) context.lookup("ds_croxit");
> 	 } catch (NamingException e) {
>   		ds = null;
> 	 }
> 
> 	Connection conn = ds.getConnection();
> 	ResultSet m_rs = null;
> 	PreparedStatement m_stmt = null;
> 
> 	try{
> 		// query
> 		m_stmt = conn.prepareStatement("SELECT nick_name FROM user_comunity");
> 		m_rs = m_stmt.executeQuery();
> 		// print result set
> 		while (m_rs.next() ) {
> 		// Retrieve the BINARY field
> 		String fieldValue = m_rs.getString("nick_name");
> 		out.println(fieldValue +"<br/>");
> 	}
> 	// Close
> 	m_rs.close();
> 	m_stmt.close();
> 	conn.close();
> 	}catch (Exception ex){
> 	response.getWriter().println(ex.toString());
> 	ex.printStackTrace();
> 	}
> %>
> 	</body>
> </html>
> 
> I use jrun4, jstl 1.0.5, mysql 4.0.x and sdk SunJava2-1.4.2
> 
> Can you confirm that?
> 
> regards Lorenzo Sicilia
> 
> --
> KEMEN srl
> http://www.kemen.it

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org