Created
December 23, 2013 05:24
-
-
Save 64lines/8092078 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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