1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * JdbGet.java --
|
---|
4 | * A component of the Greenstone digital library software
|
---|
5 | * from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | **********************************************************************/
|
---|
25 |
|
---|
26 | import jdbm.RecordManager;
|
---|
27 | import jdbm.RecordManagerFactory;
|
---|
28 | import jdbm.helper.FastIterator;
|
---|
29 | import jdbm.htree.HTree;
|
---|
30 |
|
---|
31 | import java.io.IOException;
|
---|
32 | import java.util.Properties;
|
---|
33 |
|
---|
34 |
|
---|
35 | public class JdbGet
|
---|
36 | {
|
---|
37 | static String TNAME = "greenstone";
|
---|
38 |
|
---|
39 | RecordManager recman_;
|
---|
40 | HTree hashtable_;
|
---|
41 |
|
---|
42 |
|
---|
43 | public JdbGet(String db_filename)
|
---|
44 | throws IOException
|
---|
45 | {
|
---|
46 | // create or open a record manager
|
---|
47 | Properties props = new Properties();
|
---|
48 | recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
|
---|
49 |
|
---|
50 | // create or load table
|
---|
51 | long recid = recman_.getNamedObject(TNAME);
|
---|
52 | if (recid != 0) {
|
---|
53 | hashtable_ = HTree.load(recman_, recid);
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | System.err.println("Failed to find database table '" + TNAME +"' ...");
|
---|
57 | System.exit(-1);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | public void get(String key)
|
---|
63 | throws IOException
|
---|
64 | {
|
---|
65 | FastIterator iter;
|
---|
66 | String val;
|
---|
67 |
|
---|
68 | val = (String) hashtable_.get(key);
|
---|
69 | System.out.println(val);
|
---|
70 |
|
---|
71 | recman_.close();
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | public static void print_usage()
|
---|
76 | {
|
---|
77 | System.err.println("Usage: java JdbGet database-name key");
|
---|
78 | System.exit(-1);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static void main(String[] args)
|
---|
82 | {
|
---|
83 |
|
---|
84 | if (args.length != 2) {
|
---|
85 | print_usage();
|
---|
86 | }
|
---|
87 |
|
---|
88 | String dbname = args[0];
|
---|
89 | String key = args[1];
|
---|
90 |
|
---|
91 | try {
|
---|
92 |
|
---|
93 | JdbGet table = new JdbGet(dbname);
|
---|
94 | table.get(key);
|
---|
95 | }
|
---|
96 | catch (IOException e) {
|
---|
97 | e.printStackTrace();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | } |
---|