Bookmark Topic Watch Topic
  • New Topic

SQL connection error

 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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

--------------------
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • 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?
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Report post to moderator
Ooh - I'm cross that you wasted my time. Shaan patil, you are on my list
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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