mengkuku

          JAVA用JDBC連接數(shù)據(jù)庫(kù)MSSQL、MYSQL(轉(zhuǎn))

          經(jīng)常忘記怎么連接數(shù)據(jù)庫(kù),貼出來(lái)備用

          DBConnection.java

           

          import java.sql.*;
          import java.util.Locale;
          import java.util.PropertyResourceBundle;

          /**
          * @author study
          *
          * 從給定的資源信息中得到數(shù)據(jù)庫(kù)聯(lián)接對(duì)象
          *
          */
          public class DBConnection {
          Connection conn = null;

          /**
          * 從給定的資源文件中獲得連接數(shù)據(jù)庫(kù)的參數(shù)
          *
          */
          public boolean getConnect() {
             String str_URL = "";
             String str_userName = "";
             String str_passWord = ""; // the connect passWord
             String str_JdbcDriverName = ""; // the connect JDBCDriverName
             // Connection con = null;
             try {
              PropertyResourceBundle configBundle = (PropertyResourceBundle) PropertyResourceBundle
                .getBundle("common.jiangbin.dms.product",
                  new Locale("cn", "CN"));

              if (configBundle == null) {
               System.out.println("文件product_cn_CN.properties讀入錯(cuò)誤");
               return false;
              }

              // the connect URL
              str_URL = configBundle.getString("ConnectString");
              // the connect userName
              str_userName = configBundle.getString("UserID");
              // the connect passWord
              str_passWord = configBundle.getString("Password");
              // the connect JDBCDriverName
              str_JdbcDriverName = configBundle.getString("JdbcDriverName");
              try {
               // 加載驅(qū)動(dòng)程序
               Class.forName(str_JdbcDriverName).newInstance();
              } catch (ClassNotFoundException e) {
               System.out.println("Driver not found");
              }

              // DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
              // DriverManager.registerDriver(null);

              this.conn = DriverManager.getConnection(str_URL, str_userName,
                str_passWord);

             } catch (Exception e) {
              e.printStackTrace();
              return false;
             }
             return true;
          }

          /**
          * Returns the conn.
          *
          * @return Connection
          */
          public Connection getConn() {
             return conn;
          }

          /**
          * Sets the conn.
          *
          * @param conn
          *            The conn to set
          */
          public void setConn(Connection conn) {
             this.conn = conn;
          }

          }

           

          屬性文件product_cn_CN.properties(用于MSSQL)

          ConnectString=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DocManagerSystem
          UserID=sa
          Password=sa
          JdbcDriverName=com.microsoft.jdbc.sqlserver.SQLServerDriver

           

          屬性文件product_cn_CN.properties(用于MYSQL)

          ConnectString=jdbc:mysql://localhost:3306/mydata
          UserID=sa
          Password=sa
          JdbcDriverName=com.mysql.jdbc.Driver

          posted on 2012-10-16 19:26 上帝也瘋狂 閱讀(1134) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 浙江省| 中阳县| 穆棱市| 芦山县| 青川县| 喀喇沁旗| 扶风县| 泗阳县| 石屏县| 珲春市| 怀仁县| 丰都县| 临泽县| 延安市| 高平市| 五寨县| 呼和浩特市| 江川县| 永川市| 黎川县| 宝山区| 瓦房店市| 石楼县| 广元市| 台东县| 武城县| 调兵山市| 息烽县| 东丰县| 申扎县| 西乌珠穆沁旗| 武陟县| 曲麻莱县| 卢氏县| 筠连县| 屯门区| 桦川县| 富锦市| 长泰县| 枝江市| 淮滨县|