我遇到了一些从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。
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) {
}
}
}
我认为setDataList()和setDataList()不是好方法。 当我在java中实现一个程序时,setDataList()方法只映射到Afpopf1, 我写了一个案例来映射DB表的每个变量。它不是有效的代码和愚蠢的。 然后,如果4gl程序有一个新的表变量,我必须为该表编写另一个方法来接收数据。
我认为这不是一个好主意,所以我想知道如何改进java代码, 或告诉我在java中实现该函数的正确方法。 我之前没有接触过Java EE。
如果解决方案是关于Java EE的,我希望答案是详细的,或者包含一些示例代码。
答案 0 :(得分:4)
你可能会感到失望,但这就是它实际完成的方式,例如使用Spring JDBC模板:每个SQL查询都有手工编码映射到适当的bean对象。
如果您希望自动完成,则应考虑使用ORM(如Hibernate)而不是原始JDBC。
答案 1 :(得分:1)
见:
http://www.codeproject.com/Tips/372152/Mapping-JDBC-ResultSet-to-Object-using-Annotations
您需要执行Entity类以应用ResultSetMapper