JDBC 3: Java Database Connectivity

This section reviews what has been covered thus far:

Listing 6-13 contains an example that summarizes all these steps. This example opens a connection to the database, sends a SQL query to a database with a time-out value of 180 seconds and then closes the statement and connection.

Listing 6-13: Executing a Statement

// executing a statement import java.sql.*; class SimpleExample { public static void main(String args[]) { String url = "jdbc:odbc:mysource"; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection myConnection = DriverManager.getConnection(url, "javauser", "hotjava"); Statement myStatement = myConnection.createStatement(); myStatement.setQueryTimeout(180); ResultSet rs = myStatement.executeQuery("SELECT * FROM employees"); myStatement.close(); myConnection.close(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } }

Категории