JSP database connection: Notes for beginners

Source: Internet
Author: User

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 ();
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.