package news.database;
import javax.naming.*;
import javax.sql.DataSource;
import javax.servlet.ServletContext;
import java.sql.*;
public class DBConnect
{
    boolean jndiDb=false;
    private String strDbuid="sa";
    private String strDbpwd="";
    private Connection con;
    private Statement stmt;
    private PreparedStatement prepstmt;
    String dsnName;
    ServletContext application;
    public static Context env;
    public static DataSource pool;
    public static Context env2;
    public static DataSource pool2;
    public static int connNum=0;
    public static int curConnNum=0;
    boolean isNoMainCon=false;
   public static void  getDbInit(){
         try{
                env = new InitialContext();
        pool = (DataSource) env.lookup("java:comp/env/jdbc/news");
            }
            catch(Exception e){
               e.printStackTrace();
           }
    }
       public static void  getDbInit2(){
         try{
                env2 = new InitialContext();
        pool2 = (DataSource) env2.lookup("java:comp/env/jdbc/jkSoft");
            }
            catch(Exception e){
               e.printStackTrace();
           }
    }
    public  Connection connectDb2(){
            Connection conNew=null;
     try
        {
                  if (pool2 == null) throw new Exception("jdbc/jkSoft isan unknown DataSource");
conNew = pool2.getConnection();
conNew.setAutoCommit(true);
        } catch(Exception e)
        {
         conNew=null;
         System.out.println("Can not connect to database:"+e.getMessage());
 e.printStackTrace();
        }
            return conNew;
     }
     public void DBConnect()
    {
            strDbuid="sa";
            strDbpwd="";
    }
   
    public DBConnect initialize( ServletContext application)
    {
            DBConnect db=new DBConnect();
            strDbuid   =  (String)application.getAttribute("DB_UID");
            strDbpwd   = (String)application.getAttribute("DB_PWD");
            isNoMainCon=false;
        return db;
    }
    public DBConnect  initialize2( ServletContext application)
    {
          //get connection from DB pool
        DBConnect db=new DBConnect();
        try
        {
            if (pool2 == null)
                 throw new Exception("jdbc/jkSoft  is an unknown DataSource");
            con = pool2.getConnection();
            con.setAutoCommit(true);
          //  this.application = application;
            System.out.println("initialize a jksoft dbconnect object=========================="+con);
        }
        catch(Exception e)
        {
            System.err.println("Error occured in [CDbConnect.initialize(application)]:" +
                    e.getMessage());
        }
        return db;
    }
   public DBConnect initialize3( ServletContext application)
    {
     DBConnect db=new DBConnect();
       if(null!=application.getAttribute("DB_VOD_DSN"))
        {
            dsnName =(String)application.getAttribute("DB_VOD_DSN");
            isNoMainCon=true;
        }
        return db;
    }
     public Connection getCurConnect(){
        return con;
    }
    public Connection getConnect()
    {
           connNum++;
        try{
        if(con==null||con.isClosed()){              connectDb();
        }
            System.out.println("dbS:"+connNum+" "+con);
        }catch(Exception e){}
        return con;
      }
        public synchronized Connection getNewConnect(){
         Connection conNew=null;
       try
        {
           if(dsnName!=null)
                connectAccess();
           else{
                 if (pool == null) throw new Exception("jdbc/news is an unknown DataSource");
conNew = pool.getConnection(strDbuid, strDbpwd);
conNew.setAutoCommit(true);
    System.out.println("======new==============================");
System.out.println("getNewConn :"+conNew);
           }
        } catch(Exception e)
        {
            conNew=null;
            System.out.println("Can not connect to database_getNewConnect:"+e.getMessage() );
        }
        return conNew;
    }
     private synchronized void connectDb(){
     try
        {
           if(dsnName!=null)
                connectAccess();
           else{
                  if (pool == null) throw new Exception("jdbc/news isan unknown DataSource");
con = pool.getConnection(strDbuid, strDbpwd);
con.setAutoCommit(true);
                    curConnNum++;
                System.out.println("getConn :=>"+curConnNum+"+"+con);
           }
        } catch(Exception e)
        {
         con=null;
         System.out.println("Can not connect to database:"+e.getMessage());
 e.printStackTrace();
        }
     }    public Connection connectAccess(){
         Connection conAcc=null;
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conAcc = DriverManager.getConnection ("jdbc:odbc:hfjsw");
            //conAcc = DriverManager.getConnection ("jdbc:odbc:"+dsnName);
        }catch(Exception e){}
         return conAcc;
    }
     
      public ResultSet executeQuery(String sql)
      {
         ResultSet rs = null;
         try
         {
            if(con==null||con.isClosed())
                getConnect();
            stmt=con.createStatement();
            rs = stmt.executeQuery(sql);
         }
         catch(Exception ex)
         {
             System.out.println("error during executeQuery: "+sql+"\n" );
             ex.printStackTrace();
         }
        return rs;
      }          public ResultSet executeRollQuery(String sql)
      {
         ResultSet rs = null;
         try
         {
            if(con==null||con.isClosed())
                getConnect();
            stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
         }
         catch(Exception ex)
         {
             System.out.println("error during executeQuery: "+sql+"\n" );
             ex.printStackTrace();
         }
        return rs;
      }
      
      public int executeUpdate(String sql) throws SQLException
      {
        int result=0;
        stmt=null;
        if(con==null||con.isClosed())
            getConnect();
        stmt = con.createStatement();
        result=stmt.executeUpdate(sql);
        stmt.close();
        return result;
      }
     

解决方案 »

  1.   

    public ResultSet executeQuery()//返回ResultSet对象
            throws SQLException
        {
            if(con==null||con.isClosed())
                getConnect();
            if(prepstmt != null){
    return prepstmt.executeQuery();
    }
            else
                return null;
        }     /**
         * 预编译sql语句
         * @param s  sql语句
         * @throws SQLException
         */
        public void prepareStatement(String s, int i, int j)
            throws SQLException
        {
            if(con==null||con.isClosed())
                getConnect();
            prepstmt = con.prepareStatement(s, i, j);
        }
        /**
        * 预编译sql语句中的String参数
        * @param i  位置
        * @param s  参数值
        * @throws SQLException
        */    public void setString(int i, String s)//第一个参数:设置参数的序数位置;第二个参数:设置该参数的值        throws SQLException
        {
            prepstmt.setString(i, s);
        }
        /**
        * 预编译sql语句中的int参数
        * @param i  位置
        * @param j  参数值
        * @throws SQLException
        */
        public void setInt(int i, int j)
            throws SQLException
        {
            prepstmt.setInt(i, j);
        }
        /**
        * 预编译sql语句中的boolean参数
        * @param i  位置
        * @param flag  参数值
        * @throws SQLException
        */
        public void setBoolean(int i, boolean flag)
            throws SQLException
        {
            prepstmt.setBoolean(i, flag);
        }
        /**
        * 预编译sql语句中的Date参数
        * @param i  位置
        * @param  date :java.sql.Date 参数值
        * @throws SQLException
        */
        public void setDate(int i, Date date)
            throws SQLException
        {
            prepstmt.setDate(i, date);
        }
         /**
        * 预编译sql语句中的long参数
        * @param i  位置
        * @param l  long参数值
        * @throws SQLException
        */
        public void setLong(int i, long l)
            throws SQLException
        {
            prepstmt.setLong(i, l);
        }
        /**
        * 预编译sql语句中的float参数
        * @param i  位置
        * @param f  float参数值
        * @throws SQLException
        */
        public void setFloat(int i, float f)
            throws SQLException
        {
            prepstmt.setFloat(i, f);
        }
        /**
        * 预编译sql语句中的byte[]参数
        * @param i  位置
        * @param abyte0  参数值
        * @throws SQLException
        */
        public void setBytes(int i, byte abyte0[])
            throws SQLException
        {
            prepstmt.setBytes(i, abyte0);
        }
        /**
        * 关闭预编译的Statament
        */
        public void clearParameters()//调用PrepareStare接口的方法prepstmt.clearParameters()清楚参数。
        {
    try{
                if(prepstmt!=null) {
                    prepstmt.clearParameters();
                    prepstmt.close();
                }
    }
    catch(Exception e){
    //System.out.println("IDEA_ERROR:"+e.getMessage());
    }
        }
       /**
        * 返回  PreparedStatement对象
        * @return  PreparedStatement
        */
        public PreparedStatement getPreparedStatement()//返回PreparedStatement对象
        {
            return prepstmt;
        }    public void setCon(Connection con) {
            this.con = con;
        }}
      

  2.   

    tomcat是在conf/下的server.xml文件指定数据源的,然后在你的工程的WEB-INF里的web.xml里指定下,就行了,resin不知道.