Example usage for java.sql Connection close

List of usage examples for java.sql Connection close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {/* w w  w.  j  a va  2  s  . c  o  m*/
        conn = getConnection();
        String query = "insert into nullable_table(id,string_column, int_column) values(?, ?, ?)";

        // create PrepareStatement object
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setNull(2, java.sql.Types.VARCHAR);
        pstmt.setNull(3, java.sql.Types.INTEGER);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");

    ResultSet rs = st.executeQuery("SELECT id,name FROM survey");

    // Use the result set

    // Retrieve the current values of the row from the database
    rs.refreshRow();//from w w w. java 2 s  . c  om

    rs.close();
    st.close();
    conn.close();
}

From source file:TestAccessExcel.java

public static void main(String args[]) {
    Connection conn = null;
    Statement stmt = null;/*from  w ww. j  a  va2  s  . co  m*/
    ResultSet rs = null;
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        String excelQuery = "select * from [Sheet1$]";
        rs = stmt.executeQuery(excelQuery);

        while (rs.next()) {
            System.out.println(rs.getString("BadgeNumber") + " " + rs.getString("FirstName") + " "
                    + rs.getString("LastName"));
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE );");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);/*from  w  w w. j  a v a2 s.c  o  m*/

    pstmt.executeUpdate();

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    while (rs.next()) {
        System.out.print(rs.getDate(2));
    }

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);//from   w  w w . jav a  2  s.  com
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();

    boolean supportForwardOnly = metadata.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY);
    System.out.println("supportForwardOnly = " + supportForwardOnly);

    boolean supportScrollInsensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    System.out.println("supportScrollInsensitive = " + supportScrollInsensitive);

    boolean supportScrollSensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
    System.out.println("supportScrollSensitive = " + supportScrollSensitive);
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName(DB_DRIVER);// w w w  .  j a v  a 2 s .  c  o m
    Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    CallableStatement callableStatement = null;
    String insertStoreProc = "{call insertPERSON(?,?,?,?)}";

    java.util.Date today = new java.util.Date();
    callableStatement = dbConnection.prepareCall(insertStoreProc);
    callableStatement.setInt(1, 1000);
    callableStatement.setString(2, "name");
    callableStatement.setString(3, "system");
    callableStatement.setDate(4, new java.sql.Date(today.getTime()));

    callableStatement.executeUpdate();
    System.out.println("Record is inserted into PERSON table!");
    callableStatement.close();
    dbConnection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rs = meta.getPrimaryKeys(null, null, "survey");

    java.util.List list = new java.util.ArrayList();
    while (rs.next()) {
        String columnName = rs.getString("COLUMN_NAME");
        System.out.println("getPrimaryKeys(): columnName=" + columnName);
    }//  ww  w  .  ja  va 2  s  .c  o  m

    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();

    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE);");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);//from w w  w  .j a v a  2 s . c om

    pstmt.executeUpdate();

    System.out.println(
            "The type of the first parameter is: " + pstmt.getParameterMetaData().getParameterTypeName(1));
    System.out.println(pstmt.getParameterMetaData().isNullable(1));

    conn.close();
}

From source file:net.ontopia.persistence.rdbms.DDLExecuter.java

public static void main(String[] argv) throws Exception {

    // Initialize logging
    CmdlineUtils.initializeLogging();/* w  w w  .ja  v  a 2s  .  c  o  m*/

    // Initialize command line option parser and listeners
    CmdlineOptions options = new CmdlineOptions("DDLExecuter", argv);

    // Register logging options
    CmdlineUtils.registerLoggingOptions(options);

    // Parse command line options
    try {
        options.parse();
    } catch (CmdlineOptions.OptionsException e) {
        System.err.println("Error: " + e.getMessage());
        System.exit(1);
    }

    // Get command line arguments
    String[] args = options.getArguments();
    if (args.length < 2) {
        System.err.println("Error: wrong number of arguments.");
        usage();
        System.exit(1);
    }

    String schema = args[0];
    String dbprops = args[1];
    String action = args[2];

    if (!("create".equals(action) || "drop".equals(action) || "recreate".equals(action))) {
        System.err.println("Error: unknown action: " + action);
        usage();
        System.exit(1);
    }

    // Load property file
    Properties props = new Properties();
    props.load(new FileInputStream(dbprops));

    // Get database properties from property file
    String[] platforms = StringUtils.split(props.getProperty("net.ontopia.topicmaps.impl.rdbms.Platforms"),
            ",");

    Project project = DatabaseProjectReader.loadProject(schema);

    //! //! if (dbtype.equals("generic"))
    //! //!   producer = new GenericSQLProducer(project, StringUtils.split(dbtype, ","));
    //! //! else
    //! if (dbtype.equals("mysql"))
    //!   producer = new MySqlSQLProducer(project);
    //! else if (dbtype.equals("oracle"))
    //!   producer = new OracleSQLProducer(project);
    //! else {
    //!   producer = new GenericSQLProducer(project, StringUtils.split(dbtype, ","));
    //!   //! System.err.println("Error: unknown database type: " + dbtype);
    //!   //! usage();
    //!   //! System.exit(1);
    //! }

    // Create SQL producer
    GenericSQLProducer producer = getSQLProducer(project, platforms);
    log.debug("Using SQL producer: " + producer);

    // Create database connection
    DefaultConnectionFactory cfactory = new DefaultConnectionFactory(props, false);
    Connection conn = cfactory.requestConnection();

    // Execute statements
    try {
        if ("create".equals(action))
            producer.executeCreate(conn);
        else if ("drop".equals(action))
            producer.executeDrop(conn);
        else if ("recreate".equals(action)) {
            producer.executeDrop(conn);
            producer.executeCreate(conn);
        }
        conn.commit();
    } finally {
        if (conn != null)
            conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = null;
    Properties info = new Properties();
    // info.put("proxy_type", "4"); // SSL Tunneling
    info.put("proxy_host", "[proxy host]");
    info.put("proxy_port", "[proxy port]");
    info.put("proxy_user", "[proxy user]");
    info.put("proxy_password", "[proxy password]");
    info.put("user", "[db user]");
    info.put("password", "[db pass word]");
    conn = DriverManager.getConnection("jdbc:mysql://[db host]/", info);

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("Select NOW()");
    rs.next();//from w  w w . j a v a 2s  .c o  m
    System.out.println("Data- " + rs.getString(1));
    rs.close();
    stmt.close();
    conn.close();
}