Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class for upgrading the database to version 1.5
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Saurabh Garg
7
 *    Release: @release@
8
 *
9
 *   '$Author: sgarg $'
10
 *     '$Date: 2004-12-30 14:33:01 -0800 (Thu, 30 Dec 2004) $'
11
 * '$Revision: 2355 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28

    
29
import java.io.*;
30
import java.sql.*;
31

    
32
public class upgrade_db_to_1_5{
33
    public static void main(String [] ags) throws IOException{
34
        Connection sqlca  = null;
35
        Statement sqlStatement = null;
36
        PreparedStatement pstmt = null;
37
        ResultSet rset = null;
38
        String database = "@database@";
39
        String url = "@jdbc-connect@";
40
        String user = "@user@";
41
        String password = "@password@";
42

    
43
        try{
44
            // Create a JDBC connection to the database
45
            DriverManager.registerDriver(new @dbDriver@());
46
            sqlca= DriverManager.getConnection (url, user, password);
47

    
48
            // Delete old nodedatanumerical column from xml_nodes if one exsists
49
            try {
50
                System.out.println(
51
                    "Deleting old nodedatanumerical column from xml_nodes...");
52

    
53
                pstmt = sqlca.prepareStatement(
54
                       "ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical");
55
                pstmt.execute();
56
                pstmt.close();
57

    
58
                System.out.println("Done.");
59
            }
60
            catch (Exception e) {
61
                e.printStackTrace();
62
                System.out.println(" column not found.");
63
            }
64

    
65
            // Create nodedatanumerical column in xml_nodes
66
            System.out.println(
67
                "Creating new nodedatanumerical column in xml_nodes...");
68

    
69
            if(database.equals("oracle")){
70
                pstmt =sqlca.prepareStatement(
71
                    "ALTER TABLE xml_nodes ADD nodedatanumerical NUMBER");
72
            } else {
73
                pstmt =sqlca.prepareStatement(
74
                    "ALTER TABLE xml_nodes ADD nodedatanumerical FLOAT8");
75
            }
76
            pstmt.execute();
77
            pstmt.close();
78

    
79
            System.out.println("Done.");
80

    
81
            // Copy numerical values from nodedata to
82
            // nodedatanumerical column in xml_nodes
83
            System.out.println(
84
                "Copy numerical values from nodedata to nodedatanumerical "
85
                + "in xml_nodes...");
86

    
87
            sqlStatement = sqlca.createStatement();
88
            rset = sqlStatement.executeQuery(
89
                "SELECT DISTINCT NODEID, NODEDATA "
90
                + "FROM xml_nodes WHERE "
91
                + "nodedata IS NOT NULL AND "
92
                + "UPPER(nodedata) = LOWER(nodedata)");
93

    
94
            int count = 0;
95
            while (rset.next()) {
96

    
97
                String nodeid = rset.getString(1);
98
                String nodedata = rset.getString(2);
99

    
100
                try {
101
                    if(!nodedata.trim().equals("")){
102
                        double s = Double.parseDouble(nodedata);
103

    
104
                        pstmt =sqlca.prepareStatement(
105
                            "update xml_nodes set nodedatanumerical = " + s +
106
                            " where nodeid=" + nodeid);
107
                        pstmt.execute();
108
                        pstmt.close();
109

    
110
                        count++;
111
                    }
112
                } catch (NumberFormatException nfe) {
113

    
114
                } catch (Exception e) {
115
                    System.out.println("Exception:" + e.getMessage());
116
                }
117
            }
118
            System.out.println("Done. " + count + " values copied.");
119

    
120
            rset.close();
121
            sqlStatement.close();
122

    
123
            sqlca.close();
124
        }
125

    
126
        catch (SQLException ex)
127
        {
128
            System.out.println("SQLException:" + ex.getMessage());
129
            ex.printStackTrace();
130
        }
131
    }
132
}
(20-20/24)