Results 1 to 4 of 4
  1. #1
    tascoa is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default can't return a value from a method / jdbc

    Hello I ahve a problem with returning a String value from "JdbcBean" to Main Class.

    JdbcBean:
    public class JdbcBean {

    Connection conn;

    public String getColumn(int sloupecek) {

    String sloupec1 = new String();

    try {
    Class.forName("oracle.jdbc.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@xxx ", "xxx", "xxx");
    Statement s = conn.createStatement();
    ResultSet r = s.executeQuery("select * from student");

    while (r.next()) {
    sloupec1 = r.getString(sloupecek);
    return sloupec1;
    }
    r.close();
    s.close();
    conn.close();
    }
    catch (ClassNotFoundException cnfe) {
    cnfe.printStackTrace();
    }
    catch (SQLException sqle) {
    sqle.printStackTrace();
    }
    }
    }

    MainClass:
    public class MainPokus {

    public static void main(String[] args) {

    JdbcBean spojeni = new JdbcBean();

    System.out.println(spojeni.getColumn(2));
    }
    }

    Exception:
    Error(23,24): can't return a value from method whose result type is void

    Have anyone any advice for me?

    I am a begginer in Jdbc database get data. :(

    Thanks a lot.
    Last edited by tascoa; 10-14-2009 at 10:40 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You need to learn the Java basics before trying to connect to databases.
    Your method is void but you have return statements inside it.

  3. #3
    tascoa is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    I am sorry, the void is wrong, It my mistake. But if I erase void word, the error is the same...

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

Similar Threads

  1. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 09:00 PM
  2. method that return 2 arguments
    By itaipee in forum New To Java
    Replies: 19
    Last Post: 01-12-2009, 06:36 PM
  3. return a null method
    By valoyivd in forum New To Java
    Replies: 2
    Last Post: 04-22-2008, 12:19 AM
  4. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 05:45 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •