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) { Connection conn = null; Statement stmt = null;/* ww w . j a v a 2 s .c om*/ try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } }
From source file:Employee.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); Map map = conn.getTypeMap();/*from ww w . j av a 2s . com*/ map.put("EMP_DATA", Class.forName("Employee")); conn.setTypeMap(map); ResultSet rs = stmt.executeQuery("SELECT * from Emp"); Employee employee; while (rs.next()) { int empId = rs.getInt("EmpId"); employee = (Employee) rs.getObject("Emp_Info"); System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN); System.out.print(", Name: " + employee.FirstName + " " + employee.LastName); System.out.println( ", Yearly Salary: $" + employee.Salary + " Monthly Salary: " + employee.calcMonthlySalary()); } 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 resultSet = st.executeQuery("SELECT * FROM survey"); // get concurrency of the ResultSet object int concurrency = resultSet.getConcurrency(); if (concurrency == ResultSet.CONCUR_UPDATABLE) { System.out.println("ResultSet is updatable"); } else {//ww w . j a va2 s.co m System.out.println("ResultSet is not updatable"); } resultSet.close(); 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')"); String tableName = "survey"; String query = "select count(*) from " + tableName; Statement stmt = null;/*from w ww .j a v a 2 s . c o m*/ ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); // Step-2: identify the stored procedure String simpleProc = "{ call simpleproc(?) }"; // Step-3: prepare the callable statement CallableStatement cs = conn.prepareCall(simpleProc); // Step-4: register output parameters ... cs.registerOutParameter(1, java.sql.Types.INTEGER); // Step-5: execute the stored procedures: proc3 cs.execute();//from w w w . ja v a 2 s . co m // Step-6: extract the output parameters int param1 = cs.getInt(1); System.out.println("param1=" + param1); // Step-7: get ParameterMetaData ParameterMetaData pmeta = cs.getParameterMetaData(); if (pmeta == null) { System.out.println("Vendor does not support ParameterMetaData"); } else { System.out.println(pmeta.getParameterType(1)); } 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')"); DatabaseMetaData meta = conn.getMetaData(); if (meta.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) { System.out.println("type name=TYPE_FORWARD_ONLY"); }//from w w w.j av a 2s. c om if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) { System.out.println("type name=TYPE_SCROLL_INSENSITIVE"); } if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) { System.out.println("type name=TYPE_SCROLL_SENSITIVE"); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; try {/* w ww . j a v a 2s .c o m*/ Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); // Get the MetaData DatabaseMetaData metaData = conn.getMetaData(); // Get driver information System.out.println("Driver Informaion"); System.out.println(metaData.getDriverName()); System.out.println(metaData.getDriverVersion()); // Get schema information System.out.println("Schemas"); ResultSet schemas = metaData.getSchemas(); while (schemas.next()) { System.out.println(schemas.getString(1)); } // Get table information System.out.println("Tables"); ResultSet tables = metaData.getTables("", "", "", null); while (tables.next()) { System.out.println(tables.getString(3)); } conn.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.kylinolap.query.QueryCli.java
public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OPTION_METADATA);//from w w w .j a v a 2 s.c om options.addOption(OPTION_SQL); CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); KylinConfig config = KylinConfig .createInstanceFromUri(commandLine.getOptionValue(OPTION_METADATA.getOpt())); String sql = commandLine.getOptionValue(OPTION_SQL.getOpt()); Class.forName("net.hydromatic.optiq.jdbc.Driver"); File olapTmp = OLAPSchemaFactory.createTempOLAPJson(null, config); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:optiq:model=" + olapTmp.getAbsolutePath()); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int n = 0; ResultSetMetaData meta = rs.getMetaData(); while (rs.next()) { n++; for (int i = 1; i <= meta.getColumnCount(); i++) { System.out.println(n + " - " + meta.getColumnLabel(i) + ":\t" + rs.getObject(i)); } } } finally { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { 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')"); String tableName = "survey"; String query = "select * from " + tableName + " where 1=0"; Statement stmt = null;//from www.j a v a 2s. c o m ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(query); System.out.println("Exist"); ; } catch (Exception e) { // table does not exist or some other problem //e.printStackTrace(); System.out.println("Not Exist"); } 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_SENSITIVE, ResultSet.CONCUR_READ_ONLY); 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 * FROM survey"); // Move cursor forward while (rs.next()) { String id = rs.getString("id"); System.out.println(id);/* w ww .j a v a2 s .co m*/ } // Move cursor backward while (rs.previous()) { // Get data at cursor String id = rs.getString("id"); System.out.println(id); } rs.close(); st.close(); conn.close(); }