I have some problems that do the same function from 4gl to java. There are some brief explanation to 4gl code. First,AFPOPF1 is table,and IO_AFPOPF1 is variable that store contents of AFPOPF1. Excuting WK_QUERY and store row result to IO_AFPOPF1.* using 'FETCH SCUR_1 INTO IO_AFPOPF1.*' statement. After running 'FETCH SCUR_1 INTO IO_AFPOPF1.*',the cursor refer to next row. Excuting 2nd query that which statement contains IO_AFPOPF1.POLYN1 in while loop.
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
java:
//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) {
}
}
}
I think setDataList() and setDataList() are not good methods. When I implement a program in java, the setDataList() method only map to Afpopf1, and I write case to map each variable of DB table.It isn't effective code and stupid. Then, if a 4gl program has a new table variable, I must write another method for that table to receive the data.
I don't think that is a good idea, so I want to know how to improve the java code, or tell me the correct way to implement the function in java. I haven't touched Java EE before.
If the solution is about Java EE, I hope the answer is in detail or includes some example code.
You'll probably be disappointed, but this is how it's actually done, for example using Spring JDBC template: every SQL query has hand-coded mapping into an appropriate bean object.
If you want it done automatically, you should consider using an ORM such as Hibernate instead of the raw JDBC.
See this:
http://www.codeproject.com/Tips/372152/Mapping-JDBC-ResultSet-to-Object-using-Annotations
You need to do Enity class for apply ResultSetMapper
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.