Solved

question about gettting a database connection from the DriverManager class

Posted on 2007-03-23
6
208 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
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

635 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