Solved

question about gettting a database connection from the DriverManager class

Posted on 2007-03-23
6
200 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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:mayankeagle
ID: 18785759
Yeah I guess it has to print an exception.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now