JNDI创建java.sql.Connection
2012-04-18 12:03
344 查看
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import org.apache.log4j.Logger; import org.springframework.core.io.support.PropertiesLoaderUtils; /** * * @author lyh * @version * @see DbManager * @since */ public class DbManager { /** * 日志 */ private static final Logger LOGGER = Logger.getLogger(DbManager.class); /** * 命名上下文 */ private static Context initctx = null; /** * 资源文件 */ private static Properties prop = null; /** * 数据源 */ @SuppressWarnings("unused") private static DataSource bds = null; /** * jndi名称 */ private static String jndiName = ""; /** * 驱动 */ private static String driver = ""; /** * url */ private static String url = ""; /** * db用户名 */ private static String username = ""; /** * db密码 */ private static String password = ""; static { try { prop = PropertiesLoaderUtils.loadAllProperties("jdbc.properties"); } catch (IOException e1) { LOGGER.error(e1.getMessage()); } // load jdbc.properties driver = (String)prop.get("driver"); url = (String)prop.get("url"); username = (String)prop.get("username"); password = (String)prop.get("password"); // jndiName e.g. java:comp/env/jdbc/rhy jndiName = (String)prop.get("jndiName"); // for jndi // init(); try { Class.forName(driver); } catch (ClassNotFoundException e) { LOGGER.error(e.getMessage()); } } /** * * Description: 获得DB连接<br> * @return * @see */ public static Connection getDBConnection() { // for jndi /*Connection con = null; try { con = bds.getConnection(); } catch (SQLException e) { LOGGER.error(e.getMessage()); } return con; */ Connection con = null; try { con = (Connection) DriverManager.getConnection(url, username, password); } catch (SQLException e) { LOGGER.error(e.getMessage()); } return con; } /** * * Description:关闭DB连接 <br> * @param con * @see */ public static void closeDBConnection(Connection con) { try { if (con != null && !con.isClosed()) { con.close(); } } catch (Exception e) { LOGGER.error(e.getMessage()); } } /** * * Description:jndi init method <br> * @see */ @SuppressWarnings("unused") private static void init() { try { initctx = new InitialContext(); bds = (DataSource) initctx.lookup(jndiName); } catch (Exception e) { LOGGER.error(e.getMessage()); } } }
相关文章推荐
- 【MyBatis】org.apache.ibatis.executor.statement.StatementHandler.prepare(java.sql.Connection)
- java.sql.SQLException: nontx connection pool is full?
- Could not get JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'ro
- spring+ibatis问题1—— 程序报错:java.sql.SQLException: Io 异常: Connection reset by peer, socket write error; ”或“java.sql.SQLException 关闭的连接”异常
- java.sql.SQLException: No operations allowed after connection closed.Connection was implicitly clos
- java.sql.connection,java.sql.statement
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
- 2016.11.10 Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver
- 在整合spring+mybatis+Druid过程中出现的问题(java.sql.SQLException: validateConnection false和Unsupported major.m)
- java.sql.SQLException: Connection is closed
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
- 解决Linux下JSP连接Orcale的著名错误Java.SQL.Exception The Network Adapter could not establish the connection .
- druid数据库连接池问题:java.sql.SQLException: connection holder is null
- tomcat dbcp 基于jndi当配置java.sql.SQLException: Already closed
- java.sql.SQLException: The Network Adapter could not establish the connection
- Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not
- 获取WEBLOGIC控制台配置的JNDI数据源,并创建Connection
- 创建数据库失败((Microsoft.SqlServer.Smo)) 执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB