📄 databaseinterface.java
字号:
/*
Aglet Maintenance Project
File: DatabaseInterface.java
Date: 07-27-99
Author: Jesse McConnell
Description: A generic Database Interface.
*/
//Package Declaration
package edu.uidaho.csds.util;
//Import Declarations
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.util.Vector;
import java.util.Enumeration;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
public class DatabaseInterface implements Serializable {
private boolean _is_connected = false;
private String _sql_url;
private String _sql_user;
private String _sql_password;
private String _sql_driver;
private Connection _connection;
private Statement _statement;
//Constructor
public DatabaseInterface(String url, String user, String password,
String driver) {
try {
_sql_driver = driver;
Class.forName(_sql_driver);
}
catch (ClassNotFoundException cnfe) {
System.err.println("DatabaseInterface::" + cnfe);
}
_sql_user = user;
_sql_password = password;
_sql_url = url;
}
/**
This function is used for reseting the database information. Should
a programmer need to connect to different databases within the same
program then this function sets up the new JDBC information.
@param url The URL of the new database.
@param user The user of the new database.
@param password The users password for the database.
@param driver The new driver in case of a database type change.
@return A boolean value representing success.
*/
public boolean reset(String url, String user, String password,
String driver) {
if (_is_connected) {
System.err.println("DatabaseInterface:reset:Previous connection must" +
" be dropped before database reset.");
return false;
}
try {
_sql_driver = driver;
Class.forName(_sql_driver);
}
catch (ClassNotFoundException cnfe) {
System.err.println("DatabaseInterface:reset:" + cnfe);
return false;
}
_sql_user = user;
_sql_password = password;
_sql_url = url;
return true;
}
/**
This function is used to start the Database connection. This must be
called before any actions are performed with the database. Once the
connection is established, a boolean value is set that indicates if the
DatabaseInterface is connected.<br>
@return A boolean value representing success at connecting via JDBC.
*/
public boolean start() {
try {
Class.forName(_sql_driver);
_connection = DriverManager.getConnection(_sql_url, _sql_user,
_sql_password);
}
catch (ClassNotFoundException cnfe) {
_is_connected = false;
System.err.println("DatabaseInterface:start:" + cnfe);
return false;
}
catch (SQLException sqle) {
_is_connected = false;
System.err.println("DatabaseInterface:start:" + sqle);
return false;
}
_is_connected = true;
return true;
}
/**
This function stops the Database connection. This should be called when
there are no more actions to be performed with the database.
@return A boolean value representing success.
*/
public boolean stop() {
try {
_connection.close();
_connection = null;
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:stop:" + sqle);
return false;
}
_is_connected = false;
return true;
}
/**
This function returns the connection status of the DatabaseInterface.
@return A boolean value representing success.
*/
public boolean isConnected() {
return _is_connected;
}
/**
This function accepts a single string formatted in SQL. It can be a SQL
INSERT, UPDATE or DELETE statement. This is submitted to the database
which the DatabaseInterface is currently connected to.
@param sql_string A SQL formatted string of the INSERT, UPDATE, or DELETE
flavor.
@return A boolean value representing success.
*/
public boolean submitUpdate(String sql_string) {
if (!_is_connected) {
System.err.println("DatabaseInterface:submitUpdate:Must start " +
"the DatabaseInterface connection before submitting updates.");
}
try {
_statement = _connection.createStatement();
_statement.executeUpdate(sql_string);
_statement.close();
//_statement = null;
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:" + sqle);
return false;
}
return true;
}
/**
This function accepts a vector of SQL formatted strings. These strings can
be any combination of INSERT, UPDATE, or DELETE statements. These strings
are submitted to the database one at a time and an error message is
written to the screen for each entry not successfully submitted. At a
later time the return object can contain more information as to those
strings that failed.
@param sql_strings Vector of SQL formatted strings.
@return A boolean value representing success.
*/
public boolean submitUpdate(Vector sql_strings) {
String sql_string = "";
if (!_is_connected) {
System.err.println("DatabaseInterface:submitUpdate:Must start " +
"the DatabaseInterface connection before submitting updates.");
}
try {
_statement = _connection.createStatement();
Enumeration enum = sql_strings.elements();
while (enum.hasMoreElements()) {
try {
sql_string = (String)enum.nextElement();
_statement.executeUpdate(sql_string);
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:Update failed " +
"for the following element:\n-----------\n" + sql_string +
"\n-----------\nRecord Not Updated.");
}
}
_statement.close();
//_statement = null;
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:" + sqle);
return false;
}
return true;
}
/**
This function accepts a single SQL formatted string of the QUERY flavor.
The string is sent to the database and the resulting answer is returned.
@param sql_string A QUERY sql string.
@return A ResultSet of the information requested, or null if errors occur.
*/
public ResultSet submitQuery(String sql_string) {
ResultSet result = null;
if (!_is_connected) {
System.err.println("DatabaseInterface:submitUpdate:Must start " +
"the DatabaseInterface connection before submitting updates.");
return null;
}
try {
_statement = _connection.createStatement();
result = _statement.executeQuery(sql_string);
_statement.close();
//_statement = null;
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:" + sqle);
return null;
}
return result;
}
/**
This function accepts a vector of sql strings of the QUERY flavor. These
queries are submitted one at a time and the resulting answers are placed into
another vector which it returned.
@param sql_string A Vector of QUERY sql strings.
@return A vector of ResultSets containing the information requested,
or null if errors occur.
*/
public Vector submitQuery(Vector sql_strings) {
String sql_string ="";
Vector result = new Vector();
if (!_is_connected) {
System.err.println("DatabaseInterface:submitUpdate:Must start " +
"the DatabaseInterface connection before submitting updates.");
return null;
}
try {
_statement = _connection.createStatement();
Enumeration enum = sql_strings.elements();
while (enum.hasMoreElements()) {
try {
sql_string = (String)enum.nextElement();
result.addElement(_statement.executeQuery(sql_string));
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:Update failed " +
"for the following element:\n-----------\n" + sql_string +
"\n-----------\nRecord Not Updated.");
}
}
_statement.close();
//_statement = null;
}
catch (SQLException sqle) {
System.err.println("DatabaseInterface:submitUpdate:" + sqle);
return result;
}
return result;
}
/**
Overriding the method to provide custom serialization handling. Some of the
SQL components are not serializable and need to be make null.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
_statement = null;
_connection = null;
out.defaultWriteObject();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -