我想運行我的應用程序與tomcat第一次,我正在調試/運行應用程序其工作正常。但是當我第二次嘗試運行時,出現錯誤「XJ040」。與關閉數據庫與servlet連接的問題
"Failed to start database 'C:\Documents and Settings\vitaly87\.netbeans- derby\articals' with class loader WebappClassLoader
context: /WebApplication1
delegate: false
repositories:
我認爲問題是由於一些錯誤關閉connections.Because當我停止我的服務器,問題消失了,直到第二次運行。
這裏的代碼:如果你有一個例外
private Connection connect = null;
//private Statement stmt = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public ArrayList<story> stories=new ArrayList<story>();
void getStories(String version) throws SQLException{
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}catch(ClassNotFoundException e){
System.out.println(e);
}
connect = DriverManager.getConnection("jdbc:derby:C:\\Documents and Settings\\vitaly87\\.netbeans-derby\\articals", "admin", "admin");
// statement = connect.createStatement();
int ArticlesId= Integer.parseInt(version);
preparedStatement = connect.prepareStatement("SELECT * FROM admin.articles where id>"+ArticlesId+"");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
stories.add(new story(resultSet.getString("title"),resultSet.getString("date"),resultSet.getString("text")));
}
close();
}
//close connection
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
感謝在最後
connect = DriverManager.getConnection(...)
try
{
// use connection
}
finally
{
try
{
connect.close()
}
catch (SQLException e)
{
// log e
}
}
幫助
爲什麼不在close函數的開始處放一個print語句來查看它是否被運行。 – BobTurbo