aspose file tools*
The moose likes JSP and the fly likes   Why can't I select mysql'data?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "  Why can Watch "  Why can New topic
Author

Why can't I select mysql'data?

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
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:
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
Joe Broderick
Greenhorn

Joined: Apr 23, 2003
Posts: 23
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

Joined: Feb 17, 2003
Posts: 152
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't I select mysql'data?