Jsp+beans Implementation example of reading a database

Source: Internet
Author: User
Tags connect stmt
js| Data | database (reprint please indicate the source)
From:http://aspfans.yeah.net
Java beans that require two programs, JSP files, and database operations

select.jsp

<title>select everything from a database</title>
<body>

<jsp:usebean id= "Select" class= "Databaseselect" scope= "Request" >
</bean>

<% Out.print (Select.connect ()); %>


<% Out.print (Select.select ()); %>

<p>format Results



<%@ page import= "Java.util.Vector"%>
<% Vector Aresult = Select.getresult (); %>

<table>
<% for (int i=0 i < aresult.size (); i++) {%>
<tr>
<td>
<% Out.print (Aresult.elementat (i)); %>
</td>
</tr>
<%}%>
</table>

</body>

Databaseselect.java
Import java.sql.*;
Import Java.util.Vector;
public class Databaseselect {

Private Vector result;

Public Databaseselect () {
result = new Vector ();
}//Constructor Databaseselect

Public String Connect () {
try {
Class.forName ("a JDBC Driver"). newinstance ();
Return "Driver loaded!";
catch (Exception E) {
Return "Unable to load driver."
}
}

Public String Select () {
try {
Connection C = drivermanager.getconnection ("Database Connection string");

Statement Stmt = C.createstatement ();

ResultSet myresult = Stmt.executequery ("Select Acolumn from atable");

while (Myresult.next ()) {
Result.addelement (myresult.getstring (1));
}

Clean up
Myresult.close ();
Stmt.close ();
C.close ();
Return "Connection success!";
catch (SQLException E) {
Return "SQLException:" + e.getmessage ();
}
}

/**
* Accessor for result
**/
Public Vector GetResult () {
return result;
}

/**
* Mutator for result
**/
public void Setresult (Vector avector) {
result = Avector;
}

}//Class Databaseselect

(reprint please indicate the source)
From:http://aspfans.yeah.net


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.