我創建了一個數據庫,如下所示:從多個類訪問數據庫
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();
}
}
什麼,我需要做的是在我的應用程序從其他類訪問該數據庫。首先,我試圖從另一個類添加兩個字符串變量到這個數據庫,但我無法訪問語句對象來做到這一點。我會怎麼做?
您能詳細談談那是什麼?你的意思是定義一個方法嗎? – Nexus490