2014-06-20 4 views
0

TV 프로그램 목록을 저장할 응용 프로그램을 작성하려고하는데 SQLite를 사용하여이 작업을 수행 할 수있는 것처럼 보입니다. 나는 그만하고 프로그램을 시작하지 않으면, 그때 나는 그곳에 아무것도없는 것처럼 데이터베이스가 읽혀진다. 자동 커밋이 활성화됩니다. 그리고 나는 .db 파일을 열면 거기에 저장된 값을 볼 수 있기 때문에 무언가가 쓰여지고 있음을 확신합니다. 여기에 내가 데이터베이스SQLite에서 java가 데이터를 저장하지 않습니다.

package DatabaseHelper; 

import java.sql.ResultSet; 
import java.sql.SQLException; 

public class ShowDatabaseHelper extends DatabaseHelper<String> 
{ 

    public ShowDatabaseHelper(String dbpath) throws SQLException 
    { 
     super(dbpath); 

     // Create table 
     stmt = con.createStatement(); 
     stmt.execute("create table if not exists shows(showname varchar)"); 
     stmt.close(); 
    } 

    public boolean insert(String showTitle) throws SQLException 
    { 
     stmt = con.createStatement(); 

     // Check wether the show is allready in the table 
     ResultSet rs = stmt.executeQuery("select * from shows where showname = '" + showTitle + "'"); 
     stmt.close(); 
     if(rs.getFetchSize() != 0) return false; 

    stmt = con.createStatement(); 
     stmt.executeUpdate("insert into shows(showname) values('" + showTitle + "')"); 
     //con.commit(); 
     stmt.close(); 

     return true; 
    } 

    public void update(String current, String replacement) throws SQLException 
    { 
    stmt = con.createStatement(); 
     stmt.executeUpdate("update shows set showname = '" + replacement + "' where showname = '" + current + "'"); 
     //con.commit(); 
     stmt.close(); 
    } 

    public void remove(String data) throws SQLException 
    { 
     stmt = con.createStatement(); 
     stmt.executeUpdate("delete from shows where showname = '" + data + "'"); 
     //con.commit(); 
     stmt.close(); 
    } 

    public String[] select() throws SQLException 
    { 
     String[] shows; 
     stmt = con.createStatement(); 

     ResultSet rs = stmt.executeQuery("select * from shows"); 

     stmt.close(); 

     if(rs.getFetchSize() == 0) return null; 

     shows = new String[rs.getFetchSize()-1]; 

     //rs.first(); 
     for(int i = 0; i < rs.getFetchSize(); i++) 
     { 
      shows[i] = rs.getString("showname"); 
      rs.next(); 
     } 

     return shows; 
    } 

    @Override 
    public void clear() throws SQLException 
    { 
     stmt = con.createStatement(); 
     stmt.executeUpdate("truncate shows"); 
     //con.commit(); 
     stmt.close(); 
    } 

} 

그리고 이것은에서 확장하는 수퍼 클래스와 통신하기 위해 사용하고있는 코드는 다음

package DatabaseHelper; 

import java.sql.*; 

public abstract class DatabaseHelper<T> 
{ 
    protected Connection con = null; 
    protected Statement stmt = null; 

    /** 
    * Opens a connection to the database in question 
    * 
    * @param dbpath 
    * @throws SQLException 
    * @throws ClassNotFoundException 
    */ 
    public DatabaseHelper(String dbpath) throws SQLException 
    { 
     con = DriverManager.getConnection(dbpath); 
     System.out.println(con.getAutoCommit()); 
    } 

    /** 
    * Clears the database 
    * 
    * @throws SQLException 
    */ 
    public abstract void clear() throws SQLException; 

    /** 
    * Closes the connection to the database 
    * 
    * @throws SQLException 
    */ 
    public void close() throws SQLException 
    { 
     //con.commit(); 
     con.close(); 
    } 
} 
+0

참으로 수동으로 커밋하려고 할 때 오류가 발생하고 자동 커밋이 발생했을 때 true로 설정됩니다. enabled –

+0

프로그램을 종료하기 전에 데이터베이스 연결을 닫고 있습니까?))? – MadProgrammer

+0

확실히 GUI가 닫히면 창을 처리하기 전에 데이터베이스와의 연결을 닫는 함수가 호출됩니다. –

답변

0

내가 커밋 자동으로 설정하는 코드에서 아무것도 볼이다. 내 조언은 명시 적 커밋에 다시 추가하는 것입니다.

자동 커밋이 켜져있을 때 예외가 발생하므로 어설 션을 확인하는 방법이라고 생각합니다.

그렇지 않으면 자동 커밋을 설정하는 코드를 추가하면 가정에 의존하는 것보다 안전하다는 것을 알게됩니다.

디버그 문을 닫기 메서드 (및 기타)에 임시로 추가하여 디버그 문이 호출되는지 확인할 수도 있습니다.

그리고 이러한 메서드를 호출하는 코드를 검사 (및 표시) 할 수도 있습니다. Witout, 우리는 순서대로 무슨 일이 일어나는지 말할 수 없습니다. 우리가 알기로 당신이 끝내기 전에 그 안에 truncate이라는 메쏘드를 호출하고 있습니다 :-)

관련 문제