File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes SQL connection error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

SQL connection error

Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
Hi , I am not able to compile the following code, What might be the error ?


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DbConnection
{
public static Connection getConnect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc sn","scott","tiger");
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("WE have cauaght ClassNotFoundException");
e.printStackTrace();
}
catch(SQLException e)
{
System.out.println("WE have cauaght ClassNotFoundException");
e.printStackTrace();
}

}
}




---------- Compile Result --------------------------------
DbConnection.java:27: missing return statement
}
^
1 error
Output completed (1 sec consumed) - Normal Termination

what might be the error ?

Thanks

--------------------


"You cannot change the truth but truth can change you "
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Firstly - 2 tips: use [code] tags, to post code, and check the 'Disable smilies in this post', to avoid the and . You can edit the post if you like.

Secondly - the answer - you're missing a return. You've declared the method to expect a return type of Connection - at the end try block you do return one, but what happens if one of your exceptions is thrown?
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hi Shaan.
Next time you post a code use UBB please. It makes it tidier.
What I can see from your program is that your method getConnect() returns a Connection. Therefore, under whatever circumstance, you must return a value. Say from example the program goes through catch(ClassNotFoundException e), then you aren't returning anything. Try something like this:



Note: You must declare "con" outside the try statement
[ February 01, 2008: Message edited by: Olivier Legat ]

Olly
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Sorry my mistake Shaan, you need to initialize "con" as well as declare it (because like I said you've got to return something, so got to initialize "con" to return some value.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36464
    
  15
There is an alternative, which is not to catch the Exception in the same method at all, but throw it back to the calling method.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Also, please don't post the same question in more than one forum.
[ February 01, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Ooh - I'm cross that you wasted my time. Shaan patil, you are on my list
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
this wasn't really a beginner's topic anyway - it's a dup, it's locked


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SQL connection error
 
Similar Threads
JDBC connection in different machine
Stored proc not found...
LoginBean doesn't compile
SQL connection error
need java logic