Example usage for java.sql Statement close

List of usage examples for java.sql Statement close

Introduction

In this page you can find the example usage for java.sql Statement close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:CreateNewType.java

public static void main(String[] args) {
    String url = "jdbc:mySubprotocol:myDataSource";

    Connection con;//from ww w  . j a  v  a 2  s  . co  m
    Statement stmt;
    try {
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement();

        String typeToCreate = null;
        String prompt = "Enter 's' to create a structured type " + "or 'd' to create a distinct type\n"
                + "and hit Return: ";
        do {
            typeToCreate = getInput(prompt) + " ";
            typeToCreate = typeToCreate.toLowerCase().substring(0, 1);
        } while (!(typeToCreate.equals("s") || typeToCreate.equals("d")));

        Vector dataTypes = getDataTypes(con, typeToCreate);

        String typeName;
        String attributeName;
        String sqlType;
        prompt = "Enter the new type name and hit Return: ";
        typeName = getInput(prompt);
        String createTypeString = "create type " + typeName;
        if (typeToCreate.equals("d"))
            createTypeString += " as ";
        else
            createTypeString += " (";

        String commaAndSpace = ", ";
        boolean firstTime = true;
        while (true) {
            System.out.println("");
            prompt = "Enter an attribute name " + "(or nothing when finished) \nand hit Return: ";
            attributeName = getInput(prompt);
            if (firstTime) {
                if (attributeName.length() == 0) {
                    System.out.print("Need at least one attribute;");
                    System.out.println(" please try again");
                    continue;
                } else {
                    createTypeString += attributeName + " ";
                    firstTime = false;
                }
            } else if (attributeName.length() == 0) {
                break;
            } else {
                createTypeString += commaAndSpace + attributeName + " ";
            }

            String localTypeName = null;
            String paramString = "";
            while (true) {
                System.out.println("");
                System.out.println("LIST OF TYPES YOU MAY USE:  ");
                boolean firstPrinted = true;
                int length = 0;
                for (int i = 0; i < dataTypes.size(); i++) {
                    DataType dataType = (DataType) dataTypes.get(i);
                    if (!dataType.needsToBeSet()) {
                        if (!firstPrinted)
                            System.out.print(commaAndSpace);
                        else
                            firstPrinted = false;
                        System.out.print(dataType.getSQLType());
                        length += dataType.getSQLType().length();
                        if (length > 50) {
                            System.out.println("");
                            length = 0;
                            firstPrinted = true;
                        }
                    }
                }
                System.out.println("");

                int index;
                prompt = "Enter an attribute type " + "from the list and hit Return:  ";
                sqlType = getInput(prompt);
                for (index = 0; index < dataTypes.size(); index++) {
                    DataType dataType = (DataType) dataTypes.get(index);
                    if (dataType.getSQLType().equalsIgnoreCase(sqlType) && !dataType.needsToBeSet()) {
                        break;
                    }
                }

                localTypeName = null;
                paramString = "";
                if (index < dataTypes.size()) { // there was a match
                    String params;
                    DataType dataType = (DataType) dataTypes.get(index);
                    params = dataType.getParams();
                    localTypeName = dataType.getLocalType();
                    if (params != null) {
                        prompt = "Enter " + params + ":  ";
                        paramString = "(" + getInput(prompt) + ")";
                    }
                    break;
                } else { // use the name as given
                    prompt = "Are you sure?  " + "Enter 'y' or 'n' and hit Return:  ";
                    String check = getInput(prompt) + " ";
                    check = check.toLowerCase().substring(0, 1);
                    if (check.equals("n"))
                        continue;
                    else {
                        localTypeName = sqlType;
                        break;
                    }
                }
            }

            createTypeString += localTypeName + paramString;

            if (typeToCreate.equals("d"))
                break;
        }

        if (typeToCreate.equals("s"))
            createTypeString += ")";
        System.out.println("");
        System.out.print("Your CREATE TYPE statement as ");
        System.out.println("sent to your DBMS:  ");
        System.out.println(createTypeString);
        System.out.println("");

        stmt.executeUpdate(createTypeString);

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:Main.java

/**
 * DOCUMENT ME!/* www .ja  v  a 2 s.co  m*/
 * 
 * @param database
 *            DOCUMENT ME!
 * @param sql
 *            DOCUMENT ME!
 * 
 * @throws SQLException
 *             DOCUMENT ME!
 */
public static void execute(String database, String sql) throws SQLException {
    Connection c = java.sql.DriverManager.getConnection("jdbc:hsqldb:file:" + database);
    Statement st = c.createStatement();
    st.execute(sql);
    st.close();
    c.close();
}

From source file:Main.java

static void close(Statement stat) {
    try {//from  www  .j av a2 s.c  o  m
        if (stat != null) {
            stat.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void cleanup(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    stmt.execute("UPDATE EMP SET SAL = SAL - 500");
    stmt.execute("COMMIT");
    stmt.close();
}

From source file:jlp.aspectj.test.MainDBCPAndC3P0test.java

static void attemptClose(Statement o) {
    try {//  w  w w .  j  a v a2  s  .  com
        if (o != null)
            o.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.buddycloud.channeldirectory.commons.db.CreateSchema.java

private static void runScript(ChannelDirectoryDataSource channelDirectoryDataSource, String sqlFile)
        throws IOException, FileNotFoundException, SQLException {
    List<String> readLines = IOUtils.readLines(new FileInputStream(sqlFile));

    Connection connection = channelDirectoryDataSource.getConnection();
    StringBuilder statementStr = new StringBuilder();

    for (String line : readLines) {
        statementStr.append(line);/*w  w  w .  j a v  a2  s.  c  om*/
        if (line.endsWith(SQL_DELIMITER)) {
            Statement statement = connection.createStatement();
            statement.execute(statementStr.toString());
            statement.close();
            statementStr.setLength(0);
        }
    }

    connection.close();
}

From source file:com.bstek.dorado.console.jdbc.JdbcUtils.java

/**
 * Close the given JDBC Statement and ignore any thrown exception.
 * This is useful for typical finally blocks in manual JDBC code.
 * @param stmt the JDBC Statement to close (may be <code>null</code>)
 *///from   w w  w . j a v  a2 s . c o  m
public static void closeStatement(Statement stmt) {
    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException ex) {
            logger.trace("Could not close JDBC Statement", ex);
        } catch (Throwable ex) {
            logger.trace("Unexpected exception on closing JDBC Statement", ex);
        }
    }
}

From source file:net.mlw.vlh.adapter.jdbc.util.JdbcUtil.java

/** Cleans up resources.
 * //from  w  w  w.  ja v  a 2 s  . c  o  m
 * @param statement Statement to close.
 * @param connection Connection to close.
 */
public static void close(Statement statement, Connection connection) {
    try {
        statement.close();
    } catch (Exception ignore) {
        LOGGER.info(ignore);
    }
    try {
        connection.close();
    } catch (Exception ignore) {
        LOGGER.info(ignore);
    }
}

From source file:Main.java

public static void close(Statement closeable) {
    if (closeable != null)
        try {//from  w w w .  j  a  v  a  2  s.c o m
            closeable.close();
        } catch (Exception ignored) {
        }
}

From source file:Main.java

public static void doSomeChanges(Connection conn) throws SQLException {
    Statement otherStmt = conn.createStatement();
    otherStmt.execute("update emp set sal = sal + 500");
    otherStmt.execute("commit");
    otherStmt.close();
}