Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

question about gettting a database connection from the DriverManager class

Posted on 2007-03-23
6
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Accepted Solution

by:
gatorvip earned 500 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

Independent Software Vendors: 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!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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