web开发项目连接访问数据库
Posted ztt_tttt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web开发项目连接访问数据库相关的知识,希望对你有一定的参考价值。
1 package com.jr.servlet.check; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 13 14 public class DBTestServlet extends HttpServlet { 15 16 private static final long serialVersionUID = 1L; 17 18 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 doPost(request, response); 20 } 21 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 try { 24 Class.forName("oracle.jdbc.driver.OracleDriver"); 25 Connection con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","neworcal","tiger"); 26 if(con==null){ 27 System.out.println("连接失败"); 28 }else{ 29 System.out.println("连接成功"); 30 } 31 32 } catch (Exception e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 } 37 38 }
xml文件的配置:
访问网页:
连接数据库成功:
以上是关于web开发项目连接访问数据库的主要内容,如果未能解决你的问题,请参考以下文章
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途