Revision 2354
Added by sgarg almost 20 years ago
src/upgrade_db_to_1_5.java | ||
---|---|---|
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$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
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, sqlStatement2 = null; |
|
36 |
ResultSet rset = null; |
|
37 |
try{ |
|
38 |
|
|
39 |
// Create a JDBC connection to the database |
|
40 |
DriverManager.registerDriver(new @dbDriver@()); |
|
41 |
sqlca= DriverManager.getConnection ("@jdbc-connect@", "@user@", "@password@"); |
|
42 |
|
|
43 |
// Delete old nodedatanumerical column from xml_nodes if one exsists |
|
44 |
try { |
|
45 |
System.out.print( |
|
46 |
"Deleting old nodedatanumerical column from xml_nodes..."); |
|
47 |
|
|
48 |
sqlStatement = sqlca.createStatement(); |
|
49 |
sqlStatement.executeQuery( |
|
50 |
"ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical"); |
|
51 |
sqlStatement.close(); |
|
52 |
|
|
53 |
System.out.println(" done."); |
|
54 |
} |
|
55 |
catch (Exception e) { |
|
56 |
System.out.println(" column not found."); |
|
57 |
} |
|
58 |
|
|
59 |
// Create nodedatanumerical column in xml_nodes |
|
60 |
System.out.print( |
|
61 |
"Creating new nodedatanumerical column in xml_nodes..."); |
|
62 |
|
|
63 |
sqlStatement = sqlca.createStatement(); |
|
64 |
sqlStatement.executeQuery( |
|
65 |
"ALTER TABLE xml_nodes ADD nodedatanumerical number"); |
|
66 |
sqlStatement.close(); |
|
67 |
|
|
68 |
System.out.println(" done."); |
|
69 |
|
|
70 |
// Copy numerical values from nodedata to |
|
71 |
// nodedatanumerical column in xml_nodes |
|
72 |
System.out.print( |
|
73 |
"Copy numerical values from nodedata to nodedatanumerical " |
|
74 |
+ "in xml_nodes..."); |
|
75 |
|
|
76 |
sqlStatement = sqlca.createStatement(); |
|
77 |
rset = sqlStatement.executeQuery( |
|
78 |
"SELECT DISTINCT NODEID, NODEDATA " |
|
79 |
+ "FROM xml_nodes WHERE " |
|
80 |
+ "nodedata IS NOT NULL AND " |
|
81 |
+ "UPPER(nodedata) = LOWER(nodedata)"); |
|
82 |
|
|
83 |
int count = 0; |
|
84 |
while (rset.next()) { |
|
85 |
|
|
86 |
String nodeid = rset.getString(1); |
|
87 |
String nodedata = rset.getString(2); |
|
88 |
|
|
89 |
try { |
|
90 |
if(!nodedata.trim().equals("")){ |
|
91 |
double s = Double.parseDouble(nodedata); |
|
92 |
|
|
93 |
sqlStatement2 = sqlca.createStatement(); |
|
94 |
sqlStatement2.executeQuery( |
|
95 |
"update xml_nodes set nodedatanumerical = " + s + |
|
96 |
" where nodeid=" + nodeid); |
|
97 |
sqlStatement2.close(); |
|
98 |
|
|
99 |
count++; |
|
100 |
} |
|
101 |
} catch (NumberFormatException nfe) { |
|
102 |
|
|
103 |
} catch (Exception e) { |
|
104 |
System.out.println("Exception:" + e.getMessage()); |
|
105 |
} |
|
106 |
} |
|
107 |
System.out.println(" done. " + count + " values copied."); |
|
108 |
|
|
109 |
rset.close(); |
|
110 |
sqlStatement.close(); |
|
111 |
|
|
112 |
sqlca.close(); |
|
113 |
} |
|
114 |
|
|
115 |
catch (SQLException ex) |
|
116 |
{ |
|
117 |
System.out.println("SQLException:" + ex.getMessage()); |
|
118 |
ex.printStackTrace(); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
0 | 122 |
Also available in: Unified diff
Adding new java code which upgrades the database for version 1.5