1
2
3 package com.pnpconsult.zeiterfassung.dump;
4
5 import java.io.BufferedOutputStream;
6 import java.io.FileOutputStream;
7 import java.io.PrintWriter;
8 import java.sql.Connection;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.ResultSetMetaData;
12 import java.sql.Types;
13 import java.text.DateFormat;
14 import java.text.DecimalFormat;
15 import java.text.DecimalFormatSymbols;
16 import java.text.NumberFormat;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
19 import java.util.ResourceBundle;
20
21 import net.sf.hibernate.Session;
22
23 import com.pnpconsult.zeiterfassung.helper.SessionLocator;
24
25 /***
26 * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen </a>
27 * @version $Id: DumpMain.java,v 1.3 2004/06/24 20:54:20 powerpete Exp $
28 */
29 public class DumpMain
30 {
31 private static final int MIN = 1;
32
33 private static PrintWriter out;
34
35 public static void main(String[] args) throws Exception
36 {
37 ResourceBundle properties = ResourceBundle.getBundle("dump");
38 System.setProperty("file.encoding", properties
39 .getString("dump.file.encoding"));
40 out = new PrintWriter(new BufferedOutputStream(new FileOutputStream(
41 properties.getString("dump.output.file"))));
42 DateFormat dateFormat = new SimpleDateFormat(properties
43 .getString("dump.date.format"));
44 DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance();
45 numberFormat.applyLocalizedPattern(properties
46 .getString("dump.number.format"));
47 DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
48 symbols.setGroupingSeparator(properties.getString(
49 "dump.number.format.groupingseparator").charAt(0));
50 symbols.setDecimalSeparator(properties.getString(
51 "dump.number.format.decimalseparator").charAt(0));
52 numberFormat.setDecimalFormatSymbols(symbols);
53 String delimiter = properties.getString("dump.delimiter");
54 Session session = SessionLocator.getSession();
55 Connection con = session.connection();
56 if (con.isClosed()) { throw new IllegalStateException(
57 "Connection is closed."); }
58 con.setReadOnly(true);
59 PreparedStatement ps = con.prepareStatement(properties
60 .getString("dump.sql"));
61 ResultSet rs = ps.executeQuery();
62 boolean isFirstLine = true;
63 while (rs.next())
64 {
65 ResultSetMetaData metaData = rs.getMetaData();
66 int columnCount = metaData.getColumnCount() + MIN;
67 if (isFirstLine)
68 {
69 isFirstLine = false;
70 for (int i = MIN; i < columnCount; i++)
71 {
72 if (i > MIN)
73 {
74 print(delimiter);
75 }
76 print(metaData.getColumnLabel(i));
77 }
78 printNewLine();
79 }
80 for (int i = MIN; i < columnCount; i++)
81 {
82 if (i > MIN)
83 {
84 print(delimiter);
85 }
86 switch (metaData.getColumnType(i))
87 {
88 case Types.BIGINT:
89 case Types.DECIMAL:
90 case Types.DOUBLE:
91 case Types.FLOAT:
92 case Types.INTEGER:
93 case Types.NUMERIC:
94 case Types.REAL:
95 case Types.SMALLINT:
96 case Types.TINYINT:
97 print(numberFormat.format(rs.getDouble(i)));
98 break;
99 case Types.DATE:
100 Date date = rs.getDate(i);
101 if (date == null)
102 {
103 print("");
104 }
105 else
106 {
107 print(dateFormat.format(date));
108 }
109 default:
110 print(rs.getObject(i));
111 }
112 }
113 printNewLine();
114 }
115 rs.close();
116 ps.close();
117 session.close();
118 out.flush();
119 out.close();
120 }
121
122 private static void printNewLine()
123 {
124 out.println();
125 }
126
127 private static void print(Object obj)
128 {
129 out.print(obj == null ? "" : obj);
130 }
131 }