mengkuku

          JAVA用JDBC連接數據庫MSSQL、MYSQL(轉)

          經常忘記怎么連接數據庫,貼出來備用

          DBConnection.java

           

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

          /**
          * @author study
          *
          * 從給定的資源信息中得到數據庫聯接對象
          *
          */
          public class DBConnection {
          Connection conn = null;

          /**
          * 從給定的資源文件中獲得連接數據庫的參數
          *
          */
          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讀入錯誤");
               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 {
               // 加載驅動程序
               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 上帝也瘋狂 閱讀(1135) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 彩票| 黄大仙区| 沅江市| 肥乡县| 临城县| 安阳市| 天水市| 修水县| 景洪市| 金湖县| 菏泽市| 广汉市| 八宿县| 广饶县| 长武县| 旬邑县| 藁城市| 民丰县| 砀山县| 台州市| 汪清县| 合阳县| 宜都市| 淮安市| 彭阳县| 阿克陶县| 拜城县| 横峰县| 多伦县| 蕉岭县| 抚松县| 蓬安县| 潍坊市| 南丹县| 贵溪市| 昂仁县| 肃南| 乐东| 屏东县| 定西市| 新营市|