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();
}
}
참으로 수동으로 커밋하려고 할 때 오류가 발생하고 자동 커밋이 발생했을 때 true로 설정됩니다. enabled –
프로그램을 종료하기 전에 데이터베이스 연결을 닫고 있습니까?))? – MadProgrammer
확실히 GUI가 닫히면 창을 처리하기 전에 데이터베이스와의 연결을 닫는 함수가 호출됩니다. –