-2

私は次のようにデータベースを作成しました:

Class.forName("org.sqlite.JDBC");

    Connection connection = null;

    try
    {
        connection = DriverManager.getConnection("jdbc:sqlite:ebank.db");
        final Statement statement = connection.createStatement();
        statement.setQueryTimeout(30);

        statement.executeUpdate("create table if not exists employee (id integer,firstname string, lastname string, username string, password string, lgdin integer)");
//            statement.executeUpdate("insert into person values(1, 'leo')");
//            statement.executeUpdate("insert into person values(2, 'yui')");
//            ResultSet rs = statement.executeQuery("select * from person");

//            while(rs.next())
//            {
//                System.out.println("name = " + rs.getString("name"));
//                System.out.println("id = " + rs.getString("id"));
//            }


    }
    catch(SQLException e)
    {
        System.err.println(e.getMessage());
    }
    finally
    {
        try
        {
            if (connection !=null)
            {
                connection.close();
            }
        }
        catch(SQLException e)
        {
            System.err.println();
        }
    }

私がする必要があるのは、私のアプリケーションの他のクラスからこのデータベースにアクセスできることです。まず、別のクラスからこのデータベースに2つの文字列変数を追加しようとしていますが、ステートメントオブジェクトにアクセスしてこれを行うことができません。どうすればいいですか?

4

3 に答える 3

2

必要なものを処理するパブリック メソッドを使用して、データベースを処理するクラスを定義します。

例としては、いくつかの静的メソッドを持つ静的クラスがあり、パッケージ内の各クラスはそのクラスを直接呼び出すことができます。

public static class Database {
    public Database(String dbName){

        //HERE YOU INITIALIZE YOUR DATABASE, create the connection etc.
        // with an HSQLDB server would be something like this: change with your code.

        hsqlServer = new Server();
        hsqlServer.setDatabaseName(0, dbName);
        hsqlServer.setDatabasePath(0, "file:db/" + fileName);
    }

    public static void start(){
        //start database class
    }
    public static void connect(){
        //create the connectio, something like this:
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/garby", "sa", "");
            } catch (ClassNotFoundException e) {

            } catch (SQLException e) {

        }
    }
    public stati String getNameById(int id){
         //HERE YOU create the statement, get the name while id=id and so on, it's an example
    }
 }

クラスでデータベースを使用する場合は、次のことを行う必要があります。

main メソッドで、データベースを初期化します。

public static void main (String arg[]){
    new Database("name");
    Database.start();
}

他のクラスとメソッドでは、次を呼び出します。

Database.connect();
Database.myMethodToReturnSomethin();
Database.disconnect();

(明らかに、disconnect() および stop() メソッドが必要です)

このメソッドを使用すると、パッケージ内の任意のクラスからデータベースに簡単にアクセスできます

于 2012-10-15T12:09:38.707 に答える
0

このクラスから戻っconnectionて、他のクラスでやりたいことをしてください。

このクラスの公開getDBConnection()およびメソッドreleaseResources(Connection connection)

public class DBConnectionManager {

    public static Connection getDBConnection() throws Exception {
        //some code to create connection
        return connection;
    }


    public static void releaseResources(Connection connection) {
        //close connection if not null
    }

}
于 2012-10-15T11:57:53.980 に答える
0

シングルトン設計パターンが必要です

于 2012-10-15T12:10:12.170 に答える