dbconn.java

来自「jsp做的一个在线考试,不错的哈.可以看看,这个我测试通过了的.」· Java 代码 · 共 48 行

JAVA
48
字号
package org.mmxbb.exam.util;



import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBConn {
  DataSource ds = null;
  Connection conn = null;

  public DBConn() {
  }

  /**
   * <li>get the connection from lookup DataSource
   * @return Connection
   */
  public Connection getConnection() {
    try {
    	Context initCtx = new InitialContext();
    	 Context envCtx = (Context)initCtx.lookup("java:comp/env");
         ds = (DataSource)envCtx.lookup("jdbc/exam");
         conn=ds.getConnection();
      	
    
    } catch (Exception e) {
      System.err.println("getConnection error: " + e);
      return null;
    }
    return conn;
  }

    
  /**
   * return connection to pool
   */
  public void close() {
    try {
      conn.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?