訪問DB2資料庫進行資料更新的程式碼片段

來源:互聯網
上載者:User

1.訪問Db2資料庫首先請載入如下lib:  
   db2jcc.jar,db2jcc_license_cu.jar
   在C:\Program Files\IBM\SQLLIB\java下可以找到它們。

2.若使用者沒有想要訪問的表的許可權,請開啟Db2控制中心,找到表,在右鍵菜單中加入使用者訪問許可。如果倒過來做不容易成功。

3.以下是存取碼:
package com.ibm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class TableUpdater{
    public static void main(String[] args){
        int count=TableUpdater.updateTableNOTIFY_TEXT("127.0.0.1","50000","db2admin","123456789");
        System.out.println(""+count+" records have been updated.");
    }
    
    public static int updateTableNOTIFY_TEXT(String dbIpAddress,String dbPort,String dbUserName,String dbUserPassword){
        String driver = "com.ibm.db2.jcc.DB2Driver";
        String url = "jdbc:db2://"+dbIpAddress+":"+dbPort+"/ONETEAMP";
        String userName = dbUserName;
        String passWord = dbUserPassword;
        int updatedRecordCount=0;
        
        String sql = "";
        try {
            Class.forName(driver).newInstance();
            Connection conn =  DriverManager.getConnection(url, userName, passWord);
            Statement st = conn.createStatement();
            
            sql = " update ONETEAM.NOTIFY_TEXT set NOTIFY_TYPE='a23' where SUBJECT='a' ";
            
            Map<String,String> map=getUpdateMap();
            
            for(String key:map.keySet()){
                String value=map.get(key);
                
                sql = " update ONETEAM.NOTIFY_TEXT set NOTIFY_TYPE='"+value+"' where SUBJECT='"+key+"' ";
                updatedRecordCount+=st.executeUpdate(sql);
            }
            
            conn.close();
            return updatedRecordCount;
        } catch (Exception e) {
            System.out.println("Exception occured:" + e);
            return updatedRecordCount;
        }
    }
    
    private static Map<String,String> getUpdateMap(){
        Map<String,String> map=new HashMap<String,String>();
        
        map.put("RECERT_BUNDLE_MGR_ONLY_C", "Action may be required: OneTEAM Recertification Completed. See Recertify before Date.");
        map.put("RECERT_BUNDLE_MGR_ONLY_F", "Action may be required: OneTEAM Recertification Final reminder. See Recertify before Date.");
        map.put("RECERT_BUNDLE_MGR_ONLY_I", "Action may be required: OneTEAM Recertification Initiated. See Recertify before Date.");
        map.put("RECERT_BUNDLE_MGR_ONLY_R", "Action may be required: OneTEAM Recertification Reminder notification. See Recertify before Date.");
        map.put("RECERT_BUNDLE_USER_C", "Action may be required: OneTEAM Recertification Completed. See Recertify before Date.");
        map.put("RECERT_BUNDLE_USER_F", "Action may be required: OneTEAM Recertification Final reminder. See Recertify before Date.");
        map.put("RECERT_BUNDLE_USER_I", "Action may be required: OneTEAM Recertification Initiated. See Recertify before Date.");
        map.put("RECERT_BUNDLE_USER_R", "Action may be required: OneTEAM Recertification Reminder notification. See Recertify before Date");
        
        return map;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.