1.将数据库的jar包放到工程的lib下
2.在tomcat的/conf/context.xml文件中添加(一些参数需要修改)
<Resource name="jdbc/oracle" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
username="dtm" password="dtm" maxActive="50" maxIdle="20" maxWait="10000" />
3.测试数据库连接代码
public class DBConn {
private static Connection conn;
public static Connection getConn(String name){
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/oracle");
conn = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
PS:红色字体部分数据库名称要相同一致