In fact, I am a newbie, But I have spent a lot of time solving this problem and made a lot of detours, so I don't want everyone to be like me.
In general, in order to facilitate development and code reuse, the Code connecting to the database in JSP is put in a bean separately (at least I did this ), just as ASP has an ASP file dedicated for database connection.
The following are my database query functions:
Public resultset dbquery (string strsql ){
Rs = NULL;
Try {
Conn = drivermanager. getconnection (dbconn );
Statement stmt = conn. createstatement ();
Rs1_stmt.exe cutequery (strsql );
}
Catch (sqlexception e ){
System. Err. println ("can not open the DB! /N ");
System. Err. println (E. getmessage ());
}
Return Rs;
}
If you are familiar with me before, you may have to pay attention to it.
Before I found this problem, the query function I wrote does not return a value, but will always be used directly. I have made a very big and ridiculous mistake: The returned result is a null !!
The reason should be the local variable. Well, it's really a hit. The following is the correct code application:
<JSP: usebean id = "DB" class = "dingsea. dboperat" Scope = "page"/>
<%
Resultset rs = dB. dbquery ("select * from Man ");
Try {
While (Rs. Next ()){
Out. println (Rs. getstring ("ID") + ":");
Out. println (Rs. getstring ("name") + "| ");
Out. println (Rs. getstring ("tel") + "|" + "<br> ");
}
}
Catch (exception e ){
Out. println (E. getmessage () + "/N ");
}
Rs. Close ();
%>