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 java.sql.Timestamp colTimestamp = null;
64 private java.sql.Time colTime = null;
65 private java.sql.Date colDate = null;
66 private java.sql.Date colYear = null;
67 private byte [] colBytes = null;
68 private Blob colBlob = null;
69 private Clob colClob = null;
70 private String tbName = null;
71 private int recCount = 0;
72 private CmnProperty cp = null;
74 DataAccessObjects(String _user, String _pass, String _url, String _driver){
81 DataAccessObjects(CmnProperty _cp){
89 public Connection connect() throws Exception{
92 CmnUtils.debugPrint("Start Driver class loading");
93 Class.forName (driver);
94 CmnUtils.debugPrint("End Driver class loading");
95 CmnUtils.debugPrint("Start Connect Database>" + url + "," + user);
96 conn = DriverManager.getConnection(url, user, pass);
97 CmnUtils.debugPrint("End Connect Database");
101 catch (ClassNotFoundException e) {
104 catch (IOException e) {
112 public PreparedStatement prepareSql(String _sql) throws Exception{
114 CmnUtils.debugPrint("Start prepared Statement");
115 CmnUtils.debugPrint(_sql);
116 stmt = conn.prepareStatement(_sql);
117 CmnUtils.debugPrint("End prepared Statement");
120 catch (SQLException e) {
128 public void executeSql() throws Exception{
132 CmnUtils.debugPrint("Start get RecordSet");
134 for(int i=0;i<ColumnCount;i++){
135 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
136 if (colString != null) {
137 alData.add(colString);
143 CmnUtils.debugPrint("End get RecordSet");
145 catch (SQLException e) {
163 public void executeSql(PreparedStatement _stmt) throws Exception{
166 CmnUtils.debugPrint("Start execute Query");
167 rst = _stmt.executeQuery();
168 CmnUtils.debugPrint("End execute Query");
170 CmnUtils.debugPrint("Start get MetaData");
171 rsmd = rst.getMetaData();
172 CmnUtils.debugPrint("End get MetaData");
174 CmnUtils.debugPrint("Start get ColumnCount");
175 ColumnCount = rsmd.getColumnCount();
176 CmnUtils.debugPrint("End get ColumnCount," + ColumnCount);
177 alColTypeName = new ArrayList<String>();
178 alColName = new ArrayList<String>();
179 alColPrecision = new ArrayList<Integer>();
180 alIsNullable = new ArrayList<Integer>();
181 alData = new ArrayList<String>();
182 for(int i=0;i<ColumnCount;i++){
183 alColTypeName.add(rsmd.getColumnTypeName(i + 1));
184 alColName.add(rsmd.getColumnName(i + 1));
185 alColPrecision.add(rsmd.getPrecision(i + 1));
186 alIsNullable.add(rsmd.isNullable(i + 1));
188 colTypeName = (String[])alColTypeName.toArray(new String[0]);
189 colName = (String[])alColName.toArray(new String[0]);
190 colPrecision = (Integer[])alColPrecision.toArray(new Integer[0]);
191 isNullable = (Integer[])alIsNullable.toArray(new Integer[0]);
194 catch (SQLException e) {
202 public void commit() throws Exception{
203 CmnUtils.debugPrint("Start commit");
205 CmnUtils.debugPrint("End commit");
208 public void rollback() throws Exception{
209 CmnUtils.debugPrint("Start rollback");
211 CmnUtils.debugPrint("End rollback");
214 public void closeRecordSet() throws Exception{
225 public void select(String _sql) throws Exception{
226 PreparedStatement stmt;
227 stmt = prepareSql(_sql);
233 public int getColumnCount() throws Exception{
237 public int getRecCount() throws Exception{
241 public ArrayList<String> getArrayList() throws Exception{
245 public ArrayList<String> getArrayColumnNameList() throws Exception{
249 public ArrayList<String> getArrayColumnTypeNameList() throws Exception{
250 return alColTypeName;
253 public ArrayList<Integer> getArrayIsNullableList() throws Exception{
257 public ArrayList<Integer> getArrayPrecisionList() throws Exception{
258 return alColPrecision;
261 public void setArrayColumnMaskList(ArrayList<Boolean> _alColMask) throws Exception{
262 alColMask = _alColMask;
263 colMask = (Boolean[])alColMask.toArray(new Boolean[0]);
266 public void setArrayColumnInclList(ArrayList<Boolean> _alColIncl) throws Exception{
267 alColIncl = _alColIncl;
268 colIncl = (Boolean[])alColIncl.toArray(new Boolean[0]);
271 public void setTableName(String _tbName) throws Exception{
275 public ResultSet getResultSet() throws Exception{
279 public void getRecordToArray() throws Exception{
281 CmnUtils.debugPrint("Start get RecordSet to Array");
284 for(int i=0;i<ColumnCount;i++){
285 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], false);
286 CmnUtils.debugPrint("col=" + colString);
287 if (colString != null) {
288 alData.add(colString);
294 CmnUtils.debugPrint("End get RecordSet to Array");
296 catch (SQLException e) {
304 public void getRecordToPrint(PrintWriter _pw, int _firstCol, int _lastCol) throws Exception{
306 CmnUtils.debugPrint("Start get Record to Print");
311 for(int i=0;i<ColumnCount;i++){
313 colString = getColString(rst, i + 1, colTypeName[i], isNullable[i], colMask[i]);
315 if (colString != null) {
316 if(CmnUtils.isFixString(colTypeName[i]) && cp.isSpcPadding)colString = colString + CmnUtils.padSpace(colString, colPrecision[i], Charset.forName(cp.fileEncoding));
319 if (_firstCol == _lastCol){
320 _pw.print("\"" + colString + "\"" + cp.lineSeparator);
322 _pw.print("\"" + colString);
325 if (_firstCol == _lastCol){
326 _pw.print(colString + cp.lineSeparator);
328 _pw.print(colString);
332 } else if (i == _lastCol){
334 _pw.print("\"" + cp.delimiter + "\"" + colString + "\"" + cp.lineSeparator);
336 _pw.print(cp.delimiter + colString + cp.lineSeparator);
340 _pw.print("\"" + cp.delimiter + "\"" + colString);
342 _pw.print(cp.delimiter + colString);
351 if (_firstCol == _lastCol){
352 _pw.print("\"" + cp.nullMark + "\"" + cp.lineSeparator);
354 _pw.print("\"" + cp.nullMark);
357 if (_firstCol == _lastCol){
358 _pw.print(cp.nullMark + cp.lineSeparator);
360 _pw.print(cp.nullMark);
363 } else if (i == _lastCol){
365 _pw.print("\"" + cp.delimiter + "\"" + cp.nullMark + "\"" + cp.lineSeparator);
367 _pw.print(cp.delimiter + cp.nullMark + cp.lineSeparator);
371 _pw.print("\"" + cp.delimiter + "\"" + cp.nullMark);
373 _pw.print(cp.delimiter + cp.nullMark);
381 CmnUtils.debugPrint("End get Record to Print");
383 catch (SQLException e) {
391 private String getColString(ResultSet _rst, int _rec_cnt, String _colTypeName, Integer _isNullable, Boolean _colMask) throws Exception{
397 colBigDecimal = null;
406 ByteArrayOutputStream baos = null;
407 InputStream is = null;
409 if (CmnUtils.isColString(_colTypeName)) {
410 colString = _rst.getString(_rec_cnt);
411 if (colString != null) {
413 colString =colString.trim().replaceAll("\0","").replaceAll("\"","\"\"");
415 colString =colString.trim().replaceAll("\0","");
419 StringBuffer sb = new StringBuffer();
420 for(int j=0;j<colString.length();j++){
421 if((cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALT]) &&
423 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_EDGE]) &&
424 !(j == 0 || j == colString.length() - 1) ||
425 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALL]))
428 if(CmnUtils.isHankaku(colString.charAt(j))){
429 sb.append(cp.singleByteMaskChar);
431 sb.append(cp.doubleByteMaskChar);
434 sb.append(colString.charAt(j));
437 colString = sb.toString();
440 if(colString.equals(""))colString = " ";
442 } else if (CmnUtils.isColDate(_colTypeName)) {
443 colDate = _rst.getDate(_rec_cnt);
444 if (colDate != null) {
445 colString = colDate.toString();
447 } else if (CmnUtils.isColYear(_colTypeName)) {
448 colYear = _rst.getDate(_rec_cnt);
449 if (colYear != null) {
450 colString = colYear.toString().substring(0, 4);
452 } else if (CmnUtils.isColTimestamp(_colTypeName)) {
453 colTimestamp = _rst.getTimestamp(_rec_cnt);
454 if (colTimestamp != null) {
455 colString = colTimestamp.toString();
457 } else if (CmnUtils.isColTime(_colTypeName)) {
458 colTime = _rst.getTime(_rec_cnt);
459 if (colTime != null) {
460 colString = colTime.toString();
462 } else if (CmnUtils.isColBigDecimal(_colTypeName)) {
463 colBigDecimal = _rst.getBigDecimal(_rec_cnt);
464 if (colBigDecimal != null) {
465 colString = colBigDecimal.toString();
467 } else if (CmnUtils.isColShort(_colTypeName)) {
468 colShort = _rst.getShort(_rec_cnt);
469 if (colShort != null) {
470 colString = colShort.toString();
472 } else if (CmnUtils.isColInt(_colTypeName)) {
473 colInt = _rst.getInt(_rec_cnt);
474 if (colInt != null) {
475 colString = colInt.toString();
477 } else if (CmnUtils.isColLong(_colTypeName)) {
478 colLong = _rst.getLong(_rec_cnt);
479 if (colLong != null) {
480 colString = colLong.toString();
482 } else if (CmnUtils.isColFloat(_colTypeName)) {
483 colFloat = _rst.getFloat(_rec_cnt);
484 if (colFloat != null) {
485 colString = colFloat.toString();
487 } else if (CmnUtils.isColDouble(_colTypeName)) {
488 colDouble = _rst.getDouble(_rec_cnt);
489 if (colDouble != null) {
490 colString = colDouble.toString();
492 } else if (CmnUtils.isColBytes(_colTypeName)) {
493 colBytes = _rst.getBytes(_rec_cnt);
494 if (colBytes != null) {
495 colString = CmnUtils.bytesToBase64(colBytes);
497 } else if (CmnUtils.isColBlob(_colTypeName)) {
498 colBlob = _rst.getBlob(_rec_cnt);
499 if (colBlob != null) {
500 is = colBlob.getBinaryStream();
501 baos = new ByteArrayOutputStream();
503 colBytes = new byte[8192];
504 while ((len = is.read(colBytes)) != -1) {
505 baos.write(colBytes,0,len);
507 colBytes = baos.toByteArray();
508 colString = CmnUtils.bytesToBase64(colBytes);
514 } else if (CmnUtils.isColClob(_colTypeName)) {
515 colClob = _rst.getClob(_rec_cnt);
516 if (colClob != null) {
517 colString = CmnUtils.clobToString(colClob);
520 throw new Exception("
\83J
\83\89\83\80\82Ì
\83f
\81[
\83^
\8c^
\82ª
\91Î
\8fÛ
\8aO
\82Å
\82·
\81B[" + _colTypeName + "]");
525 protected void setColString(PreparedStatement stmt_ins, String [] colData, int ins_col_count, int [] tb_col_seq, int [] fl_col_seq) throws Exception{
527 if (CmnUtils.isColString(colTypeName[tb_col_seq[ins_col_count]])){
530 (cp.isInclColsMask &&
531 !cp.isExclColsMask &&
532 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[ins_col_count]].toUpperCase())
534 (cp.isInclColsMask &&
536 CmnUtils.isMatch(cp.inclColsMaskArray,tbName.toUpperCase(), colName[tb_col_seq[ins_col_count]].toUpperCase()) &&
537 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[ins_col_count]].toUpperCase())
539 (!cp.isInclColsMask &&
541 !CmnUtils.isMatch(cp.exclColsMaskArray,tbName.toUpperCase(),colName[tb_col_seq[ins_col_count]].toUpperCase())
544 StringBuffer sb = new StringBuffer();
545 for(int l=0;l<colData[fl_col_seq[ins_col_count]].length();l++){
546 if((cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALT]) &&
548 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_EDGE]) &&
549 !(l == 0 || ins_col_count == colData[fl_col_seq[ins_col_count]].length() - 1) ||
550 (cp.maskPattern.toUpperCase().equals(cp.MASK_PTN[cp.MASK_PTN_ALL]))
553 if(CmnUtils.isHankaku(colData[fl_col_seq[ins_col_count]].charAt(l))){
554 sb.append(cp.singleByteMaskChar);
556 sb.append(cp.doubleByteMaskChar);
559 sb.append(colData[fl_col_seq[ins_col_count]].charAt(l));
562 colData[fl_col_seq[ins_col_count]] = sb.toString();
565 if (!colData[fl_col_seq[ins_col_count]].equals("")){
567 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]].replaceAll("\"\"","\""));
569 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
572 stmt_ins.setString(ins_col_count+1,null);
574 } else if (CmnUtils.isColDate(colTypeName[tb_col_seq[ins_col_count]])){
575 if (!colData[fl_col_seq[ins_col_count]].equals("")){
576 stmt_ins.setDate(ins_col_count+1,java.sql.Date.valueOf(colData[fl_col_seq[ins_col_count]]));
578 stmt_ins.setDate(ins_col_count+1,null);
580 } else if (CmnUtils.isColYear(colTypeName[tb_col_seq[ins_col_count]])){
581 if (!colData[fl_col_seq[ins_col_count]].equals("")){
582 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
584 stmt_ins.setDate(ins_col_count+1,null);
586 } else if (CmnUtils.isColTime(colTypeName[tb_col_seq[ins_col_count]])){
587 if (!colData[fl_col_seq[ins_col_count]].equals("")){
588 stmt_ins.setTime(ins_col_count+1,java.sql.Time.valueOf(colData[fl_col_seq[ins_col_count]]));
590 stmt_ins.setTime(ins_col_count+1,null);
592 } else if (CmnUtils.isColTimestamp(colTypeName[tb_col_seq[ins_col_count]])){
593 if (!colData[fl_col_seq[ins_col_count]].equals("")){
594 stmt_ins.setTimestamp(ins_col_count+1,java.sql.Timestamp.valueOf(colData[fl_col_seq[ins_col_count]]));
595 //CmnUtils.debugPrint("Timestamp=" + colData[fl_col_seq[ins_col_count]]);
597 stmt_ins.setTimestamp(ins_col_count+1,null);
599 } else if (CmnUtils.isColBigDecimal(colTypeName[tb_col_seq[ins_col_count]])){
600 if (!colData[fl_col_seq[ins_col_count]].equals("")){
601 stmt_ins.setBigDecimal(ins_col_count+1,new BigDecimal(colData[fl_col_seq[ins_col_count]]));
603 stmt_ins.setNull(ins_col_count+1,java.sql.Types.DECIMAL);
605 } else if (CmnUtils.isColShort(colTypeName[tb_col_seq[ins_col_count]])){
606 if (!colData[fl_col_seq[ins_col_count]].equals("")){
607 stmt_ins.setShort(ins_col_count+1,Short.parseShort(colData[fl_col_seq[ins_col_count]]));
609 stmt_ins.setNull(ins_col_count+1,java.sql.Types.SMALLINT);
611 } else if (CmnUtils.isColInt(colTypeName[tb_col_seq[ins_col_count]])){
612 if (!colData[fl_col_seq[ins_col_count]].equals("")){
613 stmt_ins.setInt(ins_col_count+1,Integer.parseInt(colData[fl_col_seq[ins_col_count]]));
615 stmt_ins.setNull(ins_col_count+1,java.sql.Types.INTEGER);
617 } else if (CmnUtils.isColLong(colTypeName[tb_col_seq[ins_col_count]])){
618 if (!colData[fl_col_seq[ins_col_count]].equals("")){
619 stmt_ins.setLong(ins_col_count+1,Long.parseLong(colData[fl_col_seq[ins_col_count]]));
621 stmt_ins.setNull(ins_col_count+1,java.sql.Types.BIGINT);
623 } else if (CmnUtils.isColFloat(colTypeName[tb_col_seq[ins_col_count]])){
624 if (!colData[fl_col_seq[ins_col_count]].equals("")){
625 stmt_ins.setFloat(ins_col_count+1,Float.parseFloat(colData[fl_col_seq[ins_col_count]]));
627 stmt_ins.setNull(ins_col_count+1,java.sql.Types.REAL);
629 } else if (CmnUtils.isColDouble(colTypeName[tb_col_seq[ins_col_count]])){
630 if (!colData[fl_col_seq[ins_col_count]].equals("")){
631 stmt_ins.setDouble(ins_col_count+1,Double.parseDouble(colData[fl_col_seq[ins_col_count]]));
633 stmt_ins.setNull(ins_col_count+1,java.sql.Types.FLOAT);
635 } else if (CmnUtils.isColBytes(colTypeName[tb_col_seq[ins_col_count]])){
636 if (!colData[fl_col_seq[ins_col_count]].equals("")){
637 stmt_ins.setBytes(ins_col_count+1,CmnUtils.base64ToBytes(colData[fl_col_seq[ins_col_count]]));
639 stmt_ins.setBytes(ins_col_count+1,null);
641 } else if (CmnUtils.isColBlob(colTypeName[tb_col_seq[ins_col_count]])){
642 if (!colData[fl_col_seq[ins_col_count]].equals("")){
643 stmt_ins.setBytes(ins_col_count+1,CmnUtils.base64ToBytes(colData[fl_col_seq[ins_col_count]]));
645 stmt_ins.setBlob(ins_col_count+1,null,0);
647 } else if (CmnUtils.isColClob(colTypeName[tb_col_seq[ins_col_count]])){
648 if (!colData[fl_col_seq[ins_col_count]].equals("")){
649 stmt_ins.setString(ins_col_count+1,colData[fl_col_seq[ins_col_count]]);
651 stmt_ins.setClob(ins_col_count+1,null,0);
654 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]] + "]");
658 public void disconnect() throws Exception{
661 CmnUtils.debugPrint("Start disconnect database");
663 CmnUtils.debugPrint("End disconnect database");
666 catch (SQLException e) {