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文件的配置:
访问网页:
连接数据库成功: