1 package com.rapide_act;
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
8 import java.io.FileWriter;
9 import java.io.BufferedWriter;
10 import java.io.PrintWriter;
11 import java.io.ByteArrayOutputStream;
12 import java.sql.Connection;
13 import java.sql.DriverManager;
14 import java.sql.ResultSet;
15 import java.sql.ResultSetMetaData;
16 import java.sql.SQLException;
17 import java.sql.PreparedStatement;
19 import java.sql.Timestamp;
22 import java.io.PrintWriter;
23 import java.util.Date;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.math.BigDecimal;
28 public class DataAccessObjects{
30 private String driver = null;
31 private String user = null;
32 private String pass = null;
33 private String url = null;
34 private Connection conn = null;
35 private PreparedStatement stmt = null;
36 private ResultSet rst = null;
37 private ResultSetMetaData rsmd = null;
38 private int ColumnCount = 0;
39 private ArrayList<String> alColName = null;
40 private ArrayList<String> alColTypeName = null;
41 private ArrayList<Integer> alIsNullable = null;
42 private ArrayList<Boolean> alColMask = null;
43 private ArrayList<Boolean> alColIncl = null;
44 private ArrayList<String> alData = null;
45 private static String sql="select sysdate from dual";
46 private String[] colTypeName = null;
47 private String[] colName = null;
48 private Integer[] isNullable = null;
49 private Boolean [] colMask = null;
50 private Boolean [] colIncl = null;
52 private String colString = null;
53 private BigDecimal colBigDecimal = null;
54 private Integer colInt = null;
55 private Short colShort = null;
56 private Float colFloat = null;
57 private Double colDouble = null;
58 private java.sql.Timestamp colTimestamp = null;
59 private java.sql.Time colTime = null;
60 private java.sql.Date colDate = null;
61 private byte [] colBytes = null;
62 private Blob colBlob = null;
63 private Clob colClob = null;
64 private int recCount = 0;
65 private InputStream is = null;
66 private ByteArrayOutputStream baos = null;
67 private boolean isQuotation = true;
68 private boolean isMask = false;
69 private boolean isInclMaskCols = false;
70 private boolean isExclMaskCols = false;
71 private String inclMaskCols = null;
72 private String exclMaskCols = null;
73 private String tbName = null;
74 private String nullMark = "";
75 private String lnSprtr = System.getProperty("line.separator");
76 private char singleByteChr = '*';
77 private char doubleByteChr = '
\81\96';
78 private String maskPtn = "ALT";
79 private static final int MASK_PTN_ALL = 0;
80 private static final int MASK_PTN_ALT = 1;
81 private static final int MASK_PTN_EDGE = 2;
82 private static final String [] MASK_PTN = {"ALL", "ALT", "EDGE"};
83 private static final int USER = 0;
84 private static final int PASS = 1;
85 private static final int URL = 2;
86 private static final int DRIVER = 3;
87 private static final int OUTPUT = 4;
88 private static final int INPUT = 5;
89 private static final int LINESEP = 6;
90 private static final int INLINESEP = 7;
91 private static final int FILENCODE = 8;
92 private static final int NULLMARK = 9;
93 private static final int DELIMITER = 10;
94 private static final int TBLSQL = 11;
95 private static final int TBLKEYSQL = 12;
96 private static final int ORDER = 13;
97 private static final int QUOTATION = 14;
98 private static final int BATCOUNT = 15;
99 private static final int INCLTAB = 16;
100 private static final int EXCLTAB = 17;
101 private static final int INCLCOL = 18;
102 private static final int EXCLCOL = 19;
103 private static final int INCLCOLMASK = 20;
104 private static final int EXCLCOLMASK = 21;
105 private static final int SBYTEMASKCHR = 22;
106 private static final int DBYTEMASKCHR = 23;
107 private static final int MASKPTN = 24;
108 private static final int PROPFILE = 25;
109 private static final String [] PROP_KEY = { "us","pw","url","dr","out","in","ls","ils","fe","nm",
110 "dm","ts","tks","od","qt","bc","it","et","ic","ec",
111 "icm","ecm","smc","dmc","mp","pf"};
112 private static final int KEYS = 26;
113 private String [] prop_val = null;
114 DataAccessObjects(String _user, String _pass, String _url, String _driver){
121 public Connection connect() throws Exception{
124 CmnUtils.debugPrint("Start Driver class loading");
125 Class.forName (driver);
126 CmnUtils.debugPrint("End Driver class loading");
127 CmnUtils.debugPrint("Start Connect Database>" + url + "," + user);
128 conn = DriverManager.getConnection(url, user, pass);
129 CmnUtils.debugPrint("End Connect Database");
133 catch (ClassNotFoundException e) {
136 catch (IOException e) {
144 public PreparedStatement prepareSql(String _sql) throws Exception{
146 CmnUtils.debugPrint("Start prepared Statement");
147 CmnUtils.debugPrint(_sql);
148 stmt = conn.prepareStatement(_sql);
149 CmnUtils.debugPrint("End prepared Statement");
152 catch (SQLException e) {
160 public void executeSql() throws Exception{
164 CmnUtils.debugPrint("Start get RecordSet");
166 for(int i=0;i<ColumnCount;i++){
167 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
168 if (colString != null) {
169 alData.add(colString);
175 CmnUtils.debugPrint("End get RecordSet");
177 catch (SQLException e) {
195 public void executeSql(PreparedStatement _stmt) throws Exception{
198 CmnUtils.debugPrint("Start execute Query");
199 rst = _stmt.executeQuery();
200 CmnUtils.debugPrint("End execute Query");
202 CmnUtils.debugPrint("Start get MetaData");
203 rsmd = rst.getMetaData();
204 CmnUtils.debugPrint("End get MetaData");
206 CmnUtils.debugPrint("Start get ColumnCount");
207 ColumnCount = rsmd.getColumnCount();
208 CmnUtils.debugPrint("End get ColumnCount," + ColumnCount);
209 alColTypeName = new ArrayList<String>();
210 alColName = new ArrayList<String>();
211 alIsNullable = new ArrayList<Integer>();
212 alData = new ArrayList<String>();
213 for(int i=0;i<ColumnCount;i++){
214 alColTypeName.add(rsmd.getColumnTypeName(i + 1));
215 alColName.add(rsmd.getColumnName(i + 1));
216 alIsNullable.add(rsmd.isNullable(i + 1));
218 colTypeName = (String[])alColTypeName.toArray(new String[0]);
219 colName = (String[])alColName.toArray(new String[0]);
220 isNullable = (Integer[])alIsNullable.toArray(new Integer[0]);
223 catch (SQLException e) {
231 public void commit() throws Exception{
232 CmnUtils.debugPrint("Start commit");
234 CmnUtils.debugPrint("End commit");
237 public void rollback() throws Exception{
238 CmnUtils.debugPrint("Start rollback");
240 CmnUtils.debugPrint("End rollback");
243 public void closeRecordSet() throws Exception{
254 public void select(String _sql) throws Exception{
255 PreparedStatement stmt;
256 stmt = prepareSql(_sql);
262 public int getColumnCount() throws Exception{
266 public int getRecCount() throws Exception{
270 public ArrayList<String> getArrayList() throws Exception{
274 public ArrayList<String> getArrayColumnNameList() throws Exception{
278 public ArrayList<String> getArrayColumnTypeNameList() throws Exception{
279 return alColTypeName;
282 public ArrayList<Integer> getArrayIsNullableList() throws Exception{
286 public void setArrayColumnMaskList(ArrayList<Boolean> _alColMask) throws Exception{
287 alColMask = _alColMask;
288 colMask = (Boolean[])alColMask.toArray(new Boolean[0]);
291 public void setArrayColumnInclList(ArrayList<Boolean> _alColIncl) throws Exception{
292 alColIncl = _alColIncl;
293 colIncl = (Boolean[])alColIncl.toArray(new Boolean[0]);
296 public void setTableName(String _tbName) throws Exception{
300 public void setPropVal(String[] _prop_val) throws Exception{
301 prop_val = _prop_val;
304 public ResultSet getResultSet() throws Exception{
308 public void getRecordToArray() throws Exception{
310 CmnUtils.debugPrint("Start get RecordSet to Array");
313 for(int i=0;i<ColumnCount;i++){
314 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
315 if (colString != null) {
316 alData.add(colString);
322 CmnUtils.debugPrint("End get RecordSet to Array");
324 catch (SQLException e) {
332 public void getRecordToPrint(PrintWriter _pw, String _splt, boolean _isQuotation, boolean _isMask, int _firstCol, int _lastCol) throws Exception{
334 CmnUtils.debugPrint("Start get Record to Print");
335 isQuotation = _isQuotation;
338 if(!CmnUtils.isEmpty(prop_val[LINESEP]))lnSprtr = CmnUtils.getLineSeparator(prop_val[LINESEP]);
339 if(!CmnUtils.isEmpty(prop_val[NULLMARK]))nullMark = prop_val[NULLMARK];
343 for(int i=0;i<ColumnCount;i++){
345 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], colMask[i]);
347 if (colString != null) {
350 if (_firstCol == _lastCol){
351 _pw.print("\"" + colString + "\"" + lnSprtr);
353 _pw.print("\"" + colString);
356 if (_firstCol == _lastCol){
357 _pw.print(colString + lnSprtr);
359 _pw.print(colString);
363 } else if (i == _lastCol){
365 _pw.print("\"" + _splt + "\"" + colString + "\"" + lnSprtr);
367 _pw.print(_splt + colString + lnSprtr);
371 _pw.print("\"" + _splt + "\"" + colString);
373 _pw.print(_splt + colString);
382 if (_firstCol == _lastCol){
383 _pw.print("\"" + nullMark + "\"" + lnSprtr);
385 _pw.print("\"" + nullMark);
388 if (_firstCol == _lastCol){
389 _pw.print(nullMark + lnSprtr);
394 } else if (i == _lastCol){
396 _pw.print("\"" + _splt + "\"" + nullMark + "\"" + lnSprtr);
398 _pw.print(_splt + nullMark + lnSprtr);
402 _pw.print("\"" + _splt + "\"" + nullMark);
404 _pw.print(_splt + nullMark);
412 CmnUtils.debugPrint("End get Record to Print");
414 catch (SQLException e) {
422 private String getColString(ResultSet _rst, int _rec_cnt, String _colTypeName, Integer _isNullable, Boolean _colMask) throws Exception{
423 String colString = null;
424 java.sql.Timestamp colTimestamp = null;
425 java.sql.Time colTime = null;
426 java.sql.Date colDate = null;
427 BigDecimal colBigDecimal = null;
428 Integer colInt = null;
429 Short colShort = null;
430 Float colFloat = null;
431 Double colDouble = null;
432 byte [] colBytes = null;
435 ByteArrayOutputStream baos = null;
436 InputStream is = null;
437 if(!CmnUtils.isEmpty(prop_val[QUOTATION]) && prop_val[QUOTATION].toUpperCase().equals("N"))isQuotation = false;
438 if(!CmnUtils.isEmpty(prop_val[INCLCOLMASK]))isInclMaskCols = true;
439 if(!CmnUtils.isEmpty(prop_val[EXCLCOLMASK]))isExclMaskCols = true;
440 if(isInclMaskCols || isExclMaskCols)isMask = true;
441 if(!CmnUtils.isEmpty(prop_val[SBYTEMASKCHR]))singleByteChr = prop_val[SBYTEMASKCHR].charAt(0);
442 if(!CmnUtils.isEmpty(prop_val[DBYTEMASKCHR]))doubleByteChr = prop_val[DBYTEMASKCHR].charAt(0);
443 if(!CmnUtils.isEmpty(prop_val[MASKPTN]))maskPtn = prop_val[MASKPTN];
445 if (CmnUtils.isColString(_colTypeName)) {
446 colString = _rst.getString(_rec_cnt);
447 if (colString != null) {
449 colString =colString.trim().replaceAll("\0","").replaceAll("\"","\"\"");
451 colString =colString.trim().replaceAll("\0","");
455 StringBuffer sb = new StringBuffer();
456 for(int j=0;j<colString.length();j++){
457 if((maskPtn.toUpperCase().equals(MASK_PTN[MASK_PTN_ALT]) &&
459 (maskPtn.toUpperCase().equals(MASK_PTN[MASK_PTN_EDGE]) &&
460 !(j == 0 || j == colString.length() - 1) ||
461 (maskPtn.toUpperCase().equals(MASK_PTN[MASK_PTN_ALL]))
464 if(CmnUtils.isHankaku(colString.charAt(j))){
465 sb.append(singleByteChr);
467 sb.append(doubleByteChr);
470 sb.append(colString.charAt(j));
473 colString = sb.toString();
476 if(colString.equals(""))colString = " ";
478 } else if (CmnUtils.isColDate(_colTypeName)) {
479 colDate = _rst.getDate(_rec_cnt);
480 if (colDate != null) {
481 colString = colDate.toString();
483 } else if (CmnUtils.isColTimestamp(_colTypeName)) {
484 colTimestamp = _rst.getTimestamp(_rec_cnt);
485 if (colTimestamp != null) {
486 colString = colTimestamp.toString();
488 } else if (CmnUtils.isColTime(_colTypeName)) {
489 colTime = _rst.getTime(_rec_cnt);
490 if (colTime != null) {
491 colString = colTime.toString();
493 } else if (CmnUtils.isColBigDecimal(_colTypeName)) {
494 colBigDecimal = _rst.getBigDecimal(_rec_cnt);
495 if (colBigDecimal != null) {
496 colString = colBigDecimal.toString();
498 } else if (CmnUtils.isColShort(_colTypeName)) {
499 colShort = _rst.getShort(_rec_cnt);
500 if (colShort != null) {
501 colString = colShort.toString();
503 } else if (CmnUtils.isColInt(_colTypeName)) {
504 colInt = _rst.getInt(_rec_cnt);
505 if (colInt != null) {
506 colString = colInt.toString();
508 } else if (CmnUtils.isColFloat(_colTypeName)) {
509 colFloat = _rst.getFloat(_rec_cnt);
510 if (colFloat != null) {
511 colString = colFloat.toString();
513 } else if (CmnUtils.isColDouble(_colTypeName)) {
514 colDouble = _rst.getDouble(_rec_cnt);
515 if (colDouble != null) {
516 colString = colDouble.toString();
518 } else if (CmnUtils.isColBytes(_colTypeName)) {
519 colBytes = _rst.getBytes(_rec_cnt);
520 if (colBytes != null) {
521 colString = CmnUtils.bytesToBase64(colBytes);
523 } else if (CmnUtils.isColBlob(_colTypeName)) {
524 colBlob = _rst.getBlob(_rec_cnt);
525 if (colBlob != null) {
526 is = colBlob.getBinaryStream();
527 baos = new ByteArrayOutputStream();
529 colBytes = new byte[8192];
530 while ((len = is.read(colBytes)) != -1) {
531 baos.write(colBytes,0,len);
533 colBytes = baos.toByteArray();
534 colString = CmnUtils.bytesToBase64(colBytes);
540 } else if (CmnUtils.isColClob(_colTypeName)) {
541 colClob = _rst.getClob(_rec_cnt);
542 if (colClob != null) {
543 colString = CmnUtils.clobToString(colClob);
546 CmnUtils.errorPrint("unmatch column type=" + _colTypeName);
547 throw new Exception("unmatch column type=" + _colTypeName);
553 public void disconnect() throws Exception{
556 CmnUtils.debugPrint("Start disconnect database");
558 CmnUtils.debugPrint("End disconnect database");
561 catch (SQLException e) {