我的连接池已经配好,就是我在做增回一条记录时,怎么打开连接!!!!

解决方案 »

  1.   

    在进行连接池操作的时候都是要在try{}catch   中的,你这样可以捕获异常啊
      

  2.   

    你说的就是这样吧
    public class CheckUser 
    {
    DataSource dataSource;
    public  CheckUser(DataSource dataSource)
    {
    this.dataSource=dataSource;
    }
    Connection con;
    public boolean checklogin(String name,String psw)throws Exception
    {

    String sql;
    ResultSet rs;

    boolean result=false;
    sql="select * from classuser where username='"+ name +"' and password='"+ psw +"'";
    try
    {
    con=dataSource.getConnection();
    Statement st=con.createStatement();
    rs=st.executeQuery(sql);
    if(rs!=null)
    {
    result=true;
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    if(con!=null)
    {
    con.close();
    }
    }
    return result;
    }
    }
      

  3.   

    做成一个工厂类,每次用静态方法来取得联接:
    public class GetConn
    {
    public static Connection getConn(DataSource ds)
    {try
    {return ds.getConnection();}
    catch(Exception ex)
    {
    ex.printStacktrace();
    }
    }
    }
    调用的话用:GetConn.getConn(ds);每次用完注意close()
      

  4.   

    package com.db;import java.sql.*;
    import com.util.LogWriter;
    public class Dbconnection {  /**
       * 获取数据库普通连接
       * @return Connection
       */  public static Connection getConnection() {
        try {
           Connection conn= JsqlDataSource.getjsqlDataSource().getPooledConnection().getConnection();
           System.out.println("获取数据库连接 DBConnection="+conn) ;
           return conn;
        } catch (Exception e) {
          LogWriter.error(e);
          return null;
        }
      }  /**
       * 获取带事物的数据库连接
       * @throws SQLException
       * @throws Exception
       * @return Connection
       */
      public static Connection getTransConnect() throws SQLException, Exception {
        Connection conn = getConnection();
        conn.setAutoCommit(false);
        return conn;
      }  /**
       * 关闭数据库连接 表达式 数据集
       * @param conn Connection
       * @param stmt PreparedStatement
       * @param rs ResultSet
       * @return boolean
       */
      public static boolean tryClose(Connection conn, PreparedStatement stmt,
                                     ResultSet rs) {
        boolean result = true;    try {
          if (rs != null) rs.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("数据集关闭异常", e);
        }    try {
          if (stmt != null) stmt.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("Statement关闭异常", e);
        }
        try {
          if (conn != null) conn.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("数据库关闭异常", e);
        }    return result;
      }  /**
       * 关闭数据库连接 表达式 数据集
       * @param conn Connection
       * @param stmt Statement
       * @param rs ResultSet
       * @return boolean
       */
      public static boolean tryClose(Connection conn, Statement stmt, ResultSet rs) {
        boolean result = true;    try {
          if (rs != null) rs.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("数据集关闭异常", e);
        }    try {
          if (stmt != null) stmt.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("Statement关闭异常", e);
        }
        try {
          if (conn != null) conn.close();
        } catch (Exception e) {
          result = false;
          LogWriter.error("数据库关闭异常", e);
        }    return result;
      }  /**
       * 关闭数据库连接
       * @param conn Connection
       * @return boolean
       */
      public static boolean tryClose(Connection conn) {
        try {
          conn.close();
          return true;    } catch (Exception e) {
          LogWriter.error("数据库关闭异常", e);
          return false;
        }
      }  /**
       * 关闭数据库连接 表达式
       * @param conn Connection
       * @param stm Statement
       * @return boolean
       */
      public static boolean tryClose(Connection conn, Statement stm) {
        try {
          stm.close();
          conn.close();
          return true;
        }
        catch (Exception e) {
          LogWriter.error("Statement和数据库连接关闭异常", e);
          return false;
        }
      }  /**
       * 关闭ResultSet
       * @param conn Connection
       * @return boolean
       */
      public static boolean tryClose(ResultSet rs) {
        try {
          rs.close();
          return true;
        } catch (Exception e) {
          LogWriter.error("数据集关闭异常", e);
          return false;
        }
      }}
      

  5.   

    <%@ page import="javax.sql.*"%>
    <%@ page import="javax.naming.*"%>
    <% 
       DataSource ds = null;
       try{
       InitialContext ctx=new InitialContext();
       ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mssql");
       Connection conn = ds.getConnection();
       Statement stmt = conn.createStatement();
       String strSql = " select * from ttt";
       ResultSet rs = stmt.executeQuery(strSql);
       while(rs.next()){
          System.out.println(rs.getString(1));                 
         }
       }
       catch(Exception ex){
       ex.printStackTrace();
       }
    %>