KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > generator > database > DatabaseOutputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * DatabaseOutputStream.java
26  *
27  * Created on Jan 14, 2003
28  */

29
30
31 package com.sun.jdo.spi.persistence.generator.database;
32
33 import java.io.*;
34 import java.sql.*;
35 import java.util.ResourceBundle JavaDoc;
36 import com.sun.jdo.spi.persistence.utility.I18NHelper;
37 import com.sun.jdo.spi.persistence.utility.logging.Logger;
38
39
40 /*
41  * Represents a database connection as an output stream.
42  *
43  * @author Jie Leng
44  */

45 public class DatabaseOutputStream extends OutputStream {
46      /** The logger */
47     private static final Logger logger =
48             LogHelperDatabaseGenerator.getLogger();
49
50     /** I18N message handler */
51     private final static ResourceBundle JavaDoc messages =
52             I18NHelper.loadBundle(DatabaseOutputStream.class);
53
54     /** Connection to the database. */
55     // XXX FIXME S/b final; make it so if we can get rid of setConnection.
56
private Connection conn_ = null;
57
58     // XXX FIXME Assert conn != null and directly set the value of conn;
59
// remove setConnection (below)
60
public DatabaseOutputStream(Connection conn) {
61         super();
62         setConnection(conn);
63     }
64
65     // XXX FIXME I think this is not needed.
66
public DatabaseOutputStream() {
67         super();
68     }
69
70     /**
71      * Closes the database connection.
72      */

73     public void close() {
74         try {
75             // XXX test is not necessary once we assert not null in constructor
76
if (conn_ != null) {
77                 conn_.commit();
78                 // Close the connection
79
conn_.close();
80             }
81
82         } catch (SQLException e) {
83         if (logger.isLoggable(Logger.FINE))
84             logger.fine("Exception in cleanup", e); // NOI18N
85
}
86     }
87
88     /**
89      * Commits the database connection.
90      */

91     public void flush() {
92         try {
93             // XXX test is not necessary once we assert not null in constructor
94
if (conn_ != null) {
95                 conn_.commit();
96             }
97         } catch (SQLException e) {
98             if (logger.isLoggable(Logger.FINE))
99                logger.fine("Exception in cleanup", e); // NOI18N
100
}
101     }
102
103     /**
104      * This method is not supported in DatabaseOutputStream because it
105      * doesn't make sense to write a single int to a database stream. So
106      * always throws UnsupportedOperationException.
107      * @throws UnsupportedOperationException
108      */

109     public void write(int b) {
110         throw new UnsupportedOperationException JavaDoc();
111     }
112
113     /**
114      * Executes the given statement in the database.
115      * @param stmt SQL to be executed
116      * @throws SQLException Thrown if there is a problem preparing stmt as a
117      * statement, or in executing it.
118      */

119     public void write(String JavaDoc stmt) throws SQLException {
120         PreparedStatement pstmt = conn_.prepareStatement(stmt);
121         pstmt.execute();
122     }
123
124     // XXX FIXME Is this really necessary? Delete if possible.
125
public void setConnection(Connection conn) {
126         conn_ = conn;
127     }
128 }
129
Popular Tags