• 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

Why can't I select mysql'data?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone:
I have a javabean that has a executequery method.I use it in jsp file.I use "select * from statement" for query data in the mysql.But I get nothing in browser!The IE is blank!My javabean code is:
/////////////////////////////Mydata.java////////////////////////////////////////////


public class Mydata {
ResultSet rs;
Statement stm;
Connection conn;
String sql="select * from statement";
String connStr="jdbc:mysql://lyo:3306/test";
String uri="org.gjt.mm.mysql.Driver";
public Mydata(){
}
public ResultSet executequery(String sql){
try{
Class.forName(uri).newInstance();
conn=DriverManager.getConnection(connStr,"lyo","qijiashe");
stm=conn.createStatement();
rs=stm.executeQuery(sql);
rs.close();
}catch(Exception e){
System.out.println("Exception:"+e.toString());
}
return rs;
}

/////////////////////////////////main.jsp/////////////////////////////////////////////////
<%
String sql="select * from statement";
ResultSet rs;
rs = lyo.executequery(sql);
String id="";
String title="";
String statement="";
String name="";
String time="";
String fatherid="";
while(rs.next()){
%>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("title")%></td>
<td><%=rs.getString("content")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("time")%></td>
<td><%=rs.getString("fatherid")%></td></tr>
<%
}
rs.close();
Can anyone help me?Where do I mistake? :roll:
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be a stupid question but... Is the "statement" table empty?
If your code (specifically the piece of code which is calling the Statement#executeQuery(String) method) is not getting any exceptions and ResultSet#next() always returns false, that's the only explanation I can think of based on the code snippets provided. Unless you've got some HTML error which makes the table rows invisible?
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I can select all the data use commandline!
mysql>use test;
Database changed;
mysql>select * from statement
mysql>go;
+----+-------+----------------------------------+-------+---------------------+-
---------+
| id | title | content | name | time |
fatherid |
+----+-------+----------------------------------+-------+---------------------+-
---------+
| 1 | hello | The DATABASE created successful? | lyo | 2003-04-25 10:59:56 |
NULL |
| 2 | 中文 | Can you see the chinese | liren | 2003-04-25 11:00:45 |
NULL |
+----+-------+----------------------------------+-------+---------------------+-
---------+
2 rows in set (0.04 sec)
My table is composed of id,title,content,name,time,fatherid,
Why?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. It's not the table.
How about the HTML. I noticed that the code you posted is missing a starting <tr> tag. Could that cause the rows to not display on your browser?
I'd also add the good old System.out.println's all around the code handling the ResultSet... If the while(rs.next()) loop is never executed, you know that the problem is within the Bean or in the SQL statement. If the loop is executed, you know that it's the presentation code that's failing.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you running the database on your PC? If so, I think you should try and change the following from:
String connStr="jdbc:mysql://lyo:3306/test";
To:
String connStr="jdbc:mysql://127.0.0.1:3306/test";
What is lyo? The name of your PC? Also where is the user / password in the line? If you use a password to log into your database, then you need that in your bean too.
Finally, what I would do to debug is add a System.out.println("1") after each line of code(increasing the number by 1 after each line of code). Run it from the command line and see where your output stops, and you will know what line of code is not working.
[ April 25, 2003: Message edited by: Joe Broderick ]
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lasse Koskela and Joe Broderick:
Thank you for helping me.I can't get the data because there are some problem with my javabean.I close the ResultSet before I return the rs.It's my mistake!Thank you!
 
reply
    Bookmark Topic Watch Topic
  • New Topic