Solved

question about gettting a database connection from the DriverManager class

Posted on 2007-03-23
6
205 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
hibernate example for saving data 19 54
CSV file parsing thru Java 13 34
going to wrong jsp page 2 33
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

860 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