condb.java
来自「一个简单的班费管理系统」· Java 代码 · 共 71 行
JAVA
71 行
/*
* condb.java
*
* Created on 2007年12月8日, 上午9:44
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.classmoney.servlet;
/**
*
* @author Administrator
*/
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class condb {
//数据库连接文件
public static synchronized Connection getConnection() {
try {
Context envCtx = new InitialContext(); //创建上下文
DataSource ds = (DataSource) envCtx.lookup("java:/MysqlDs"); //查找数据源,数据源文件在jboo服务器下:jboss\server\default\deploy\mysql-ds.xml
return ds.getConnection();//返回 连接
} catch (SQLException e) {
System.out.println("数据源配置发生错误" + e.toString());
return null;
} catch (NamingException e2) {
System.out.print("数据源配置" + e2.toString());
return null;
}
}
public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException ex) {
}
;
try {
if (st != null)
st.close();
} catch (SQLException ex) {
}
;
try {
if (conn != null)
conn.close();
} catch (SQLException ex) {
}
;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?