tomcat6.0 + myeclipse6.5 under tomcat data source configuration

(1) Environment: such as a very important question! Different environment, different configurations!

(2) struts package loaded: My MyEclipse6.5 bring him under the load of the struts jar package Error occurred "collections can not find under quickHashMap" abnormal (perhaps meaning, forget the record), then bring him to remove the STRUTS JAR, together with an independent Jar, and will all have to copy added to STRUTS jar works in WEB-INF/lib.

(3) the database connection JAR file COPY separately to the project under WEB-INF/lib and under lib under tomcat.

(4) in the tomcat's conf / under Context.xml changes at one of the <Context> </ Context> add the following code (according to their own situation to be slightly amended):

Wrote

<Resource name = "sdemo" auth = "Container" type = "javax.sql.DataSource"
maxActive = "100" maxIdle = "30" maxWait = "10000"
username = "sa" password = "xxxxxx" driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url = "jdbc: sqlserver: / / localhost: 1433; databaseName = afdDemo" />

(5) In the works cited in web.xml:

<!-- A reference to the data source -->
	<resource-ref>
       <res-ref-name>sdemo</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>


(6) in the projects to use the DBCP had:)

 
public static Connection getConnection(){
		Connection conn = null;
		try {
			Context initContext = new InitialContext();
			 // Context envContext  = (Context)initContext.lookup("java:/comp/env");      
			  DataSource ds = (DataSource)initContext.lookup("java:/comp/env/sdemo"); 
			
			 conn = ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return conn;
	}

(7) at dao class can be directly connected to the call:)

public List<User> selectAll(){
		List<User> list = new ArrayList<User>();
		Connection conn = DBUtil.getConnection();
		if(conn == null){
			System.out.println("conn is null!");
		}
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select * from [User]");
			rs = pstmt.executeQuery();
			User user = new User();
			while(rs.next()){
				user.setUserID(rs.getInt(1));
				user.setUserName(rs.getString(2));
				System.out.println(user.getUserName());
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(rs != null){
					rs.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(conn != null){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of tomcat6.0 + myeclipse6.5 under tomcat data source configuration

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate to use the principle of

    The use of hibernate, implementation of data persistence. Has the following several processes. One configuration database connection information. Hibernate.config 2 configuration mapping. 3 use: the use of the process are the following steps: 3.1: Ge ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • spring struts2.0 hibernate environmental structures .. despair carried out more than one hour only with good.

    http://www.qqread.com/java/2008/06/f413762.html Look here. . Note added myeclipse support spring when necessary add the commons-dbcp database connection pool package. And to add hibernate support. . Finally add struts2 support. . Oh the lazy point. . . fu

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries