KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JDBCTest


1 /*
2     This file is part of the Tutorial chapter in "Getting Started with Enhydra".
3     It is not meant to be used in any other context.
4
5     Simple program to test JDBC connectivity. Assumes you have created and populated
6     a table called LE_TUTORIAL_DISCS in your database.
7     Edit the connection string in the call to DriverManager.getConnection()/
8
9 */

10
11 import java.sql.*;
12
13 public class JDBCTest {
14     public static void main( String JavaDoc[] args ) {
15         Connection con = null;
16         Statement stmt = null;
17         ResultSet rs = null;
18 // Load the driver, get a connection, create statement, run query, and print.
19
try {
20             // To test with a different database, replace JDBC driver information below
21
Class.forName("org.enhydra.instantdb.jdbc.idbDriver");
22             /* To test with a different database, provide appropriate connection data
23                            (e.g., database connection string, username, and password) */

24             con = DriverManager.getConnection("jdbc:idb:/enhydra/myapps/data/simpleApp.prp");
25             stmt = con.createStatement();
26             rs = stmt.executeQuery("SELECT * FROM LE_TUTORIAL_DISCS");
27             rs.next();
28             System.out.println("Title = " + rs.getString("title") + " -- Artist = " + rs.getString("artist"));
29                         con.close();
30
31         }
32         catch(ClassNotFoundException JavaDoc e) {
33             System.err.println("Couldn't load the driver: " + e.getMessage());
34         }
35         catch(SQLException e) {
36             System.err.println("SQLException caught: " + e.getMessage());
37         }
38     }
39 }
Popular Tags