Skip to content

Instantly share code, notes, and snippets.

@64lines
Created December 23, 2013 05:24
Show Gist options
  • Save 64lines/8092078 to your computer and use it in GitHub Desktop.
Save 64lines/8092078 to your computer and use it in GitHub Desktop.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class SQLiteConnection {
public static final String STRING_CONNECTION = "jdbc:sqlite:";
public Connection connection;
public Statement statement;
private String databaseFilePath = "application_db.dat";
/*******************************************
* Get database file path.
* @return String
*******************************************/
public String getDatabaseFilePath() {
return databaseFilePath;
}
/*******************************************
* Set database file path.
* @return String
*******************************************/
public void setDatabaseFilePath(String databaseFilePath) {
this.databaseFilePath = databaseFilePath;
}
public SQLiteConnection() {
try {
Class.forName("org.sqlite.JDBC");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/************************************************
* Create a connection to database, this must
* to execute before make any query or update.
************************************************/
public void connect() {
String connectionString = STRING_CONNECTION + databaseFilePath;
try {
connection = DriverManager.getConnection(connectionString);
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
/********************************************************
* Close database connection, this must to be executed
* when querys and updates are done.
********************************************************/
public void disconnect() {
try {
connection.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/*************************************************
* Execute an update to database like INSERT,
* CREATE or UPDATE
* @param strUpdateStatement String type
* @return boolean type
*************************************************/
public boolean executeUpdate(String strUpdateStatement) {
boolean status = false;
try {
statement.executeUpdate(strUpdateStatement);
status = true;
} catch (SQLException e) {
e.printStackTrace();
}
return status;
}
public ResultSet executeQuery(String strQueryStatement) {
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery(strQueryStatement);
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment