配置Tomcat的连接池和数据源

  在Tomcat的context.xml中修改:如下

 

<Context reloadable="true">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	<Resource name="jdbc/oracleds" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
	username="scott" password="tiger" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:ora92"/>




</Context>

  <Resource...的为添加内容:

  在程序中使用数据源连接数据库:

 

try {
		Context context = new InitialContext();
		DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
		Connection conn = ds.getConnection();
		
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, "test");		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		pstmt.setString(2,sdf.format(new Date()));
		result = pstmt.executeUpdate();
		
		conn.close();
		pstmt.close();
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}

你可能感兴趣的:(oracle,sql,tomcat,Web,jdbc)