Java Utililty Methods JDBC MySQL Connection

List of utility methods to do JDBC MySQL Connection

Description

The list of methods to do JDBC MySQL Connection are organized into topic(s).

Method

voidaddItem(int itemId, String itemName, int page, String tradeable)
add Item
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String sql = "INSERT INTO items" + " (id, itemName, itemId, page, tradable)" + " VALUES (NULL,?,?,?,?)";
try {
    dbConnection = getDBConnection();
    preparedStatement = dbConnection.prepareStatement(sql);
    preparedStatement.setString(1, itemName);
    preparedStatement.setInt(2, itemId);
...
voidcheckConnection(String dbFile)
check Connection
try {
    Connection connection = connections.get(dbFile);
    if (connection.isClosed())
        initializeConnection(dbFile);
} catch (SQLException e) {
    throw new RuntimeException(e);
voidcomputeAverageGeneIdsPerName()
compute Average Gene Ids Per Name
Connection connection = DriverManager.getConnection("jdbc:mysql://wanaheim:3306/user_cplake", "cplake",
        "cplake");
Map<String, Set<Integer>> name2geneIdCount = new HashMap<String, Set<Integer>>();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
        "SELECT gene.ID, gene.name, protein.name FROM GR_Names gene, GR_ProteinNames protein");
while (resultSet.next()) {
    Integer geneId = resultSet.getInt(1);
...
ConnectioncreateConnection()
create Connection
System.out.println("Creating database connection...");
String driverName = "com.mysql.jdbc.Driver";
try {
    Class.forName(driverName);
} catch (ClassNotFoundException ex) {
    System.out.println(ex.getMessage());
try {
...
ConnectioncreateConnection(String hostname, String port, String database, String username, String password)
create Connection
try {
    return DriverManager.getConnection(
            "jdbc:mysql://" + hostname + "/" + database + "?user=" + username + "&password=" + password);
} catch (SQLException e) {
return null;
voidcreateConnection(String url, String user, String password)
create Connection
try {
    Connection conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
    e.printStackTrace();
ConnectioncreateConnectionUtf8(String connString)
create Connection Utf
if (connString == null || connString.length() == 0) {
    throw new IllegalArgumentException("connString");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Properties props = new Properties();
props.put("useUnicode", "true");
props.put("characterEncoding", "utf8");
props.put("characterSetResults", "utf8");
...
voidcreateTable(String dbFile, String tableName, String tableDefinition)
create Table
checkConnection(dbFile);
Connection connection = getConnection(dbFile);
Statement statement = connection.createStatement();
String sql = "create table " + tableName + " (" + tableDefinition + ")";
System.out.println(sql);
statement.executeUpdate(sql);
statement.close();
voiddeleteTableData(String tableName)
delete Table Data
try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/harddiskbrowser", "root",
            "root");
    String queryDelete = " delete from " + tableName;
    PreparedStatement deletingStatement = con.prepareStatement(queryDelete);
    deletingStatement.execute();
    con.close();
...
voidgetAllNames()
get All Names
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/joerg", "joerg", "joerg9");
Statement statement = connection.createStatement();
ResultSet resultSet = null;
for (int taxon : restrictAnalysisToTaxonIDs) {
    String query = "SELECT g.ID, g.name FROM GR_Names AS g";
    query += " , GR_Origin as o WHERE g.id=o.id AND o.taxon=" + taxon;
    resultSet = statement.executeQuery(query);
    while (resultSet.next()) {
...