01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.samples;
07:
08: import java.sql.ResultSet;
09: import java.sql.ResultSetMetaData;
10: import java.sql.SQLException;
11: import java.sql.Types;
12:
13: import org.h2.tools.Csv;
14: import org.h2.tools.SimpleResultSet;
15:
16: /**
17: * This sample application shows how to use the CSV tool
18: * to write CSV (comma separated values) files, and
19: * how to use the tool to read such files.
20: */
21: public class CsvSample {
22:
23: public static void main(String[] args) throws SQLException {
24: CsvSample.write();
25: CsvSample.read();
26: }
27:
28: /**
29: * Write a CSV file.
30: */
31: static void write() throws SQLException {
32: SimpleResultSet rs = new SimpleResultSet();
33: rs.addColumn("NAME", Types.VARCHAR, 255, 0);
34: rs.addColumn("EMAIL", Types.VARCHAR, 255, 0);
35: rs.addColumn("PHONE", Types.VARCHAR, 255, 0);
36: rs.addRow(new String[] { "Bob Meier", "bob.meier@abcde.abc",
37: "+41123456789" });
38: rs.addRow(new String[] { "John Jones", "john.jones@abcde.abc",
39: "+41976543210" });
40: Csv.getInstance().write("data/test.csv", rs, null);
41: }
42:
43: /**
44: * Read a CSV file.
45: */
46: static void read() throws SQLException {
47: ResultSet rs = Csv.getInstance().read("data/test.csv", null,
48: null);
49: ResultSetMetaData meta = rs.getMetaData();
50: while (rs.next()) {
51: for (int i = 0; i < meta.getColumnCount(); i++) {
52: System.out.println(meta.getColumnLabel(i + 1) + ": "
53: + rs.getString(i + 1));
54: }
55: System.out.println();
56: }
57: rs.close();
58: }
59: }
|