I'll be back!

            Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
          posts - 76, comments - 161, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          DBAccess.java

          package database;

          import java.sql.*;

          /**
           * 
          @author Administrator
           * 
           
          */
          public class DBAccess {
              
          private Connection m_conn;

              
          private Statement m_stmt;

              String driver 
          = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

              String url 
          = "jdbc:microsoft:sqlserver://localhost:1433;databasename=sc";

              String uName 
          = "sa";

              String uPwd 
          = "sa";

              
          /**
               * 
               
          */
              
          public DBAccess() {
                  
          this.setDriver(driver);
                  
          this.setConnection(url, uName, uPwd);
              }

              
          public DBAccess(String driver, String url, String userName, String userPWD) {
                  
          try {
                      m_conn 
          = DriverManager.getConnection(url, userName, userPWD);
                      m_stmt 
          = m_conn.createStatement();
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                  }
              }

              
          public boolean setDriver(String driver) {
                  
          try {
                      Class.forName(driver);
                      
          return true;
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
                  
          return false;
              }

              
          public boolean setConnection(String url, String userName, String userPWD) {
                  
          try {
                      m_conn 
          = DriverManager.getConnection(url, userName, userPWD);
                      m_stmt 
          = m_conn.createStatement();
                      
          return true;
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
                  
          return false;
              }

              
          // 處理查詢
              public ResultSet sendQuery(String sql) {
                  
          try {
                      ResultSet m_rs 
          = m_stmt.executeQuery(sql);
                      
          return m_rs;
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                      
          return null;
                  }
              }

              
          // 處理數(shù)據(jù)更新
              public int sendUpdate(String sql) {
                  
          try {
                      
          return m_stmt.executeUpdate(sql);
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                      
          return -1;
                  }
              }

              
          // 測(cè)試程序
              public static void main(String[] arg) {
                  DBAccess db 
          = new DBAccess();
                  String sql 
          = "select * from Student";
                  ResultSet rs 
          = db.sendQuery(sql);
                  
          try {
                      
          if (rs != null) {
                          
          while (rs.next()) {
                              System.out.println(rs.getInt(
          "Sno"+ " "
                                      
          + rs.getString("Sname"));
                          }
                      }
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                  }
              }

          }

          下面羅列了各種數(shù)據(jù)庫(kù)使用JDBC連接的方式,可以作為一個(gè)手冊(cè)使用。 

            1、Oracle8/8i/9i數(shù)據(jù)庫(kù)(thin模式) 

          Class.forName('oracle.jdbc.driver.OracleDriver').newInstance(); 
          String url
          ='jdbc:oracle:thin:@localhost:1521:orcl'//orcl為數(shù)據(jù)庫(kù)的SID 
          String user='test'
          String password
          ='test'
          Connection conn
          = DriverManager.getConnection(url,user,password);  


            2、DB2數(shù)據(jù)庫(kù) 

          Class.forName('com.ibm.db2.jdbc.app.DB2Driver ').newInstance(); 
          String url
          ='jdbc:db2://localhost:5000/sample'//sample為你的數(shù)據(jù)庫(kù)名 
          String user='admin'
          String password
          =''
          Connection conn
          = DriverManager.getConnection(url,user,password);  


            3、Sql Server7.0/2000數(shù)據(jù)庫(kù) 

          Class.forName('com.microsoft.jdbc.sqlserver.SQLServerDriver').newInstance(); 
          String url
          ='jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb'
          //mydb為數(shù)據(jù)庫(kù) 
          String user='sa'
          String password
          =''
          Connection conn
          = DriverManager.getConnection(url,user,password);  


            4、Sybase數(shù)據(jù)庫(kù) 

          Class.forName('com.sybase.jdbc.SybDriver').newInstance(); 
          String url 
          =' jdbc:sybase:Tds:localhost:5007/myDB';//myDB為你的數(shù)據(jù)庫(kù)名 
          Properties sysProps = System.getProperties(); 
          SysProps.put(
          'user','userid'); 
          SysProps.put(
          'password','user_password'); 
          Connection conn
          = DriverManager.getConnection(url, SysProps);  


            5、Informix數(shù)據(jù)庫(kù) 

          Class.forName('com.informix.jdbc.IfxDriver').newInstance(); 
          String url 
          = 'jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; 
          user=testuser;password=testpassword'; //myDB為數(shù)據(jù)庫(kù)名 
          Connection conn= DriverManager.getConnection(url);  


            6、MySQL數(shù)據(jù)庫(kù) 

          Class.forName('org.gjt.mm.mysql.Driver').newInstance(); //或者Class.forName('com.mysql.jdbc.Driver');
          String url ='jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1' 
          //myDB為數(shù)據(jù)庫(kù)名 
          Connection conn= DriverManager.getConnection(url);  


            7、PostgreSQL數(shù)據(jù)庫(kù) 

          Class.forName('org.postgresql.Driver').newInstance(); 
          String url 
          ='jdbc:postgresql://localhost/myDB' //myDB為數(shù)據(jù)庫(kù)名 
          String user='myuser'
          String password
          ='mypassword'
          Connection conn
          = DriverManager.getConnection(url,user,password);  


            8、access數(shù)據(jù)庫(kù)直連用ODBC的

          Class.forName('sun.jdbc.odbc.JdbcOdbcDriver') ;
          String url
          ='jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ='+application.getRealPath('/Data/ReportDemo.mdb');
          Connection conn 
          = DriverManager.getConnection(url,'','');
          Statement stmtNew
          =conn.createStatement() ; 


           


          評(píng)論

          # re: 簡(jiǎn)單的JDBC連接數(shù)據(jù)庫(kù)代碼  回復(fù)  更多評(píng)論   

          2007-10-24 10:22 by freeman1984
          up 不過代碼有點(diǎn)繁瑣,方法名需要改進(jìn)。

          # re: 簡(jiǎn)單的JDBC連接數(shù)據(jù)庫(kù)代碼  回復(fù)  更多評(píng)論   

          2008-04-27 12:46 by thebye85
          Oracle中
          Class.forName('oracle.jdbc.driver.OracleDriver').newInstance();

          Class.forName('oracle.jdbc.driver.OracleDriver');

          都可以吧?有什么區(qū)別嗎?

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 伊川县| 上杭县| 兴隆县| 房山区| 广宁县| 隆尧县| 广元市| 玉树县| 左权县| 昌吉市| 仁寿县| 莱州市| 乳山市| 文成县| 台北县| 阳朔县| 永济市| 安乡县| 杂多县| 德化县| 阜平县| 济阳县| 和林格尔县| 贵德县| 双鸭山市| 麻江县| 辽阳市| 阳朔县| 水富县| 治多县| 临洮县| 肃宁县| 兴安盟| 昔阳县| 盖州市| 阿瓦提县| 财经| 揭阳市| 闽清县| 宁津县| 阳泉市|