访问DB2数据库进行数据更新的代码片段


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;
    }
}

相关内容