Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL connection error

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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

--------------------
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 ]
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Also, please don't post the same question in more than one forum.
[ February 01, 2008: Message edited by: Paul Sturrock ]
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ooh - I'm cross that you wasted my time. Shaan patil, you are on my list
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
this wasn't really a beginner's topic anyway - it's a dup, it's locked
 
    Bookmark Topic Watch Topic
  • New Topic