-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();
        }
    }

我需要做的是从我的应用程序中的其他类访问这个数据库。首先,我试图将另一个类中的两个字符串变量添加到该数据库中,但我无法访问语句对象来执行此操作。我该怎么做?

4

3 回答 3

2

Define a class that handles the database with some public method to handle anything you need.

An example can be a static class with some static method, each class in your package can call directly the class.

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
    }
 }

In a class if you want to use the database you have to:

in the main method, initialize the database:

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

in other class and methods just call:

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

(obviously you need to have a disconnect() and a stop() methods)

With this method you can simply access the database from any class you have in the package

于 2012-10-15T12:09:38.707 回答
0

Singleton design pattern is what you need

于 2012-10-15T12:10:12.170 回答
0

Return connection from this class and do whatever you want in other classes.

expose getDBConnection() and releaseResources(Connection connection) methods in this class

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 回答