ibm-information-center/dist/eclipse/plugins/i5OS.ic.ddp_5.4.0.1/rbal1exjavaprogram.htm

277 lines
15 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Java program" />
<meta name="DC.Relation" scheme="URI" content="rbal1progxmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbal1exjavaprogram" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Java program</title>
</head>
<body id="rbal1exjavaprogram"><a name="rbal1exjavaprogram"><!-- --></a>
<img src="./delta.gif" alt="Start of change" /><!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Java™ program</h1>
<div><div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
</div>
<div class="example"><div class="fignone"><pre>/******************************************************************************/
/* PROGRAM NAME: SampJava */
/* */
/* DESCRIPTIVE NAME: Sample java application using DRDA */
/* */
/* FUNCTION: This module processes the PART_STOCK table and */
/* for each part below the ROP (REORDER POINT) */
/* creates a supply order. */
/* */
/* LOCAL TABLES: PART_STOCK */
/* */
/* REMOTE TABLES: PART_ORDER, PART_ORDLN, SHIPMENTLN */
/* */
/* COMPILE OPTIONS: */
/* javac SampJava.java */
/* */
/* INVOKED BY: */
/* java SampJava lcldbname rmtdbname */
/******************************************************************************/
import java.sql.*;
public class SampJava {
private static String JDBCDriver = "com.ibm.db2.jcc.DB2Driver";
private static String part_table = " ";/* part number in table part_stock */
private static long line_count = 0;/* total number of order lines */
private static long eoq_table = 0;/* reorder quantity , tbl part_stock */
private static long quant_table = 0;/* quantity in stock, tbl part_stock */
private static long rop_table = 0;/* reorder point , tbl part_stock */
private static int contl = 0; /* continuation line, tbl order_line */
private static short next_num = 0;/* next order nbr,table part_order */
/****************************************************************************/
/* Method For Reseting Environment */
/****************************************************************************/
private static void resetTables(Connection rmtConn) throws SQLException {
Statement stmt1 = rmtConn.createStatement();
/* Clean up for rerunability in test environment */
stmt1.executeUpdate("DELETE FROM DRDA.PART_ORDLN WHERE ORDER_NUM IN " +
" (SELECT ORDER_NUM FROM DRDA.PART_ORDER " +
" WHERE ORDER_TYPE = 'R')");
stmt1.executeUpdate("DELETE FROM DRDA.PART_ORDER WHERE ORDER_TYPE = 'R'");
stmt1.close();
rmtConn.commit();
} /* function delete_for_rerun */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre> /****************************************************************************/
/* Method For Calculating Order Quantity */
/****************************************************************************/
private static void calculateOrderQuantity(Connection lclConn, Connection rmtConn, String loc)
throws SQLException {
PreparedStatement prpStmt1;
PreparedStatement prpStmt2;
ResultSet rsltSet1;
ResultSet rsltSet2;
short ord_table = 0; /* order nbr. , tbl order_line */
short orl_table = 0; /* order line , tbl order_line */
prpStmt1 = lclConn.prepareStatement("SELECT PART_NUM, PART_QUANT, PART_ROP, PART_EOQ " +
" FROM DRDA.PART_STOCK WHERE PART_ROP &gt; PART_QUANT AND " +
" PART_NUM &gt; ? ORDER BY PART_NUM");
prpStmt1.setString(1,part_table);
rsltSet1 = prpStmt1.executeQuery();
if (rsltSet1.next() == false) {
System.out.println("--------------------------------");
System.out.println("NUMBER OF LINES CREATED = " + line_count);
System.out.println("--------------------------------");
System.out.println("***** END OF PROGRAM *********");
rop_table = 0; /* no (more) orders to process */
}
else {
/* available qty = Stock qty + qty in order - qty received */
part_table = rsltSet1.getString(1);
quant_table = rsltSet1.getLong(2);
rop_table = rsltSet1.getLong(3);
eoq_table = rsltSet1.getLong(4);
long qty_rec = 0;
prpStmt2 = rmtConn.prepareStatement("SELECT A.ORDER_NUM, ORDER_LINE, QUANT_REQ " +
" FROM DRDA.PART_ORDLN A, DRDA.PART_ORDER B " +
" WHERE PART_NUM = ? AND LINE_STAT &lt;&gt; 'C' AND " +
" A.ORDER_NUM = B.ORDER_NUM AND ORDER_TYPE = 'R'");
prpStmt2.setString(1,part_table);
rsltSet2 = prpStmt2.executeQuery();
while (rsltSet2.next()) {
ord_table = rsltSet2.getShort(1);
orl_table = rsltSet2.getShort(2);
long qty_table = rsltSet2.getLong(3);
qty_rec = qty_rec + qty_table;
}
rsltSet2.close();</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>prpStmt2 = rmtConn.prepareStatement("SELECT SUM(QUANT_RECV) FROM DRDA.SHIPMENTLN " +
" WHERE ORDER_LOC = ? AND ORDER_NUM = ? AND " +
" ORDER_LINE = ?");
prpStmt2.setString(1,loc);
prpStmt2.setShort(2,ord_table);
prpStmt2.setShort(3,orl_table);
rsltSet2 = prpStmt2.executeQuery();
rsltSet2.next();
long qty_table = rsltSet2.getLong(1);
qty_rec = qty_rec + qty_table;
rsltSet2.close();
prpStmt2.close();
}
rsltSet1.close();
prpStmt1.close();
} /* end of calculate_order_quantity */
/****************************************************************************/
/* Method For Processing Orders */
/****************************************************************************/
private static void processOrder(Connection rmtConn, String loc) throws SQLException {
PreparedStatement prpStmt1;
ResultSet rsltSet1;
/* insert order and order_line in remote database */
if (contl == 0) {
prpStmt1 = rmtConn.prepareStatement("SELECT (MAX(ORDER_NUM) + 1) FROM DRDA.PART_ORDER");
rsltSet1 = prpStmt1.executeQuery();
rsltSet1.next();
next_num = rsltSet1.getShort(1);
rsltSet1.close();
prpStmt1 = rmtConn.prepareStatement("INSERT INTO DRDA.PART_ORDER (ORDER_NUM, ORIGIN_LOC,
ORDER_TYPE, ORDER_STAT, CREAT_TIME) " +
" VALUES (?, ?, 'R', 'O', CURRENT TIMESTAMP)");
prpStmt1.setShort(1,next_num);
prpStmt1.setString(2,loc);
prpStmt1.executeUpdate();
System.out.println("***** ROP PROCESSING *********");
System.out.println("ORDER NUMBER = " + next_num);
System.out.println("--------------------------------");
System.out.println(" LINE PART QTY ");
System.out.println(" NBR NBR REQUESTED");
System.out.println("--------------------------------");
contl = contl + 1;
} /* if contl == 0 */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre> prpStmt1 = rmtConn.prepareStatement("INSERT INTO DRDA.PART_ORDLN (ORDER_NUM, ORDER_LINE,
PART_NUM, QUANT_REQ, LINE_STAT) " +
" VALUES (?, ?, ?, ?, 'O')");
prpStmt1.setShort(1,next_num);
prpStmt1.setInt(2,contl);
prpStmt1.setString(3,part_table);
prpStmt1.setLong(4,eoq_table);
prpStmt1.executeUpdate();
line_count = line_count + 1;
System.out.println(" " + line_count + " " + part_table + " " + eoq_table + "");
contl = contl + 1;
prpStmt1.close();
} /* end of function processOrder */
/****************************************************************************/
/* Method For Displaying Errors */
/****************************************************************************/
private static void errorFunction(SQLException e, Connection lclConn, Connection rmtConn) {
System.out.println("************************");
System.out.println("* SQL ERROR *");
System.out.println("************************");
System.out.println("SQLCODE = " + e.getErrorCode());
System.out.println("SQLSTATE = " + e.getSQLState());
System.out.println("**********************");
try {
lclConn.rollback();
rmtConn.rollback();
}
catch (SQLException uowErr) {
}
} /* end of function errorFunction */</pre>
</div>
</div>
<div class="example"><div class="fignone"><pre>/****************************************************************************/
/* Mainline */
/****************************************************************************/
public static void main(String[] args) {
String User = "myuser";
String Password = "mypwd";
String lclUrl = null;
String rmtUrl = null;
String loc = "SQLA"; /* dealer's database name */
Connection lclConn = null;
Connection rmtConn = null;
try {
Class.forName(JDBCDriver).newInstance();
}
catch (Exception e) {
System.out.println("Error: Failed to load DB2 driver.");
System.exit(1);
}
try {
lclUrl = "jdbc:db2:" + args[0];
lclConn = DriverManager.getConnection(lclUrl, User, Password);
rmtUrl = "jdbc:db2:" + args[1];
rmtConn = DriverManager.getConnection(rmtUrl, User, Password);
}
catch (Exception e) {
System.out.println("Error: Failed to get database connections.");
System.exit(1);
}
try {
/* Initialization */
resetTables(rmtConn);
/* Main Work */
do {
calculateOrderQuantity(lclConn, rmtConn, loc);
if (rop_table &gt; quant_table) {
processOrder(rmtConn, loc);
quant_table = 0;
}
} while (rop_table != 0);
/* End Work */
lclConn.commit();
rmtConn.commit();
}
catch (SQLException e) {
e.printStackTrace();
errorFunction(e, lclConn, rmtConn);
System.exit(1);
}
}
}</pre>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbal1progxmp.htm" title="This example application for distributed relational database use is written in RPG/400, COBOL/400, Java and ILE C/400 programming languages. This example shows how to use a distributed relational database for functional specification tasks.">Examples: Application programming</a></div>
</div>
</div>
<img src="./deltaend.gif" alt="End of change" /></body>
</html>