哪位java大神帮我解释下面一段代码
publicConnectiongetConnection(){try{//读取配置文件,取数据源名称DocumentBuilderFactorydomfac=Docum...
public Connection getConnection() {
try {
//读取配置文件,取数据源名称
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
// File f = new File(this.getClass().getResource("/").getPath());
// f = new File(f.getPath() + "/org/ecms/config/db_config.xml");
// InputStream is = new FileInputStream(f);
InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("org/ecms/config/db_config.xml");
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement();
String resourceName = ((Element)doc.getElementsByTagName("databases").item(0)).getElementsByTagName("resource").item(0).getFirstChild().getNodeValue();
System.out.println(resourceName);
Connection conn = null;
try {
Context initContext = new InitialContext();
//TOMCAT
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup(resourceName);
//WEBSPHERE
// InitialContext ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup(resourceName);
conn = ds.getConnection();
conn.setAutoCommit(false);
initContext.close();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败");
// TODO: handle exception
}
return conn;
} 展开
try {
//读取配置文件,取数据源名称
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
// File f = new File(this.getClass().getResource("/").getPath());
// f = new File(f.getPath() + "/org/ecms/config/db_config.xml");
// InputStream is = new FileInputStream(f);
InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("org/ecms/config/db_config.xml");
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement();
String resourceName = ((Element)doc.getElementsByTagName("databases").item(0)).getElementsByTagName("resource").item(0).getFirstChild().getNodeValue();
System.out.println(resourceName);
Connection conn = null;
try {
Context initContext = new InitialContext();
//TOMCAT
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup(resourceName);
//WEBSPHERE
// InitialContext ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup(resourceName);
conn = ds.getConnection();
conn.setAutoCommit(false);
initContext.close();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败");
// TODO: handle exception
}
return conn;
} 展开
1个回答
展开全部
public Connection getConnection() {
try {
//读取配置文件,取数据源名称
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
// File f = new File(this.getClass().getResource("/").getPath());
// f = new File(f.getPath() + "/org/ecms/config/db_config.xml");
// InputStream is = new FileInputStream(f);
InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("org/ecms/config/db_config.xml"); //读取org/ecms/config/目录下的db_config.xml文件
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement();//获取XML下的所有节点
String resourceName = ((Element)doc.getElementsByTagName("databases").item(0)).getElementsByTagName("resource").item(0).getFirstChild().getNodeValue();
System.out.println(resourceName);
Connection conn = null;
try {
Context initContext = new InitialContext();
//TOMCAT
Context envContext = (Context)initContext.lookup("java:/comp/env");
//假如你需要获取datasource,例如:dataSource = (DataSource) context.lookup("java:comp/env/jdbc/demoDS");
//那么在配置文件中进行资源映射时,在web.xml中,
// <resource-ref>
// <res-ref-name>jdbc/demoDS</res-ref-name>
//<res-type>javax.sql.DataSource</res-type>
//<res-auth>Container</res-auth>
//</resource-ref>
DataSource ds = (DataSource)envContext.lookup(resourceName);
//WEBSPHERE
// InitialContext ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup(resourceName);
conn = ds.getConnection();//得到数据库链接
conn.setAutoCommit(false);//设置执行SQL手动提交
initContext.close();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败");
// TODO: handle exception
}
return conn;//返回拿到的数据库链接
}
try {
//读取配置文件,取数据源名称
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
// File f = new File(this.getClass().getResource("/").getPath());
// f = new File(f.getPath() + "/org/ecms/config/db_config.xml");
// InputStream is = new FileInputStream(f);
InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("org/ecms/config/db_config.xml"); //读取org/ecms/config/目录下的db_config.xml文件
Document doc = dombuilder.parse(is);
Element root = doc.getDocumentElement();//获取XML下的所有节点
String resourceName = ((Element)doc.getElementsByTagName("databases").item(0)).getElementsByTagName("resource").item(0).getFirstChild().getNodeValue();
System.out.println(resourceName);
Connection conn = null;
try {
Context initContext = new InitialContext();
//TOMCAT
Context envContext = (Context)initContext.lookup("java:/comp/env");
//假如你需要获取datasource,例如:dataSource = (DataSource) context.lookup("java:comp/env/jdbc/demoDS");
//那么在配置文件中进行资源映射时,在web.xml中,
// <resource-ref>
// <res-ref-name>jdbc/demoDS</res-ref-name>
//<res-type>javax.sql.DataSource</res-type>
//<res-auth>Container</res-auth>
//</resource-ref>
DataSource ds = (DataSource)envContext.lookup(resourceName);
//WEBSPHERE
// InitialContext ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup(resourceName);
conn = ds.getConnection();//得到数据库链接
conn.setAutoCommit(false);//设置执行SQL手动提交
initContext.close();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败");
// TODO: handle exception
}
return conn;//返回拿到的数据库链接
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |