LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2007, 02:18 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
Java function return value cannot be resolved


Hello,
This is my first program writing in Java after hello world. I know I have jumped very fast to write a relatively complex program, but I'm not patient enough to learn it step by step by writing simple. However, I m facing a problem when trying to write a function that returns a ResultSet value. When I place the the return statement at the end of function, it says that the variable cannot be resolved and when I place it right after the variable gets initialized, it warns me that the function should return a ResultSet value.

Here is the function:

PHP Code:
public static ResultSet query(String query)
    {
           
Statement stmt;
           
           try {
               Class.
forName("com.mysql.jdbc.Driver").newInstance();
           } catch (
Exception ex) {
               
System.out.println(ex);
           }  
           
           try {
               
String myUrl "jdbc:mysql://localhost/University?user=root&password=esteghlal";
               
Connection conn DriverManager.getConnection(myUrl);
               
stmt conn.createStatement();
               
               
ResultSet rs stmt.executeQuery(query); 
               
           
            
           } catch (
SQLException ex) {
               
System.out.println("SQLException: " ex.getMessage());
              
System.out.println("SQLState: " ex.getSQLState());
               
System.out.println("VendorError: " ex.getErrorCode());
           }
           return 
rs;
           
    } 
As you can see in this example it tells me that rs cannot be resolved. I think this problem is produced by try and catch, but I don't know how to get rid of it.

I will really appreciate your time reading this thread.

Thanks in advance
 
Old 10-25-2007, 02:39 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Try declaring your ResultSet variable outside your try/catch blocks (before you use it, obviously).
 
Old 10-25-2007, 02:47 AM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
when I declare it right after declaring stmt outside try/catch block, at the end it tells me that The local variable rs may not have been initialized, while it is initialized inside try/catch block.

I have tried different combination of declaring and returning rs in different positions with no success.
 
Old 10-25-2007, 07:32 AM   #4
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Why not like this:

PHP Code:
public static ResultSet query(String query)
    {
           
Statement stmt;
           
ResultSet rs;
           
           try {
               Class.
forName("com.mysql.jdbc.Driver").newInstance();
           } catch (
Exception ex) {
               
System.out.println(ex);
               
rs 0;
           }  
           
           try {
               
String myUrl "jdbc:mysql://localhost/University?user=root&password=esteghlal";
               
Connection conn DriverManager.getConnection(myUrl);
               
stmt conn.createStatement();
               
               
rs stmt.executeQuery(query); 
               
           
            
           } catch (
SQLException ex) {
               
System.out.println("SQLException: " ex.getMessage());
               
System.out.println("SQLState: " ex.getSQLState());
               
System.out.println("VendorError: " ex.getErrorCode());
               
rs 0;
           } 
           
finally 
               return 
rs;
           }
    } 
Did not check.
 
Old 10-25-2007, 10:47 AM   #5
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
rs cannot be set to zero and finally block does not complete normally.

This error is really strange.
 
Old 10-25-2007, 11:08 AM   #6
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
I got it working. I initialized the rs variable to null when declaring it.

I really appreciate your time.

Thanks
 
Old 10-25-2007, 12:11 PM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
You can set set rs value to null in catch statements too without declaring it null before them.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash function return rejeep Programming 15 08-24-2007 04:21 AM
How can I get the return value of a function in a shellscript? [KIA]aze Programming 8 07-05-2007 08:18 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 01:05 PM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration