1 package nmp.dbms.JDBC;
2
3 import java.io.FileWriter;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.sql.SQLException;
7
8 import nmp.dbms.SQLVectorResult;
9
10 public class JDBCHandle
11 // implements DBHandle
12 {
13 Jdbc_Sql database;
14 String dbName;
15
16 public JDBCHandle( String driverName, String dbName, String url, String user, String passwd)
17 throws Exception {
18 this.dbName = dbName;
19 database = new Jdbc_Sql( driverName, url, user, passwd, true, true, new PrintWriter( new FileWriter( "//tmp//SQLdebug.log", true), true), 1);
20 }
21
22 /***
23 * Returns the name of the associated database
24 */
25 public String dbName() {
26 return dbName;
27 }
28
29 /***
30 * Executes a SQL query, returning all found rows
31 */
32 public SQLVectorResult executeQuery( String sqlstring)
33 throws SQLException {
34 return database.executeQuery ( sqlstring);
35 }
36
37 /***
38 * Executes a SQL query, returning the first maxcount rows
39 */
40 public SQLVectorResult executeQuery( String sqlstring, int maxcount)
41 throws SQLException {
42 return database.executeQuery ( sqlstring, maxcount);
43 }
44
45 /***
46 * Gets more rows that conform to the executed query
47 */
48 public boolean executeQuery( SQLVectorResult result, int maxcount)
49 throws SQLException {
50 return database.executeQuery ( result, maxcount);
51 }
52
53 /***
54 * Executes a SQL update, returning the number of modified rows
55 */
56 public int executeUpdate(String sqlstring)
57 throws SQLException {
58 return database.executeUpdate ( sqlstring);
59 }
60
61
62 /***
63 * Serialize the state of the database
64 *
65 * @param oos Output stream to serialize state
66 * @param tables Name of the table from which the state should be serialized
67 */
68 public void serialize( java.io.ObjectOutputStream oos, String[] tables)
69 throws IOException {
70 for( int i = 0; i < tables.length; i++) {
71 try {
72 SQLVectorResult result = executeQuery( "select * from " + tables[i] + ";", 100);
73 while( result != null && result.rows != null) {
74 String baseStr = "insert into " + tables[i] + "(";
75 for( int j = 0; j < result.columnName.length; j++)
76 if( j > 0)
77 baseStr = baseStr + "," + result.columnName[j];
78 else
79 baseStr = baseStr + result.columnName[j];
80 baseStr = baseStr + ") values (";
81 for( int j = 0; j < result.rows.size(); j++) {
82 String str = baseStr;
83 Object[] row = (Object[])result.rows.elementAt( j);
84 for( int k = 0; k < row.length; k++)
85 if( k > 0)
86 str = str + ", '" + row[k] + "'";
87 else
88 str = str + " '" + row[k] + "'";
89 str = str + ");";
90 oos.writeUTF( str);
91 }
92 if( ! executeQuery( result, 100))
93 break;
94 }
95 } catch( SQLException ex) {
96 /* do nothing */
97 }
98 }
99 oos.writeUTF( "");
100 }
101
102 /***
103 * De-serialize the state of the database
104 *
105 * @param oos Output stream to serialize state
106 */
107 public void unserialize( java.io.ObjectInputStream ois)
108 throws IOException {
109 for( ; ; ) {
110 String str = ois.readUTF();
111 if( str.equals( ""))
112 break;
113 try {
114 executeUpdate( str);
115 }catch( SQLException ex) {
116 /* do nothing */
117 }
118 }
119 }
120
121 /***
122 * Closes this handle. After calling this method no other DB method should be called.
123 */
124 public void close() {
125 database.closeAll();
126 }
127
128 }
This page was automatically generated by Maven