File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL connection error " Watch "SQL connection error " 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("jdbcdbc:dsn","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 "
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your method will not return anything if an Exception is thown. The method signature means it must always return a Connection (or null).

(Also - it looks like you may be trying to use the JDBC-ODBC bridge to connect to an Oracle database. Why not use the thin driver?)
[ February 01, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Shaan patil
Ranch Hand

Joined: Dec 22, 2007
Posts: 58
Hi Paul,

First Let me thank you for the response. Yes The method signature demands that we should send a connection object.But If an exception occurs then It will be a false connection object if we return any connection object, right ?


Now what correction measure I should take in the code?

(One solution :Should I chech for the null in connection object and then only proceed with my code ?)


Regards
Shaan
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If an exception occurs the only thing you can return is null.

Its up to you how you want ot handle this. My personal preference is to try to ensure methods that have a return value never return null.
 
 
subject: SQL connection error