Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL connection error

 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • 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("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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shaan patil
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic