Solved

question about gettting a database connection from the DriverManager class

Posted on 2007-03-23
6
206 Views
Last Modified: 2013-11-23
Hi all,

When I try and get a connection using the driverManager class and I pass in an incorrect suprotocol name I get an sqlexception which is fine. But if my subname is incorrect I don't get an exception?
Why is this?

        try
        {
String subprotocol = "correct:";
String subname="notvalid";
            con = DriverManager.getConnection(subprotocol+subname);
            con.setAutoCommit(true);
        }
        catch (SQLException se)
        {
            //print some loggging info
        }

Thanks
0
Comment
Question by:mousemat212
6 Comments
 
LVL 11

Expert Comment

by:elfe69
ID: 18779177
If I test what you explain, I always receive an SQLException:

java.sql.SQLException: No suitable driver

Can you provide an example with the connection string you are using ?
0
 

Author Comment

by:mousemat212
ID: 18779660

Thanks for your reply,

Ok here is an example which does not throw an exception but it should since subname is set to "notvalid"?

Thank you,

        try
        {
String subprotocol = "jdbc:apache:commons:dbcp:";
String subname="notvalid";
            con = DriverManager.getConnection(subprotocol+subname);
            con.setAutoCommit(true);
        }
        catch (SQLException se)
        {
            //print some loggging info
        }

0
 
LVL 11

Expert Comment

by:elfe69
ID: 18779847
It is not a DriverManager issue,a DBCP issue, maybe is an Exception rather than an SQLException thrown ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
gatorvip earned 125 total points
ID: 18781420
You do have something that prints the error info and not just the comment, right?
***
try
{
     String subprotocol = "jdbc:apache:commons:dbcp:";
     String subname="notvalid";
     Connection  con = DriverManager.getConnection(subprotocol+subname);
     con.setAutoCommit(true);
}
catch (SQLException se)
{
   se.printStackTrace();
}

***

java.sql.SQLException: No suitable driver found for jdbc:apache:commons:dbcp:not
valid
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
....
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18784014
Try with something like that to see if you do not get another error:

try {
    String subprotocol = "correct:";
    String subname="notvalid";
    con = DriverManager.getConnection(subprotocol+subname);
    con.setAutoCommit(true);
} catch (SQLException se) {
    se.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18785759
Yeah I guess it has to print an exception.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question