---
title: "Tomcat6 数据连接池配置"
author: "ldsjdy"
date: "2011-04-20T10:50:12Z"
tags: ["JDBC", "Java", "SQL", "Servlet", "XML"]
categories: ["企业架构"]
slug: "tomcat6"
draft: false
lastmod: "2011-04-20T10:50:12Z"
id: 1008947
url: "https://webqq.iteye.com/blog/1008947"
---

1.在context.xml中配置数据连接源，并将数据库驱动拷贝到lib目录 \[code="java"] \[/code] 2.在Servlet中通过InitialContext lookup到DataSource进行数据库操作 \[code="java"] protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doget........"); try { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Datasource"); Connection conn = ds.getConnection(); System.out.println("connection: " +conn); String userName=request.getParameter("userName"); String password = request.getParameter("password"); String sql = "insert into user(name,password) value('"+userName+"','"+password+"')"; System.out.println(sql); Statement st = conn.createStatement(); st.execute(sql); conn.close(); } catch (SQLException ex) { Logger.getLogger(JDBCTest.class.getName()).log(Level.SEVERE, null, ex); } catch (NamingException ex) { Logger.getLogger(JDBCTest.class.getName()).log(Level.SEVERE, null, ex); } } \[/code]