Use the data source to complete the data connection. You can increase the speed of your program access, when you spoke about struts, you talked about how data sources are configured in struts, which is done in tomcat6.0 today in the data source that is configured, because of the different Tomcat versions, the data sources are configured differently, So at the time of configuration parameters Tomcat official website. Http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html This example uses the case of SQL Server2005: Specific steps: 1. Copy the jar package associated with the data source connection to Tomcat's Lib, ($CATALINA _home/lib/). Sqljdbc.jar Tomcat-dbcp.jar (already included in Tomcat) 2. Find the server configuration file in $catalina_home/conf Server.xml, we recommend that you back up before the change, once the chaos, you can copy back. 3. Find <Host> elements in Server.xml, add context element <context> to <Host>, note that employeeweb is the context name. 4. Add <Resource> elements in <Context> where the Name property is the data source name and the other properties are analyzed by themselves. Save exit after the modification is complete. Unpackwars= "true" autodeploy= "true" Xmlvalidation= "false" Xmlnamespaceaware= "false" > <context path= "/employeeweb" docbase= "employeeweb" debug= "5" reloadable= "true" crosscontext= "true" > <resource name= "jdbc/onlineshopdatasource" auth= "Container" type= "Javax.sql.DataSource" Maxactive= "maxidle=" maxwait= "10000" Username= "sa" password= "123456" Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver" Url= "Jdbc:sqlserver://localhost:1433;databasename=onlineshop"/> </Context> </Host> 5. Open the project Employeeweb and add the configuration in Web.xml: <?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" Xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <servlet> <servlet-name>DepartmentController</servlet-name> <servlet-class>cn.sunfengwei.employee.controller.DepartmentController</servlet-class> </servlet> <servlet> <servlet-name>EmployeeController</servlet-name> <servlet-class>cn.sunfengwei.employee.controller.EmployeeController</servlet-class> </servlet> <servlet-mapping> <servlet-name>DepartmentController</servlet-name> <url-pattern>/department</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>EmployeeController</servlet-name> <url-pattern>/EmployeeController</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <resource-ref> <description>db connection</description> <res-ref-name>jdbc/onlineShopDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> 6. Test: 6.1 In the JSP test, here for the sake of convenience, the use of JSP standard tag library and expression language. <%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%> <%@ taglib uri= "http://java.sun.com/jsp/jstl/sql" prefix= "SQL"%> <%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%> <sql:query var= "rs" datasource= "Jdbc/onlineshopdatasource" > Select Id,name from department </sql:query> <title>db test</title> <body> <select name= "DepartmentID" > <c:foreach var= "Row" items= "${rs.rows}" >
<option value= "${row.id}" >${row.name}</option>
</c:forEach> </select> </body>
6.2 Complete the data source connection in JavaBean, but one thing you should be aware of is that the client cannot run the data source locally because we are configured in Tomcat and run only on the server. Package cn.sunfengwei.employee.database; /** * @author Sun Fengwei e-mail:sunfengweimail@163.com * @version creation time: June one, 2008 6:19:20 PM * @see */ Import java.sql.*; Import Javax.naming.InitialContext; Import javax.naming.NamingException; Import Javax.sql.DataSource; public class Connectdb { private static Connection Conn=null; // Use local Area Connection // public static Connection getconnection () {
String url= "Jdbc:sqlserver://localhost:1433;databasename=onlineshop"; try { Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Conn=drivermanager.getconnection (URL, "sa", "123456"); catch (ClassNotFoundException e) { TODO auto-generated Catch block E.printstacktrace (); catch (SQLException e) { TODO auto-generated Catch block E.printstacktrace (); } Return conn;
} // Connect using a data source // public static Connection Getconnectionbydatasource () throws Exception {
InitialContext CXT; try { CXT = new InitialContext (); if (CXT = = null) { throw new Exception ("Uh oh-no context!"); }
DataSource ds = (DataSource) cxt.lookup ("java:/comp/env/jdbc/onlineshopdatasource"); Conn=ds.getconnection (); if (ds = = null) { throw new Exception ("Data Source not found!"); } catch (Namingexception e) { TODO auto-generated Catch block E.printstacktrace (); } Return conn;
} } 7. In the data Access object DAO, same as the original, this is not repeated here. |