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.BigInteger;
27 import java.math.BigDecimal;
28 import java.nio.charset.Charset;
30 public class DataAccessObjects{
32 private String driver = null;
33 private String user = null;
34 private String pass = null;
35 private String url = null;
36 private Connection conn = null;
37 private PreparedStatement stmt = null;
38 private ResultSet rst = null;
39 private ResultSetMetaData rsmd = null;
40 private int ColumnCount = 0;
41 private ArrayList<String> alColName = null;
42 private ArrayList<String> alColTypeName = null;
43 private ArrayList<Integer> alColPrecision = null;
44 private ArrayList<Integer> alIsNullable = null;
45 private ArrayList<Boolean> alColMask = null;
46 private ArrayList<Boolean> alColIncl = null;
47 private ArrayList<String> alData = null;
48 private static String sql="select sysdate from dual";
49 private String[] colTypeName = null;
50 private String[] colName = null;
51 private Integer[] colPrecision = null;
52 private Integer[] isNullable = null;
53 private Boolean [] colMask = null;
54 private Boolean [] colIncl = null;
56 private String colString = null;
57 private BigDecimal colBigDecimal = null;
58 private Integer colInt = null;
59 private Long colLong = null;
60 private Short colShort = null;
61 private Float colFloat = null;
62 private Double colDouble = null;
63 private Boolean colBoolean = null;
64 private java.sql.Timestamp colTimestamp = null;
65 private java.sql.Time colTime = null;
66 private java.sql.Date colDate = null;
67 private java.sql.Date colYear = null;
68 private byte [] colBytes = null;
69 private Blob colBlob = null;
70 private Clob colClob = null;
71 private String tbName = null;
72 private int recCount = 0;
73 private CmnProperty cp = null;
75 DataAccessObjects(String _user, String _pass, String _url, String _driver){
82 DataAccessObjects(CmnProperty _cp){
90 protected Connection connect() throws Exception{
93 CmnUtils.debugPrint("Start Driver class loading");
94 Class.forName (driver);
95 CmnUtils.debugPrint("End Driver class loading");
96 CmnUtils.debugPrint("Start Connect Database>" + url + "," + user);
97 conn = DriverManager.getConnection(url, user, pass);
98 CmnUtils.debugPrint("End Connect Database");
102 catch (ClassNotFoundException e) {
105 catch (IOException e) {
113 protected PreparedStatement prepareSql(String _sql) throws Exception{
115 CmnUtils.debugPrint("Start prepared Statement");
116 CmnUtils.debugPrint(_sql);
117 stmt = conn.prepareStatement(_sql);
118 CmnUtils.debugPrint("End prepared Statement");
121 catch (SQLException e) {
129 protected void executeSql() throws Exception{
133 CmnUtils.debugPrint("Start get RecordSet");
135 for(int i=0;i<ColumnCount;i++){
136 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
137 if (colString != null) {
138 alData.add(colString);
144 CmnUtils.debugPrint("End get RecordSet");
146 catch (SQLException e) {
164 protected void executeSql(PreparedStatement _stmt) throws Exception{
167 CmnUtils.debugPrint("Start execute Query");
168 rst = _stmt.executeQuery();
169 CmnUtils.debugPrint("End execute Query");
171 CmnUtils.debugPrint("Start get MetaData");
172 rsmd = rst.getMetaData();
173 CmnUtils.debugPrint("End get MetaData");
175 CmnUtils.debugPrint("Start get ColumnCount");
176 ColumnCount = rsmd.getColumnCount();
177 CmnUtils.debugPrint("End get ColumnCount," + ColumnCount);
178 alColTypeName = new ArrayList<String>();
179 alColName = new ArrayList<String>();
180 alColPrecision = new ArrayList<Integer>();
181 alIsNullable = new ArrayList<Integer>();
182 alData = new ArrayList<String>();
183 for(int i=0;i<ColumnCount;i++){
184 alColTypeName.add(rsmd.getColumnTypeName(i + 1));
185 alColName.add(rsmd.getColumnName(i + 1));
186 alColPrecision.add(rsmd.getPrecision(i + 1));
187 alIsNullable.add(rsmd.isNullable(i + 1));
189 colTypeName = (String[])alColTypeName.toArray(new String[0]);
190 colName = (String[])alColName.toArray(new String[0]);
191 colPrecision = (Integer[])alColPrecision.toArray(new Integer[0]);
192 isNullable = (Integer[])alIsNullable.toArray(new Integer[0]);
195 catch (SQLException e) {
203 protected void commit() throws Exception{
204 CmnUtils.debugPrint("Start commit");
206 CmnUtils.debugPrint("End commit");
209 protected void rollback() throws Exception{
210 CmnUtils.debugPrint("Start rollback");
212 CmnUtils.debugPrint("End rollback");
215 protected void closeRecordSet() throws Exception{
226 protected void select(String _sql) throws Exception{
227 PreparedStatement stmt;
228 stmt = prepareSql(_sql);
234 protected int getColumnCount() throws Exception{
238 protected int getRecCount() throws Exception{
242 protected ArrayList<String> getArrayList() throws Exception{
246 protected ArrayList<String> getArrayColumnNameList() throws Exception{
250 protected ArrayList<String> getArrayColumnTypeNameList() throws Exception{
251 return alColTypeName;
254 protected ArrayList<Integer> getArrayIsNullableList() throws Exception{
258 protected ArrayList<Integer> getArrayPrecisionList() throws Exception{
259 return alColPrecision;
262 protected void setArrayColumnMaskList(ArrayList<Boolean> _alColMask) throws Exception{
263 alColMask = _alColMask;
264 colMask = (Boolean[])alColMask.toArray(new Boolean[0]);
267 protected void setArrayColumnInclList(ArrayList<Boolean> _alColIncl) throws Exception{
268 alColIncl = _alColIncl;
269 colIncl = (Boolean[])alColIncl.toArray(new Boolean[0]);
272 protected void setTableName(String _tbName) throws Exception{
276 protected ResultSet getResultSet() throws Exception{
280 protected void getRecordToArray() throws Exception{
282 CmnUtils.debugPrint("Start get RecordSet to Array");
285 for(int i=0;i<ColumnCount;i++){
286 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
287 CmnUtils.debugPrint("col=" + colString);
288 if (colString != null) {
289 alData.add(colString);
295 CmnUtils.debugPrint("End get RecordSet to Array");
297 catch (SQLException e) {
305 protected void getRecordToPrint(PrintWriter _pw, int _firstCol, int _lastCol) throws Exception{
307 CmnUtils.debugPrint("Start get Record to Print");
312 for(int i=0;i<ColumnCount;i++){
314 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], colMask[i]);
316 if (colString != null) {
317 if(CmnUtils.isFixString(colTypeName[i]) && cp.isSpcPadding)colString = colString + CmnUtils.padSpace(colString, colPrecision[i], Charset.forName(cp.fileEncoding));
320 if (_firstCol == _lastCol){
321 _pw.print("\"" + colString + "\"" + cp.lineSeparator);
323 _pw.print("\"" + colString);
326 if (_firstCol == _lastCol){
327 _pw.print(colString + cp.lineSeparator);
329 _pw.print(colString);
333 } else if (i == _lastCol){
335 _pw.print("\"" + cp.delimiter + "\"" + colString + "\"" + cp.lineSeparator);
337 _pw.print(cp.delimiter + colString + cp.lineSeparator);
341 _pw.print("\"" + cp.delimiter + "\"" + colString);
343 _pw.print(cp.delimiter + colString);
352 if (_firstCol == _lastCol){
353 _pw.print("\"" + cp.nullMark + "\"" + cp.lineSeparator);
355 _pw.print("\"" + cp.nullMark);
358 if (_firstCol == _lastCol){
359 _pw.print(cp.nullMark + cp.lineSeparator);
361 _pw.print(cp.nullMark);
364 } else if (i == _lastCol){
366 _pw.print("\"" + cp.delimiter + "\"" + cp.nullMark + "\"" + cp.lineSeparator);
368 _pw.print(cp.delimiter + cp.nullMark + cp.lineSeparator);
372 _pw.print("\"" + cp.delimiter + "\"" + cp.nullMark);
374 _pw.print(cp.delimiter + cp.nullMark);
382 CmnUtils.debugPrint("End get Record to Print");
384 catch (SQLException e) {
392 private String getColString(ResultSet _rst, int _rec_cnt, String _colTypeName, Integer _isNullable, Boolean _colMask) throws Exception{
398 colBigDecimal = null;
408 ByteArrayOutputStream baos = null;
409 InputStream is = null;
411 if (CmnUtils.isColString(_colTypeName)) {
412 colString = _rst.getString(_rec_cnt);
413 if (colString != null) {
415 colString =colString.trim().replaceAll("\0","").replaceAll("\"","\"\"");
417 colString =colString.trim().replaceAll("\0","");
421 StringBuffer sb = new StringBuffer();
422 for(int j=0;j<colString.length();j++){
423 if((cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALT]) &&
425 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_EDGE]) &&
426 !(j == 0 || j == colString.length() - 1) ||
427 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALL]))
430 if(CmnUtils.isHankaku(colString.charAt(j))){
431 sb.append(cp.singleByteMaskChar);
433 sb.append(cp.doubleByteMaskChar);
436 sb.append(colString.charAt(j));
439 colString = sb.toString();
442 if(colString.equals(""))colString = " ";
444 } else if (CmnUtils.isColDate(_colTypeName)) {
445 colDate = _rst.getDate(_rec_cnt);
446 if (colDate != null) {
447 colString = colDate.toString();
449 } else if (CmnUtils.isColYear(_colTypeName)) {
450 colYear = _rst.getDate(_rec_cnt);
451 if (colYear != null) {
452 colString = colYear.toString().substring(0, 4);
454 } else if (CmnUtils.isColTimestamp(_colTypeName)) {
455 colTimestamp = _rst.getTimestamp(_rec_cnt);
456 if (colTimestamp != null) {
457 colString = colTimestamp.toString();
459 } else if (CmnUtils.isColTime(_colTypeName)) {
460 colTime = _rst.getTime(_rec_cnt);
461 if (colTime != null) {
462 colString = colTime.toString();
464 } else if (CmnUtils.isColBigDecimal(_colTypeName)) {
465 colBigDecimal = _rst.getBigDecimal(_rec_cnt);
466 if (colBigDecimal != null) {
467 colString = colBigDecimal.toString();
469 } else if (CmnUtils.isColBoolean(_colTypeName)) {
470 colBoolean = _rst.getBoolean(_rec_cnt);
471 if (colBoolean != null) {
472 colString = colBoolean.toString();
474 } else if (CmnUtils.isColShort(_colTypeName)) {
475 colShort = _rst.getShort(_rec_cnt);
476 if (colShort != null) {
477 colString = colShort.toString();
479 } else if (CmnUtils.isColInt(_colTypeName)) {
480 colInt = _rst.getInt(_rec_cnt);
481 if (colInt != null) {
482 colString = colInt.toString();
484 } else if (CmnUtils.isColLong(_colTypeName)) {
485 colLong = _rst.getLong(_rec_cnt);
486 if (colLong != null) {
487 colString = colLong.toString();
489 } else if (CmnUtils.isColFloat(_colTypeName)) {
490 colFloat = _rst.getFloat(_rec_cnt);
491 if (colFloat != null) {
492 colString = colFloat.toString();
494 } else if (CmnUtils.isColDouble(_colTypeName)) {
495 colDouble = _rst.getDouble(_rec_cnt);
496 if (colDouble != null) {
497 colString = colDouble.toString();
499 } else if (CmnUtils.isColBytes(_colTypeName)) {
500 colBytes = _rst.getBytes(_rec_cnt);
501 if (colBytes != null) {
502 colString = CmnUtils.bytesToBase64(colBytes);
504 } else if (CmnUtils.isColBlob(_colTypeName)) {
505 colBlob = _rst.getBlob(_rec_cnt);
506 if (colBlob != null) {
507 is = colBlob.getBinaryStream();
508 baos = new ByteArrayOutputStream();
510 colBytes = new byte[8192];
511 while ((len = is.read(colBytes)) != -1) {
512 baos.write(colBytes,0,len);
514 colBytes = baos.toByteArray();
515 colString = CmnUtils.bytesToBase64(colBytes);
521 } else if (CmnUtils.isColClob(_colTypeName)) {
522 colClob = _rst.getClob(_rec_cnt);
523 if (colClob != null) {
524 colString = CmnUtils.clobToString(colClob);
527 throw new Exception("
\83J
\83\89\83\80\82Ì
\83f
\81[
\83^
\8c^
\82ª
\91Î
\8fÛ
\8aO
\82Å
\82·
\81B[" + _colTypeName + "]");
532 protected void setColString(PreparedStatement stmt_ins, String [] colData, int ins_col_count, int [] tb_col_seq, int [] fl_col_seq) throws Exception{
534 if (CmnUtils.isColString(colTypeName[tb_col_seq[ins_col_count]])){
537 (cp.isInclColsMask &&
538 !cp.isExclColsMask &&
539 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[ins_col_count]].toUpperCase())
541 (cp.isInclColsMask &&
543 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[ins_col_count]].toUpperCase()) &&
544 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[ins_col_count]].toUpperCase())
546 (!cp.isInclColsMask &&
548 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[ins_col_count]].toUpperCase())
551 StringBuffer sb = new StringBuffer();
552 for(int l=0;l<colData[fl_col_seq[ins_col_count]].length();l++){
553 if((cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALT]) &&
555 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_EDGE]) &&
556 !(l == 0 || ins_col_count == colData[fl_col_seq[ins_col_count]].length() - 1) ||
557 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALL]))
560 if(CmnUtils.isHankaku(colData[fl_col_seq[ins_col_count]].charAt(l))){
561 sb.append(cp.singleByteMaskChar);
563 sb.append(cp.doubleByteMaskChar);
566 sb.append(colData[fl_col_seq[ins_col_count]].charAt(l));
569 colData[fl_col_seq[ins_col_count]] = sb.toString();
572 if (!colData[fl_col_seq[ins_col_count]].equals("")){
574 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]].replaceAll("\"\"","\""));
576 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
579 stmt_ins.setString(ins_col_count+1,null);
581 } else if (CmnUtils.isColDate(colTypeName[tb_col_seq[ins_col_count]])){
582 if (!colData[fl_col_seq[ins_col_count]].equals("")){
583 stmt_ins.setDate(ins_col_count+1,java.sql.Date.valueOf(colData[fl_col_seq[ins_col_count]]));
585 stmt_ins.setDate(ins_col_count+1,null);
587 } else if (CmnUtils.isColYear(colTypeName[tb_col_seq[ins_col_count]])){
588 if (!colData[fl_col_seq[ins_col_count]].equals("")){
589 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
591 stmt_ins.setDate(ins_col_count+1,null);
593 } else if (CmnUtils.isColTime(colTypeName[tb_col_seq[ins_col_count]])){
594 if (!colData[fl_col_seq[ins_col_count]].equals("")){
595 stmt_ins.setTime(ins_col_count+1,java.sql.Time.valueOf(colData[fl_col_seq[ins_col_count]]));
597 stmt_ins.setTime(ins_col_count+1,null);
599 } else if (CmnUtils.isColTimestamp(colTypeName[tb_col_seq[ins_col_count]])){
600 if (!colData[fl_col_seq[ins_col_count]].equals("")){
601 stmt_ins.setTimestamp(ins_col_count+1,java.sql.Timestamp.valueOf(colData[fl_col_seq[ins_col_count]]));
602 //CmnUtils.debugPrint("Timestamp=" + colData[fl_col_seq[ins_col_count]]);
604 stmt_ins.setTimestamp(ins_col_count+1,null);
606 } else if (CmnUtils.isColBigDecimal(colTypeName[tb_col_seq[ins_col_count]])){
607 if (!colData[fl_col_seq[ins_col_count]].equals("")){
608 stmt_ins.setBigDecimal(ins_col_count+1,new BigDecimal(colData[fl_col_seq[ins_col_count]]));
610 stmt_ins.setNull(ins_col_count+1,java.sql.Types.DECIMAL);
612 } else if (CmnUtils.isColShort(colTypeName[tb_col_seq[ins_col_count]])){
613 if (!colData[fl_col_seq[ins_col_count]].equals("")){
614 stmt_ins.setShort(ins_col_count+1,Short.parseShort(colData[fl_col_seq[ins_col_count]]));
616 stmt_ins.setNull(ins_col_count+1,java.sql.Types.SMALLINT);
618 } else if (CmnUtils.isColBoolean(colTypeName[tb_col_seq[ins_col_count]])){
619 if (!colData[fl_col_seq[ins_col_count]].equals("")){
620 stmt_ins.setBoolean(ins_col_count+1,Boolean.valueOf(colData[fl_col_seq[ins_col_count]]));
622 stmt_ins.setNull(ins_col_count+1,java.sql.Types.BOOLEAN);
624 } else if (CmnUtils.isColInt(colTypeName[tb_col_seq[ins_col_count]])){
625 if (!colData[fl_col_seq[ins_col_count]].equals("")){
626 stmt_ins.setInt(ins_col_count+1,Integer.parseInt(colData[fl_col_seq[ins_col_count]]));
628 stmt_ins.setNull(ins_col_count+1,java.sql.Types.INTEGER);
630 } else if (CmnUtils.isColLong(colTypeName[tb_col_seq[ins_col_count]])){
631 if (!colData[fl_col_seq[ins_col_count]].equals("")){
632 stmt_ins.setLong(ins_col_count+1,Long.parseLong(colData[fl_col_seq[ins_col_count]]));
634 stmt_ins.setNull(ins_col_count+1,java.sql.Types.BIGINT);
636 } else if (CmnUtils.isColFloat(colTypeName[tb_col_seq[ins_col_count]])){
637 if (!colData[fl_col_seq[ins_col_count]].equals("")){
638 stmt_ins.setFloat(ins_col_count+1,Float.parseFloat(colData[fl_col_seq[ins_col_count]]));
640 stmt_ins.setNull(ins_col_count+1,java.sql.Types.REAL);
642 } else if (CmnUtils.isColDouble(colTypeName[tb_col_seq[ins_col_count]])){
643 if (!colData[fl_col_seq[ins_col_count]].equals("")){
644 stmt_ins.setDouble(ins_col_count+1,Double.parseDouble(colData[fl_col_seq[ins_col_count]]));
646 stmt_ins.setNull(ins_col_count+1,java.sql.Types.FLOAT);
648 } else if (CmnUtils.isColBytes(colTypeName[tb_col_seq[ins_col_count]])){
649 if (!colData[fl_col_seq[ins_col_count]].equals("")){
650 stmt_ins.setBytes(ins_col_count+1,CmnUtils.base64ToBytes(colData[fl_col_seq[ins_col_count]]));
652 stmt_ins.setBytes(ins_col_count+1,null);
654 } else if (CmnUtils.isColBlob(colTypeName[tb_col_seq[ins_col_count]])){
655 if (!colData[fl_col_seq[ins_col_count]].equals("")){
656 stmt_ins.setBytes(ins_col_count+1,CmnUtils.base64ToBytes(colData[fl_col_seq[ins_col_count]]));
658 stmt_ins.setBlob(ins_col_count+1,null,0);
660 } else if (CmnUtils.isColClob(colTypeName[tb_col_seq[ins_col_count]])){
661 if (!colData[fl_col_seq[ins_col_count]].equals("")){
662 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
664 stmt_ins.setClob(ins_col_count+1,null,0);
667 throw new Exception("
\83J
\83\89\83\80\82Ì
\83f
\81[
\83^
\8c^
\82ª
\91Î
\8fÛ
\8aO
\82Å
\82·
\81B[" + colTypeName[tb_col_seq[ins_col_count]] + "]");
671 protected void disconnect() throws Exception{
674 CmnUtils.debugPrint("Start disconnect database");
676 CmnUtils.debugPrint("End disconnect database");
679 catch (SQLException e) {