4gl から Java に同じ機能を実行するいくつかの問題があります。4gl コードの簡単な説明があります。まずAFPOPF1はテーブルで、IO_AFPOPF1はAFPOPF1の内容を格納する変数です。「FETCH SCUR_1 INTO IO_AFPOPF1.*」ステートメントを使用して、WK_QUERY を実行し、行の結果を IO_AFPOPF1.* に格納します。「FETCH SCUR_1 INTO IO_AFPOPF1.*」を実行した後、カーソルは次の行を参照します。while ループでどのステートメントに IO_AFPOPF1.POLYN1 が含まれているかという 2 番目のクエリを実行します。
DEFINE IO_AFPOPF1 RECORD LIKE AFPOPF1.*
.
.
.
WK_QUERY = "SELECT * FROM AFPOPF1 WHERE PRTDAT='IO_FONLY.PRTDAT'
" AND((FCERNO[2] = 'C' AND LENGTH(FCERNO) = 9)"
" OR (FCERNO[3] = 'C' AND LENGTH(FCERNO) = 10))"
" AND SOLIN1 LIKE '", BS_SOLIN1,"'"
" ORDER BY POLYN1,FCERNO"
.
.
.
PREPARE SSTM_1 FROM WK_QUERY
DECLARE SCUR_1 SCROLL CURSOR
FETCH SCUR_1 INTO IO_AFPOPF1.*
WHILE STATUS = 0
SELECT DEPTWN INTO WK_DEPTWN FROM COMLIB:DEPTPF1
WHERE DEPTNO = IO_AFPOPF1.POLYN1
.
.
.
IF WK_DEPTWN != IO_FONLY.DEPTWN
FETCH SCUR_1 INTO IO_AFPOPF1.*
CONTINUE WHILE
ELSE
LET SW_COUNT = SW_COUNT+1
END IF
.
.
.
FETCH SCUR_1 INTO IO_AFPOPF1.*
END WHILE
ジャバ:
//the class mapping to Afpopf1 Table
public class TableAfpopf1 {
private String fcerno;//var1
private String polyn1;//var2
.
.
.
private Date chkdat;//var58
public void setXXX
.
.
.
}
//database connection anf return resultSet
public class DBConnection {
private Connection connection;
private Statement statement;
private PreparedStatement preS;
private ResultSet resultSet;
private boolean hasRow;
private boolean isReturn;
private String dbURL;
private ArrayList<ArrayList<Object>> allResultList;
private Object returnObj;
public DBConnection() {
this.connection = null;
this.statement = null;
this.preS = null;
this.resultSet = null;
this.hasRow = false;
this.isReturn = false;
this.dbURL = "";
this.allResultList = new ArrayList<ArrayList<Object>>();
}
public void connectDB(String query) {
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
if (query.contains("SELECT"))
resultSet = statement.executeQuery(query);
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
while (resultSet.next()) {
hasRow = true;
if (isReturn) {
if (numberOfColumns == 1) {
resultSet.last();
int rowCount = resultSet.getRow();
resultSet.first();
if (rowCount == 1)
returnObj = resultSet.getObject(1);
}
else {
ArrayList<Object> arrayResultSet = new ArrayList<Object>();
for (int i = 1; i <= numberOfColumns; i++) {
arrayResultSet.add(resultSet.getObject(i));
}
allResultList.add(arrayResultSet);
}
}
else
break;
}
}
catch (SQLException sqlEx) {
while(sqlEx != null) {
System.err.println("SQLException information");
System.err.println("Error msg: " + sqlEx.getMessage());
System.err.println("SQLSTATE: " + sqlEx.getSQLState());
System.err.println("Error code: " + sqlEx.getErrorCode());
sqlEx.printStackTrace();
sqlEx = sqlEx.getNextException();
}
}
finally {
try {
resultSet.close();
statement.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void preQueryConnectDB(String query,String expression) {
try {
preS = connection.prepareStatement(query,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
preS.setString(1, query);
resultSet = preS.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
while (resultSet.next()) {
hasRow = true;
if (isReturn) {
if (numberOfColumns == 1) {
resultSet.last();
int rowCount = resultSet.getRow();
resultSet.first();
if (rowCount == 1)
returnObj = resultSet.getObject(1);
}
else {
ArrayList<Object> arrayResultSet = new ArrayList<Object>();
for (int i = 1; i <= numberOfColumns; i++) {
arrayResultSet.add(resultSet.getObject(i));
}
allResultList.add(arrayResultSet);
}
}
else
break;
}
}catch (SQLException sqlEx) {
while(sqlEx != null) {
System.err.println("SQLException information");
System.err.println("Error msg: " + sqlEx.getMessage());
System.err.println("SQLSTATE: " + sqlEx.getSQLState());
System.err.println("Error code: " + sqlEx.getErrorCode());
sqlEx.printStackTrace();
sqlEx = sqlEx.getNextException();
}
}
finally {
try {
resultSet.close();
preS.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void closeDBConnection() {
try {
connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void setIsReturnData(boolean isReturn) { this.isReturn = isReturn; }
public void setConnectionURL(String query) {
String dbName = "";
String newURL = "";
int beginIndex = -1; //string from in index is positive
int endIndex = -1; //string : in index is positive
beginIndex = query.indexOf("FROM");
endIndex = query.indexOf(":");
dbName = query.substring(beginIndex+4, endIndex).trim();
newURL = "jdbc:informix-sqli://hwae5500:shme5500/"+dbName+":INFORMIXSERVER=shme5500;";
if (!newURL.equals(dbURL)) {
dbURL = newURL;
try {
if (connection != null)
connection.close();
Class.forName("com.informix.jdbc.IfxDriver");
connection = DriverManager.getConnection(dbURL);
}catch (SQLException e) {
e.printStackTrace();
}
catch(ClassNotFoundException drvEx) {
System.out.println("Driver fail");
drvEx.printStackTrace();
}
}
}
public void recoverDefultValue() {
this.statement = null;
this.preS = null;
this.resultSet = null;
this.hasRow = false;
this.isReturn = false;
this.returnObj = null;
}
public boolean getHasRow() { return hasRow; }
public Object getReturnObj(){ return returnObj; }
//get all resultSet in Object type
public ArrayList<ArrayList<Object>> getAllResultList() { return allResultList; }
}
//the class receive query result and process data
pubblic class DataProcessing {
.
.
.
public void generateInformation() {
.
.
.
sqlQuery = "SELECT * FROM AFFIL:AFPOPF1 WHERE PRTDAT="+"'"+dateStr+"'"+
"AND FCERNO[2]='C' AND LENGTH(FCERNO)=9 "+
"AND SOLIN1 LIKE '"+bs_SOLIN1+"' ORDER BY POLYN1,FCERNO";
db.recoverDefultValue();
db.setIsReturnData(true);
db.setConnectionURL(sqlQuery);
db.connectDB(sqlQuery);
if (db.getHasRow()) {
setDataList(db.getAllResultList(),1);//Object type to Afpopf1 type
sw_DATA = true;
}
.
.
.
if (codeStr.charAt(0) == '0') {
sqlQuery = "SELECT DEPTWN FROM COMLIB:DEPTPF1 WHERE DEPTNO=?";
db.recoverDefultValue();
db.setIsReturnData(true);
db.setConnectionURL(sqlQuery);
for (TableAfpopf1 tafp : Afpopf1List) {
db.preQueryConnectDB(sqlQuery,tafp.getPolyn1());
wk_DEPTWN = setVarwk_DEPTWN((String)db.getReturnObj());
}
}
.
.
.
}
private void setDataList(ArrayList<ArrayList<Object>> allResultList,int choiceNum)
{
int inputindex = 0;
DateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
if (choiceNum == 1) {
for (int i=0; i<allResultList.size(); i++) {
TableAfpopf1 afpItem = new TableAfpopf1();
for (Object obj : allResultList.get(i)) {
switch (inputindex+1) {
case 1:
afpItem.setFcerno((String)obj);
break;
case 2:
afpItem.setPolyn1((String)obj);
break;
.
.
.
case 58:
try {
Date date = sdf.parse(obj.toString());
afpItem.setChkdat(date);
}
catch (Exception e) {
System.out.println("error appear");
e.printStackTrace();
}
break;
}
inputindex++;
}
Afpopf1List.add(afpItem);
}
}
else if (choiceNum == 2) {
}
}
}
setDataList() と setDataList() は良い方法ではないと思います。Javaでプログラムを実装すると、setDataList()メソッドがAfpopf1にしかマッピングされず、DBテーブルの各変数にマッピングするケースを書く。次に、4gl プログラムに新しいテーブル変数がある場合、そのテーブルがデータを受け取る別のメソッドを作成する必要があります。
それは良い考えではないと思うので、Javaコードを改善する方法を知りたい、またはJavaで関数を実装する正しい方法を教えてください。Java EE には触れたことがありません。
解決策が Java EE に関するものである場合、回答が詳細であるか、サンプル コードが含まれていることを願っています。