Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
    Bookmark Topic Watch Topic
  • New Topic

SQL connection error  RSS feed

 
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

--------------------
 
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?
 
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.
 
Marshal
Posts: 58454
178
  • 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.
 
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
 
author
Sheriff
Posts: 8977
17
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!