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

          數(shù)據(jù)源的配置
          <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)  編輯  收藏

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


          網(wǎng)站導航:
           
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          朋友

          搜索

          •  

          最新評論

          評論排行榜

          主站蜘蛛池模板: 同心县| 昆明市| 普兰县| 彰化市| 白银市| 华亭县| 嘉荫县| 南乐县| 高碑店市| 宣恩县| 射洪县| 长沙市| 新野县| 和平县| 夏津县| 永康市| 大城县| 新晃| 皋兰县| 神农架林区| 汉源县| 余姚市| 尼木县| 星子县| 雅安市| 黑山县| 丰都县| 冀州市| 宁国市| 绵阳市| 金昌市| 辛集市| 泰州市| 临澧县| 景泰县| 丰原市| 芜湖市| 辽宁省| 建昌县| 行唐县| 通渭县|