Code snippet for accessing the DB2 database for data updates

Source: Internet
Author: User

1. To access the Db2 database, first load the following lib:
Db2jcc. jar, db2jcc_license_cu.jar
You can find them in C: \ Program Files \ IBM \ SQLLIB \ java.

2. If the user does not have the permission to access the table, open the Db2 control center, find the table, and add the user access permission to the right-click menu. However, it is not easy to succeed.

3. The following is the access code:
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. policy_text set policy_type = 'a23' where SUBJECT = 'A '";

Map <String, String> map = getUpdateMap ();

For (String key: map. keySet ()){
String value = map. get (key );

SQL = "update ONETEAM. policy_text set policy_type = '" + value + "'where SUBJECT ='" + key + "'";
Updatedrecordcount+st.exe cuteUpdate (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 recersponbefore Date .");
Map. put ("RECERT_BUNDLE_MGR_ONLY_F", "Action may be required: OneTEAM Recertification Final reminder. See recer=before Date .");
Map. put ("RECERT_BUNDLE_MGR_ONLY_ I", "Action may be required: OneTEAM Recertification Initiated. See recersponbefore Date .");
Map. put ("RECERT_BUNDLE_MGR_ONLY_R", "Action may be required: OneTEAM Recertification Reminder notification. See recer=before Date .");
Map. put ("RECERT_BUNDLE_USER_C", "Action may be required: OneTEAM Recertification Completed. See recersponbefore Date .");
Map. put ("RECERT_BUNDLE_USER_F", "Action may be required: OneTEAM Recertification Final reminder. See recer#before Date .");
Map. put ("RECERT_BUNDLE_USER_ I", "Action may be required: OneTEAM Recertification Initiated. See recersponbefore Date .");
Map. put ("RECERT_BUNDLE_USER_R", "Action may be required: OneTEAM Recertification Reminder notification. See recer#before Date ");

Return map;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.