JAVA

          人生若只如初見,何事秋風(fēng)悲畫扇。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

          我用以下代碼用來測試數(shù)據(jù)庫連接是否正常:

           try {
                      logger.info("start to registerDriver...");
                      DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
                      logger.info(pros.getProperty("dbURL")+"--"+pros.getProperty("user")+"--"+ pros.getProperty("password"));
                      con = DriverManager.getConnection(pros.getProperty("dbURL"), pros.getProperty("user"), pros.getProperty("password"));;
                      if (con != null) {
                          st = con.createStatement();
                          rs = st.executeQuery("select 1 from dual");
                          rs.next();
                          if (rs.getInt(1) == 1) {
                              res = true;
                          }
                      }
                  }

           

          然后我做了一個java application桌面小應(yīng)用程序(extends FrameView),在小應(yīng)用程序中點擊事件觸發(fā)此段代碼,如下:

          private void dbTestMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                              
              //
          從配置文件中讀取連接相關(guān)屬性
              String info = "
          數(shù)據(jù)庫連接異常,請確認(rèn)配置文件是參數(shù)是否正確。";
              Properties per = new Properties();
              try {
                  logger.info("start to test the db connection...");
                  per.load(new FileInputStream(Content.configFile));
                  logger.info("system have loaded the config files successfully");
                
                  DbOper db = new DbOper();
                  if (db.checkDBConnect(per)) {
                      info = "
          數(shù)據(jù)庫連接正常";
                  }
                  logger.info("
          數(shù)據(jù)連接測試結(jié)果:" + info);
              } catch (Exception e) {
                  logger.error("Exception when test the DB connection, ",e);
              }

           

              if (aboutBox == null) {
                  JFrame mainFrame = AnalyseExcelApp.getApplication().getMainFrame();
                  aboutBox = new DBTestResultBox(mainFrame, info);
                  aboutBox.setLocationRelativeTo(mainFrame);
              }
              AnalyseExcelApp.getApplication().show(aboutBox);
          }
               

           

          但在運行時,程序頭也不回地直接死了,調(diào)試時發(fā)現(xiàn)程序是在

          con = DriverManager.getConnection(pros.getProperty("dbURL"), pros.getProperty("user"), pros.getProperty("password"));;
          這一行,不行了,也不報錯,就直接在此行一直呆著,啥也不做...

           

          另外還有二種情況,也讓我比較奇怪:

          1. 若同樣調(diào)用此段測試代碼,但不是在java application中,而是直接在一個普通的java類的main方法中,測可以測試通過。

          2. 若數(shù)據(jù)庫邊的是本機(jī)的數(shù)據(jù)庫,那么此段測試代碼也可以通過,在java applicationjava類的main方法中均可以。

           

          為什么,連接遠(yuǎn)程數(shù)據(jù)時,在java application中會無故down掉呢?

           

          ps. 排除數(shù)據(jù)庫地址,用戶名,密碼等錯誤;我的JDBC驅(qū)動是ojdbc14.jar,JDK 1.5

          posted on 2008-07-15 16:28 Jkallen 閱讀(1166) 評論(3)  編輯  收藏 所屬分類: JEE學(xué)習(xí)

          評論

          # re: 咨詢一個自認(rèn)為很奇怪的問題(在java中獲取數(shù)據(jù)庫連接時相關(guān)) 2008-07-15 16:30 Jkallen
          麻煩兄弟們,幫我頂下哈
          因為在現(xiàn)場,急啊...  回復(fù)  更多評論
            

          # re: 咨詢一個自認(rèn)為很奇怪的問題(在java中獲取數(shù)據(jù)庫連接時相關(guān)) 2009-05-30 13:26 墻頭草
          廣告時間~~~~~~~~~


          ===================================================
          TinyMCE使用/集成/設(shè)置/詳解(1)
          TinyMCE使用/集成/設(shè)置/詳解(2)
          在FCKeditor中添加右鍵菜單
          FCKEditor自動過濾的解決辦法
          FCKeditor配置與擴(kuò)展   回復(fù)  更多評論
            

          # re: 咨詢一個自認(rèn)為很奇怪的問題(在java中獲取數(shù)據(jù)庫連接時相關(guān)) 2013-10-31 10:11 牛武子
          @Jkallen
          在現(xiàn)場 好苦逼哦  回復(fù)  更多評論
            

          主站蜘蛛池模板: 江油市| 赞皇县| 娄底市| 高阳县| 武安市| 从江县| 临朐县| 铁力市| 衡东县| 博罗县| 莎车县| 扶余县| 锡林浩特市| 武定县| 天柱县| 和田市| 蓬溪县| 安义县| 锡林浩特市| 曲沃县| 温泉县| 蓬安县| 云霄县| 承德市| 简阳市| 辽宁省| 贵德县| 丹江口市| 苏州市| 通州区| 兴化市| 柳江县| 博客| 靖宇县| 永川市| 渭源县| 新绛县| 顺昌县| 婺源县| 合肥市| 普宁市|