Access to a database with Struts source
Abstract: Access to a database with Struts source
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
First, preparation work
Create a database in access eBookStore.mdb and table book (download from the Site), and in the windows control panel set up good data source page
<table Width="681" height="24" border="0"> <tr> <td width="412"> at eBookStore.mdb. Three other needs jar documents:
1, commons-dbcp-1.2.1.jar
Download Address: http://apache.linuxforum.net/dist/jakarta/commons/dbcp/binaries/
2, struts-legacy.jar
Download Address: http://apache.linuxforum.net/dist/jakarta/struts/struts-legacy/
3, commons-pool-1.2.jar
Download Address: http://apache.linuxforum.net/dist/jakarta/commons/pool/binaries/
Second, struts-config.xml configuration
<? Xml version = "1.0" encoding = "ISO-8859-1"> </ td> <td width="259"> </ td> </ tr> </ table>
Struts-config PUBLIC
"- / / Apache Software Foundation / / DTD Struts Configuration 1.2 / / EN"
"Http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<form-beans>
</ Form-beans>
3. Action categories: DataSourceAction.java
Package page;
Import org.apache.struts.action .*;
Import javax.servlet.http .*;
Import java.util .*;
Import javax.sql .*;
Import java.sql .*;
Public class DataSourceAction extends Action (
Public DataSourceAction () ()
Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception (
Try (
This.getDataSource DataSource ds = (request, "dataSource");
Connection con = ds.getConnection ();
Statement stmt = con.createStatement ();
String sql = "SELECT * FROM book";
ResultSet rs = stmt.executeQuery (sql);
Request.setAttribute ( "result", rs);
) Catch (Exception e) (
E.printStackTrace ();
System.out.println ( "Database Connection unusual");
)
Return (mapping.findForward ( "success"));
)
)
Fourth, the test page: testpage.jsp
<% @ Page contentType = "text / html; charset = gb2312" language = "java"%>
<% @ Page import = "java.sql .*"%>
<%
ResultSet rs = (ResultSet) request.getAttribute ( "result");
While (rs.next ()) (
Out.println (rs.getString ( "name ")+"—–");
Out.println (rs.getString ( "author") + "
");
)
%>
OK!!
5. Test:
Test Site downloaded from the directory structure TestPage Add tomcat the webapps directory in the browser, type:
Http://127.0.0.1:8080/TestPage/page.do
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back
Tags: java access, out source, source, struts






