public abstract class DBConnector extends Object
Modifier and Type | Field and Description |
---|---|
protected Connection |
con |
protected String |
databaseName |
protected String |
databaseUrl |
protected String |
documentTableName |
protected String |
documentTermsTableName |
static Hashtable<String,Class<?>> |
fieldsDocument |
static Hashtable<String,Class<?>> |
fieldsDocumentTerms |
static Hashtable<String,Class<?>> |
fieldsTerm |
protected String |
password |
(package private) Statement |
statement |
protected String |
statementEndCharacter |
protected String |
tableNamePrefix |
protected String |
termTableName |
protected String |
user |
Constructor and Description |
---|
DBConnector(String databaseUrl,
String databaseName,
String user,
String password,
String tableNamePrefix) |
Modifier and Type | Method and Description |
---|---|
protected void |
checkDBConnection()
Checks whether the connection is open, and opens a new connection if needed.
|
void |
closeConnection()
Closes the DB connection.
|
boolean |
connect() |
protected void |
createTable(String tableName,
Hashtable<String,Class<?>> fields,
String primaryKey,
String uniqueKey)
Creates a database table.
|
int |
doInsert(String tableName,
String[] columns,
Object[] values)
Inserts a new row into a table.
|
int |
doInsert(String tableName,
String[] columns,
Object[][] values)
Inserts multiple rows into the table.
|
int |
doInsert(String tableName,
String column,
Object value) |
int |
doInsert(String tableName,
String column,
String value) |
ResultSet |
executeSelect(String query)
Executes a SELECT Statement and returns a ResultSet
|
int |
executeUpdate(String query)
Executes an update statement.
|
String |
getDocumentTableName() |
String |
getDocumentTermsTableName() |
private StringBuffer |
getInsertQuery(String tableName,
String[] columns,
Object[] values) |
private StringBuffer |
getInsertQuery(String tableName,
String column,
Object value) |
private StringBuffer |
getInsertQuery(String tableName,
String column,
String value) |
PreparedStatement |
getPreparedStatement(String sql)
Creates a prepared statement for the given query
|
String |
getTermTableName() |
abstract Connection |
openConnection() |
void |
setupTables()
Prepares the database by creating the needed tables.
|
Statement statement
protected Connection con
protected String databaseName
protected String databaseUrl
protected String password
protected String tableNamePrefix
protected String user
protected String documentTableName
protected String documentTermsTableName
protected String termTableName
protected String statementEndCharacter
protected void checkDBConnection() throws SQLException
SQLException
- if connection to DB failspublic boolean connect() throws SQLException
SQLException
public void closeConnection() throws SQLException
SQLException
- if the connection is closed or in auto-commit mode.public abstract Connection openConnection() throws SQLException
SQLException
public int doInsert(String tableName, String[] columns, Object[] values) throws SQLException
tableName
- The table to insert.columns
- The names of the columns.values
- The values, in the same order as the column namesSQLException
- if the insert failspublic int doInsert(String tableName, String column, Object value) throws SQLException
SQLException
public int doInsert(String tableName, String column, String value) throws SQLException
SQLException
public int doInsert(String tableName, String[] columns, Object[][] values) throws SQLException
SQLException
doInsert(String, String[], Object[])
public PreparedStatement getPreparedStatement(String sql) throws SQLException
SQLException
private StringBuffer getInsertQuery(String tableName, String[] columns, Object[] values)
private StringBuffer getInsertQuery(String tableName, String column, Object value)
private StringBuffer getInsertQuery(String tableName, String column, String value)
public ResultSet executeSelect(String query) throws SQLException
query
- StringSQLException
- if a db-error occures.public int executeUpdate(String query) throws SQLException
query
- SQL StatementSQLException
- if a db-error occures.public String getDocumentTableName()
public String getDocumentTermsTableName()
public String getTermTableName()
protected void createTable(String tableName, Hashtable<String,Class<?>> fields, String primaryKey, String uniqueKey) throws SQLException
tableName
- name of the tablefields
- the columns of the tableprimaryKey
- primary keyuniqueKey
- additional unique keySQLException
- if a db-error occures.public void setupTables() throws SQLException
SQLException
- if a db-error occures.