/**
* This class is a collection of database utility functions which may be
* useful to any other class that needs to interact with a database. If
* you write a database method you think will be useful to others, include
* it here.
*/
package com.visitrend.ndvis.dbutils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.netbeans.api.db.explorer.ConnectionManager;
import org.netbeans.api.db.explorer.DatabaseConnection;
/**
*
* @author dkramer
*/
public class DatabaseUtilities {
private static final String CONNECT_ERROR =
"Error getting database connection: ";
private static final String LOAD_DRIVER_ERROR =
"Error loading database driver: ";
private static final String DERBY_DRIVER =
"org.apache.derby.jdbc.ClientDriver";
/**
* Utility function to get a database connection given a name (e.g. "derby")
* and a project name (e.g., "ndvis")
* @param dbName the name of the database to look for
* @param projName the name of the project to look for
* @return the valid database connection, or null if a connection could
* not be successfully opened
*/
public static Connection getDBConnection(String dbName, String projName) {
ConnectionManager cMgr = ConnectionManager.getDefault();
DatabaseConnection[] dbconnList = cMgr.getConnections();
DatabaseConnection dbconn = null;
// find a connection for given database & project, if one exists
System.out.println("DBConnList size: " + dbconnList.length);
if (dbconnList.length > 0) {
for (DatabaseConnection con : dbconnList) {
String name = con.getDisplayName().toLowerCase();
System.out.println(con.getDisplayName());
if (name.contains(dbName) && name.contains(projName)) {
dbconn = con;
}
}
}
if (dbconn == null) {
// TODO: create a connection here if none found
return null;
} else {
System.out.println("Got db connection: " + dbconn.getName());
// System.out.println(" Driver: " + dbconn.getJDBCDriver().getName());
// System.out.println(" URL: " + dbconn.getDatabaseURL());
// System.out.println(" user: " + dbconn.getUser());
// System.out.println(" schema: " + dbconn.getSchema());
}
Connection connection = dbconn.getJDBCConnection();
if (connection == null) {
cMgr.showConnectionDialog(dbconn);
}
return connection;
}
/**
* Utility function to determine whether a database table exists, given a valid connection
* @param connection the connection to use to communicate with the database
* @param tableName the name of the table to find (e.g., "pcap")
* @return <code>true</code> if the table exists, <code>false</code> otherwise
* @throws SQLException if any error occurs while communicating with database
*/
public static boolean tableExists(Connection connection, String tableName) throws SQLException {
boolean answer = false;
DatabaseMetaData metadata = null;
metadata = connection.getMetaData();
String[] names = {"TABLE"};
ResultSet tableNames = metadata.getTables(null, null, null, names);
while (tableNames.next()) {
if (tableNames.getString("TABLE_NAME").equalsIgnoreCase(tableName)) {
answer = true;
break;
}
}
return answer;
}
}