List of usage examples for java.sql SQLException printStackTrace
public void printStackTrace()
From source file:DataSource.java
public static void main(String[] args) { try {/* w w w . ja v a 2s. c o m*/ OracleDataSource ods = new OracleDataSource(); ods.setUser("yourName"); ods.setPassword("mypwd"); ods.setDriverType("thin"); ods.setDatabaseName("ORCL"); ods.setServerName("localhost"); ods.setPortNumber(1521); Connection conn = ods.getConnection(); } catch (SQLException se) { se.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { if (conn != null) { System.out.println("Connection attempt was successful!"); try {/*from ww w . ja v a 2 s .c o m*/ conn.close(); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("Problem closing the connection"); } } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;/*from ww w . jav a 2 s . c om*/ Statement stmt = null; boolean executeResult; try { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); System.out.println("Connecting to database..."); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); stmt = conn.createStatement(); conn.setAutoCommit(false); if (!conn.getAutoCommit()) System.out.println("Auto-commit is set to false"); String sql = "INSERT INTO Location VALUES(715,'Houston')"; stmt.executeUpdate(sql); sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)"; stmt.executeUpdate(sql); conn.commit(); } catch (SQLException se) { String msg = se.getMessage(); msg = "SQLException occured with message: " + msg; System.out.println(msg); System.out.println("Starting rollback operations..."); try { conn.rollback(); } catch (SQLException se2) { se2.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;//from w ww .java 2 s. c o m Statement stmt = null; 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:Main.java
public static void main(String[] args) throws Exception { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; Connection con;/* ww w . j a v a2 s . c om*/ Statement stmt; ResultSet rs; try { Class.forName(driver); con = DriverManager.getConnection("jdbc:odbc:databaseName", "student", "student"); // Start a transaction con.setAutoCommit(false); stmt = con.createStatement(); stmt.addBatch("UPDATE EMP SET JOB = 1"); // Submit the batch of commands for this statement to the database stmt.executeBatch(); // Commit the transaction con.commit(); // Close the existing to be safe before opening a new one stmt.close(); // Print out the Employees stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM EMP"); // Loop through and print the employee number, job, and hiredate while (rs.next()) { int id = rs.getInt("EMPNO"); int job = rs.getInt("JOB"); String hireDate = rs.getString("HIREDATE"); System.out.println(id + ":" + job + ":" + hireDate); } con.close(); } catch (SQLException ex) { ex.printStackTrace(); } }
From source file:ExecuteMethod.java
public static void main(String[] args) { Connection conn = null;// ww w. ja va 2s.c o m Statement stmt = null; ResultSet rs = null; boolean executeResult; try { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); System.out.println("Connecting to database..."); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; conn = DriverManager.getConnection(jdbcUrl, "test", "mypwd"); stmt = conn.createStatement(); String sql = "INSERT INTO Employees VALUES" + "(1,'G','4351',{d '1996-12-31'},500)"; executeResult = stmt.execute(sql); processExecute(stmt, executeResult); sql = "SELECT * FROM Employees ORDER BY hiredate"; executeResult = stmt.execute(sql); processExecute(stmt, executeResult); } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] args) { try {//from w ww . j a va 2 s .c o m throwWarning(); } catch (SQLException e) { System.err.println("An SQL exception occurred: " + e); e.printStackTrace(); while ((e = e.getNextException()) != null) { System.err.println("Contained reason: " + e); } } }
From source file:net.jcreate.home.util.HomeBeanFactory.java
public static void main(String[] args) { DataSource ds = (DataSource) HomeBeanFactory.getBean("dataSource"); try {/*from w ww . ja va 2 s . c o m*/ System.out.println(ds.getConnection().getCatalog()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:net.jcreate.store.util.StoreBeanFactory.java
public static void main(String[] args) { DataSource ds = (DataSource) StoreBeanFactory.getBean("dataSource"); try {//from w w w. ja va 2 s . com System.out.println(ds.getConnection().getCatalog()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:DbUtilsUseBeanMySQL.java
public static void main(String[] args) { Connection conn = null;//from w w w .j ava 2s .c om String jdbcURL = "jdbc:mysql://localhost/octopus"; String jdbcDriver = "com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; try { DbUtils.loadDriver(jdbcDriver); conn = DriverManager.getConnection(jdbcURL, user, password); QueryRunner qRunner = new QueryRunner(); List beans = (List) qRunner.query(conn, "select id, name from animals_table", new BeanListHandler(Employee.class)); for (int i = 0; i < beans.size(); i++) { Employee bean = (Employee) beans.get(i); bean.print(); } } catch (SQLException e) { // handle the exception e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }