posts - 18,  comments - 1,  trackbacks - 0

          import java.io.*;
          import java.util.*;
          import java.sql.*;
          import javax.naming.*;
          import javax.sql.*;
          public class ConnectionFactory
          {

          /**

          oracle?? 分頁

          select * from (
          select a.*, ROWNUM rn from ( select * from user_info )? a
          where ROWNUM<=40 )
          where rn >=21

          ?


          */
          ??? private static Properties config = new Properties();
          ???
          ??? static
          ??? {
          ??????? try
          ??????? {
          ??????????? InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
          ??????????? config.load(in);
          ??????????? in.close();
          ??????? }catch(IOException e)
          ??????? {
          ??????????? e.printStackTrace();
          ??????????? throw new ExceptionInInitializerError(e.getMessage());
          ??????? }
          ??? }
          ???
          ??? public static Connection getConnection()
          ??? {
          ??????? if(config.getProperty("jndi-name") != null)
          ??????? {
          ??????????? return getJndiConnection();
          ??????? }
          ???????
          ??????? return getDirectConnection();
          ??? }
          ???
          ??? public static Connection getJndiConnection()
          ??? {
          ??????? Connection con = null;
          ??????? try
          ??????? {
          /**context.xml

          數據源的配置
          <Context>
          ????? <Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" maxActive="2" maxIdle="1" maxWait="-1"
          ????? username="openlab" password="open123" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@192.168.0.20:1521:tarena"/>
          </Context?
          */
          ??????????? Context ctx = new InitialContext();
          ??????????? DataSource ds = (DataSource)ctx.lookup("java:comp/env/" + config.getProperty("jndi-name"));
          ??????????? con = ds.getConnection();
          ??????? }catch(Exception e)
          ??????? {
          ??????????? e.printStackTrace();
          ??????? }
          ??????? return con;
          ??? }
          ???
          ??? public static Connection getDirectConnection()
          ??? {
          ??????? Connection con = null;
          ??????? try
          ??????? {
          ??????????? Class.forName(config.getProperty("driver"));
          ??????????? con = DriverManager.getConnection(config.getProperty("dburl") ,config.getProperty("user"), config.getProperty("password"));
          ??????? }catch(ClassNotFoundException cne)
          ??????? {
          ??????????? cne.printStackTrace();
          ??????? }catch(SQLException sqle)
          ??????? {
          ??????????? sqle.printStackTrace();
          ??????? }
          ???????
          ??????? return con;
          ??? }
          ???
          ??? public static void close(ResultSet rs, Statement st, Connection con)
          ??? {
          ??????? try
          ??????? {
          ??????????? rs.close();
          ??????? }catch(Exception e)
          ??????? {
          ??????? }
          ???????
          ??????? try
          ??????? {
          ??????????? st.close();
          ??????? }catch(Exception e)
          ??????? {
          ??????? }
          ???????
          ??????? try
          ??????? {
          ??????????? con.close();
          ??????? }catch(Exception e)
          ??????? {
          ??????? }
          ??? }
          ???
          ??? public static void main(String[] args) throws Exception
          ??? {
          ??????? Connection con = ConnectionFactory.getConnection();
          ??????? System.out.println(con);
          ??????? con.close();
          ??? }
          }

          posted on 2007-04-05 15:30 sunny 閱讀(153) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          朋友

          搜索

          •  

          最新評論

          評論排行榜

          主站蜘蛛池模板: 左贡县| 郯城县| 福海县| 十堰市| 白玉县| 广水市| 方城县| 名山县| 吴川市| 和林格尔县| 原阳县| 民权县| 东明县| 封开县| 吉首市| 廉江市| 浙江省| 金乡县| 武陟县| 正镶白旗| 济源市| 化德县| 贵阳市| 普宁市| 吉林省| 阿图什市| 林周县| 阿拉善右旗| 海宁市| 古蔺县| 兴隆县| 郯城县| 稻城县| 苗栗县| 乐亭县| 绩溪县| 德兴市| 苍南县| 巨野县| 沂源县| 高青县|