一、創建鏈接池:在\tomcat6\conf中的context.xml文件中的<context>中添加下面語句
oracle:
<Resource name="jdbc/oracleds"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="scott"
password="yanyan"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
mysql: <Resource name="jdbc/mysqlds" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="scott" password="yanyan" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1/orcl"/> 2,把數據庫的驅動類庫添加到tomcat中的類庫中 三、操做:以下: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");//解決中文亂碼 String title = request.getParameter("title"); String content = request.getParameter("content"); String categoryID = request.getParameter("category"); DataSource ds = null; try { // 經過在context.xml文件,設定的數據源對象的名字,獲取數據源對象 Context context = new InitialContext(); ds = (DataSource) context.lookup("java:/comp/env/jdbc/oracleds"); } catch (Exception e) { System.out.println("獲取數據源時出錯"); } int result = 0; try { Connection cn=ds.getConnection(); // 添加博文的SQL語句,now()生成當前系統時間 String sql = "insert into blog(id,title,content,catagory_id,created_time) values (B_SID.NEXTVAL,'錦衣衛','錦衣衛',2,sysdate)"; //如今我還不知道怎麼用序列實現表序號自動添加 // 爲SQL語句中的?設定參數 PreparedStatement psmt=cn.prepareStatement(sql); psmt.setString(1, title); psmt.setString(2, content); psmt.setInt(3,Integer.parseInt(categoryID)); result=psmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } String message = ""; if (result == 1) { message = "添加博文成功!"; } else { message = "添加博文失敗!"; } System.out.print(message); }