JDBC Hello World!
February 09, 2010 03:26:14 Last update: February 09, 2010 03:26:44
Simple JDBC code for Oracle.
import java.sql.*; public class JDBCHelloWorld { public static void main(String args) throws Exception { Connection conn = null; try { // register Oracle JDBC driver with DriverManager Class.forName("oracle.jdbc.driver.OracleDriver"); // get a connection to the database conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger"); // construct a Statement Statement stmt = conn.createStatement(); // run query ResultSet rs = stmt.executeQuery("SELECT 'Hello World' FROM DUAL;"); // iterate through the result set while (rs.next()) { System.out.println(rs.getString(1)); } } finally { // always close JDBC connection in a finally block! if (conn != null) conn.close(); } } }