Question : Java: JDBC: config questions

Hello Experts,

I got this java class connection example from www.idevelopment.info

I have questions about the class fields:

//is this a path in my computer?
final String driverClass = "oracle.jdbc.driver.OracleDriver"

//it looks like credentials to connect to Oracle; but
//   don't I need to use instant client to do this?
final String connectionURLThin = "jdbc:oracle:thin:@jeffreyh3:1521:CUSTDB";

thx experts.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
// -----------------------------------------------------------------------------
// ConnectionExample.java
// -----------------------------------------------------------------------------

/*
 * =============================================================================
 * Copyright (c) 1998-2009 Jeffrey M. Hunter. All rights reserved.
 * 
 * All source code and material located at the Internet address of
 * http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and
 * is protected under copyright laws of the United States. This source code may
 * not be hosted on any other site without my express, prior, written
 * permission. Application to host any of the material elsewhere can be made by
 * contacting me at [email protected].
 *
 * I have made every effort and taken great care in making sure that the source
 * code and other content included on my web site is technically accurate, but I
 * disclaim any and all responsibility for any loss, damage or destruction of
 * data or any other property which may arise from relying on it. I will in no
 * case be liable for any monetary damages arising from such loss, damage or
 * destruction.
 * 
 * As with any code, ensure to test this code in a development environment 
 * before attempting to run it in production.
 * =============================================================================
 */
 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * -----------------------------------------------------------------------------
 * The following class provides an example of using JDBC to connect to an
 * Oracle database. The one phase of JDBC that is the most difficult and 
 * hard to achieve portability, is when connecting. This phase requires
 * that the Java database application specify driver-specific information that
 * JDBC requires in the form of a database URL.
 * 
 * If you run into problems while trying to simply make a connection, check
 * if they match any of the following:
 * 
 *    Connection fails with the message "Class no found"
 *    --------------------------------------------------
 *    This message usually results from not having the JDBC driver in your
 *    CLASSPATH. Ensure that if you are including *.zip and *.jar in your
 *    CLASSPATH, that your enter them explicity. If you put all of your *.class
 *    files and the ojdbc14.jar file containing the Oracle-JDBC driver into
 *    /u02/lib, your CLASSPATH should read /u02/lib:/u02/lib/ojdbc14.jar.
 *  
 *    Connection fails with the message "Driver no found"
 *    ---------------------------------------------------
 *    In this case, you did not register the JDBC driver with the DriverManager
 *    class. This example application describes several ways to register a
 *    JDBC driver. Sometimes developers using the Class.forName() method of
 *    registering a JDBC driver encounter an inconsistency between the JDBC
 *    specification and some JVM implementations. You should thus use the
 *    Class.forName().netInstance() method as a workaround.
 * 
 * When attempting to make a database connection, your application must first
 * request a java.sql.Connection implementation from the DriverManager. You will
 * also use a database URL and whatever properties your JDBC driver requires
 * (generally a user ID and password). The DriverManager in turn will search
 * through all of the known java.sql.Driver implementations for the one that
 * connects with the URL you provided. If it exhausts all the implementations
 * without finding a match, it throws an exception back to your application.
 * 
 * Once a Driver recognizes your URL, it creates a database connection using
 * the properties you specified. It then provides the DriverManager with a 
 * java.sql.Connection implementation representing that database connection. The
 * DriverManager then passes that Connection object back to the application.
 * 
 * At this point, you may be wondering how the JDBC DriverManager learns about
 * a new driver implementation. The DriverManager actually keeps a list of
 * classes that implement that java.sql.Driver interface. Something needs to 
 * register the Driver implementation for any potential database drivers it
 * might require with the DriverManager. JDBC requires a Driver class to
 * register itself with the DriverManager when it is initiated. The act of
 * instantiating a Driver class thus enters it in the DriverManager's list.
 * 
 * This class (ConnectionExample) provides three ways to register a driver.
 * -----------------------------------------------------------------------------
 * @version 1.0
 * @author  Jeffrey M. Hunter  ([email protected])
 * @author  http://www.idevelopment.info
 * -----------------------------------------------------------------------------
 */

public class ConnectionExample {

    final String driverClass        = "oracle.jdbc.driver.OracleDriver";
    final String connectionURLThin  = "jdbc:oracle:thin:@jeffreyh3:1521:CUSTDB";
    final String connectionURLOCI   = "jdbc:oracle:oci8:@CUSTDB_JEFFREYH3";
    final String userID             = "scott";
    final String userPassword       = "tiger";
    final String queryString        = "SELECT" +
                                      "    user " +
                                      "  , TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS') " +
                                      "FROM dual";

    /**
     * The following method provides an example of how to connect to a database
     * by registering the JDBC driver using the DriverManager class. This method
     * requires you to hardcode the loading of a Driver implementation in
     * your application. this alternative is the least desirable since it
     * requires a rewrite and recompile if your database or database driver
     * changes.
     */
    public void driverManager() {

        Connection con = null;
        Statement stmt = null;
        ResultSet rset = null;

        try {

            System.out.print("\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("| USING DriverManager CLASS     |\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("\n");

            System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

            System.out.print("  Connecting to        -> " + connectionURLThin + "\n");
            con = DriverManager.getConnection(connectionURLThin, userID, userPassword);
            System.out.print("  Connected as         -> " + userID + "\n");

            System.out.print("  Creating Statement...\n");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSet...\n");
            rset = stmt.executeQuery(queryString);

            while (rset.next()) {
                System.out.println("  Results...");
                System.out.println("      User             -> " + rset.getString(1));
                System.out.println("      Sysdate          -> " + rset.getString(2));
            }

            System.out.print("  Closing ResultSet...\n");
            rset.close();

            System.out.print("  Closing Statement...\n");
            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();
    
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

        } finally {

            if (con != null) {
                try {
                    System.out.print("  Closing down all connections...\n\n");
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * The following method provides an example of how to connect to a database
     * by registering the JDBC driver using the jdbc.drivers property. The
     * DriverManager will load all classes listed in this property
     * automatically. This alternative works well for applications with a 
     * command-line interface, but might not be so useful in GUI applications
     * and applets. This is because you can specify properties at the command
     * line.
     */
    public void jdbcDriversProperty() {

        Connection con = null;
        Statement stmt = null;
        ResultSet rset = null;

        try {

            System.out.print("\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("| USING jdbc.drivers PROPERTY   |\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("\n");

            System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
            System.setProperty("jdbc.drivers", driverClass);

            System.out.print("  Connecting to        -> " + connectionURLThin + "\n");
            con = DriverManager.getConnection(connectionURLThin, userID, userPassword);
            System.out.print("  Connected as         -> " + userID + "\n");

            System.out.print("  Creating Statement...\n");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSet...\n");
            rset = stmt.executeQuery(queryString);

            while (rset.next()) {
                System.out.println("  Results...");
                System.out.println("      User             -> " + rset.getString(1));
                System.out.println("      Sysdate          -> " + rset.getString(2));
            }

            System.out.print("  Closing ResultSet...\n");
            rset.close();

            System.out.print("  Closing Statement...\n");
            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }


        } finally {

            if (con != null) {
                try {
                    System.out.print("  Closing down all connections...\n\n");
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * The following method provides an example of how to connect to a database
     * by registering the JDBC driver using the Class.forName() method. This
     * complex expression is a tool for dynamically creating an instance of
     * a class when you have some variable representing the class name. Because
     * a JDBC driver is required to register itself whenever its static
     * initializer is called, this expression has the net effect of registering
     * your driver for you. 
     *
     *      NOTE: When using Class.forName("classname"), the JVM is supposed to
     *            be sufficient. Unfortunately, some Java virtual machines do
     *            not actuall call the static intitializer until an instance of
     *            a class is created. As a result, newInstance() should be 
     *            called to guarantee that the static initializer is run for
     *            all virtual machines.
     *
     * This method is by far the BEST in that it does not require hardcoded 
     * class names and it runs well in all Java environments. In real-world
     * applications, you should use this method along with a properties file
     * from which you load the name of the driver.
     * 
     */
    public void classForName() {

        Connection con = null;
        Statement stmt = null;
        ResultSet rset = null;
            
        try {

            System.out.print("\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("| USING Class.forName()         |\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("\n");


            System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
            Class.forName(driverClass).newInstance();

            System.out.print("  Connecting to        -> " + connectionURLThin + "\n");
            con = DriverManager.getConnection(connectionURLThin, userID, userPassword);
            System.out.print("  Connected as         -> " + userID + "\n");

            System.out.print("  Creating Statement...\n");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSet...\n");
            rset = stmt.executeQuery(queryString);

            while (rset.next()) {
                System.out.println("  Results...");
                System.out.println("      User             -> " + rset.getString(1));
                System.out.println("      Sysdate          -> " + rset.getString(2));
            }

            System.out.print("  Closing ResultSet...\n");
            rset.close();

            System.out.print("  Closing Statement...\n");
            stmt.close();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        
        } catch (InstantiationException e) {

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

        } finally {

            if (con != null) {
                try {
                    System.out.print("  Closing down all connections...\n\n");
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * The following method provides an example of how to connect to a database
     * using the OCI JDBC Driver.
     * 
     */
    public void jdbcOCIDriver() {

        Connection con = null;
        Statement stmt = null;
        ResultSet rset = null;
            
        try {

            System.out.print("\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("| USING OCI Driver              |\n");
            System.out.print("+-------------------------------+\n");
            System.out.print("\n");


            System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
            Class.forName(driverClass).newInstance();

            System.out.print("  Connecting to        -> " + connectionURLOCI + "\n");
            con = DriverManager.getConnection(connectionURLOCI, userID, userPassword);
            System.out.print("  Connected as         -> " + userID + "\n");

            System.out.print("  Creating Statement...\n");
            stmt = con.createStatement ();

            System.out.print("  Opening ResultsSet...\n");
            rset = stmt.executeQuery(queryString);

            while (rset.next()) {
                System.out.println("  Results...");
                System.out.println("      User             -> " + rset.getString(1));
                System.out.println("      Sysdate          -> " + rset.getString(2));
            }

            System.out.print("  Closing ResultSet...\n");
            rset.close();

            System.out.print("  Closing Statement...\n");
            stmt.close();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        
        } catch (InstantiationException e) {

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }

        } finally {

            if (con != null) {
                try {
                    System.out.print("  Closing down all connections...\n\n");
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     * @exception java.lang.InterruptedException
     *            Thrown from the Thread class.
     */
    public static void main(String[] args)
            throws java.lang.InterruptedException {

        ConnectionExample conExample = new ConnectionExample();

        conExample.classForName();

        Thread.sleep(5000);
        
        conExample.jdbcDriversProperty();

        Thread.sleep(5000);
        
        conExample.driverManager();
        
        Thread.sleep(5000);

        conExample.jdbcOCIDriver();

    }

}

Answer : Java: JDBC: config questions

//is this a path in my computer?
final String driverClass = "oracle.jdbc.driver.OracleDriver"

That is the full name of the JDBC Driver

//it looks like credentials to connect to Oracle; but
//   don't I need to use instant client to do this?
final String connectionURLThin = "jdbc:oracle:thin:@jeffreyh3:1521:CUSTDB";

You don't need instant client, the format is "jdbc:oracle:thin@host:port:db_name"
Random Solutions  
 
programming4us programming4us