获取数据库的连接代码
public Connection getConnectionDs(){
try {
//初始化上下文
Context ctx = new InitialContext();
DataSource das = (DataSource)(ctx.lookup("java:comp/env/news"));
cont = das.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cont;
}
配置文件 tomcat conf目录下的context
<Resource name = "news"
auth = "Container" type = "javax.sql.DataSource"
maxActive = "100" maxIdle = "30" maxWait = "10000"
username = "root" password = "bdqn"
driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/news"/>