List of usage examples for java.sql Connection close
void close() throws SQLException;
Connection
object's database and JDBC resources immediately instead of waiting for them to be automatically released. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null;/*ww w .j av a2 s . c om*/ DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTypeInfo(); while (rs.next()) { // Get the database-specific type name String typeName = rs.getString("TYPE_NAME"); // Get the java.sql.Types type to which this // database-specific type is mapped short dataType = rs.getShort("DATA_TYPE"); // Get the name of the java.sql.Types value. System.out.println("type name=" + typeName); System.out.println("dataType=" + dataType); System.out.println("jdbcType=" + dataType); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement();// w w w . j a v a 2 s. com ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column MetaData "); System.out.println("column number " + i); // indicates whether the designated column is // automatically numbered, thus read-only. System.out.println(rsMetaData.isAutoIncrement(i)); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); Statement st = conn.createStatement(); // st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); PreparedStatement pstmt = null; ParameterMetaData paramMetaData = null; String query = "select * from survey where id > ? "; System.out.println("conn=" + conn); pstmt = conn.prepareStatement(query); paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {/*from w ww .ja va 2 s .c o m*/ System.out.println("db vendor supports ParameterMetaData"); // find out the number of dynamic parameters int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); } 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_INSENSITIVE, 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')"); st = conn.createStatement();//from www. j a v a 2 s . c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); String tableName = rsMetaData.getTableName(i); System.out.println(columnName); System.out.println(tableName); } rs.close(); st.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_INSENSITIVE, 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 = conn.createStatement();/* w ww . ja v a 2 s.c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); int rsType = rs.getType(); if (rsType == java.sql.ResultSet.TYPE_FORWARD_ONLY) { System.out.println("java.sql.ResultSet.TYPE_FORWARD_ONLY"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_SENSITIVE"); } else { // it is an error } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) { Connection conn = null; Statement stmt = null;//from w ww . ja v a2 s .c om ResultSet rs = null; try { conn = getConnection(); String query = "select id, name from employees"; stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(query); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } rs.first(); rs.deleteRow(); rs.beforeFirst(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } 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 = getMySqlConnection(); System.out.println("Got Connection."); ResultSet rsColumns = null;/*from w w w. ja v a 2s . co m*/ DatabaseMetaData meta = conn.getMetaData(); rsColumns = meta.getColumns(null, "%", "code", "%"); while (rsColumns.next()) { String columnType = rsColumns.getString("TYPE_NAME"); String columnName = rsColumns.getString("COLUMN_NAME"); int size = rsColumns.getInt("COLUMN_SIZE"); int nullable = rsColumns.getInt("NULLABLE"); int position = rsColumns.getInt("ORDINAL_POSITION"); System.out.println("column name=" + columnName); System.out.println("type=" + columnType); System.out.println("size=" + size); if (nullable == DatabaseMetaData.columnNullable) { System.out.println("nullable is true"); } else { System.out.println("nullable is false"); } System.out.println("position" + position); } conn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null; Statement s = null;// ww w . ja v a 2 s. co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;" + "Trusted_Connection=yes;" + "Database=myDb"); s = conn.createStatement(); s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1"); ResultSet rs = s.getResultSet(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(String.format("-- Column %d --", i)); System.out.println(String.format("Column name: %s", rsmd.getColumnName(i))); System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i))); System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i))); System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i))); System.out.println(); } s.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; java.sql.Array sqlArray = null; conn = getOracleConnection();/*w w w. j a va 2 s .c om*/ // For oracle you need an array descriptor specifying // the type of the array and a connection to the database // the first parameter must match with the SQL ARRAY type created ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn); // then obtain an Array filled with the content below String[] content = { "v1", "v2", "v3", "v4" }; sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content); String query = "insert into CHAR_ARRAY_TABLE(id, array) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setArray(2, sqlArray); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); System.out.println("--Demo_PreparedStatement_SetArray end--"); pstmt.close(); conn.close(); }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.dao.ExtractDataSetForCommonDBUnit.java
public static void main(final String[] args) throws Exception { Connection jdbcConnection = null; FileOutputStream fileOutputStream = null; try {//from ww w. j ava 2 s . c o m // database connection Class.forName("org.postgresql.Driver"); jdbcConnection = DriverManager.getConnection(DBURL, DBUser, DBPass); final IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); ITableFilter filter = new DatabaseSequenceFilter(connection); DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory()); QueryDataSet ds = new QueryDataSet(connection, true); ds.addTable("log"); String outputPath = DB_DUMP_FOLDER + DB_DUMP_FILE; //noinspection IOResourceOpenedButNotSafelyClosed fileOutputStream = new FileOutputStream(outputPath); FlatXmlDataSet.write(ds, fileOutputStream); } finally { if (jdbcConnection != null) { jdbcConnection.close(); } IOUtils.closeQuietly(fileOutputStream); } }