CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Aug 2011
    Posts
    15

    classInstanceInitalization

    I have this code:

    import java.sql.*;

    public class Products {
    private int ProductID;
    private String name;
    private String category;
    private Double price;
    public void setId(int ProductID) { this. ProductID = ProductID; }
    public int getId() { return this.ProductID; }
    public void setName(String name) { this.name = name; }
    public String getName() { return this.name; }
    public void setCategory(String category) { this. category = category; }
    public String getcategory () { return this.category; }
    public void setPrice(double price) { this.price = price; }
    public double getCijena() { return this.price; }
    public Products(int ProductID, String name, String category, Double price)
    {
    this.ProductID = ProductID;
    this.name = name;
    this.category = category;
    this.price = price;
    }


    public static void getFromId(int productID)
    {
    Connection conn = null;
    {
    try
    {
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
    System.out.println(conn.isClosed());
    }
    catch(Exception ex)
    {
    System.out.println("Connection not established");
    }
    try
    {
    Statement st = conn.createStatement();
    st.executeQuery("select * from products WHERE ID_product="+productID);
    ResultSet rs = st.getResultSet();
    while (rs.next())
    {
    int IdP = rs.getInt(1);
    String Pname = rs.getString(2);
    String Pcategory = rs.getString(3);
    Double Pprice = rs.getDouble(4);
    Products p=new Products(IdP, Pname, Pcategory, Pprice);
    }
    }
    catch (SQLException s){
    System.out.println("SQL statement not executed!");
    }

    finally {
    try
    {
    if(conn!=null && !conn.isClosed())
    conn.close();
    System.out.println(conn.isClosed());
    }
    catch(Exception ex) { }

    }
    }


    Now, what I need is to make this method(getFromId) return this newly created class instance, with its fields values.
    Any suggestons?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    method(getFromId) return this newly created class instance
    Change the void in the method defintion to the name of the class you want to return and add a return statement that returns the instance.
    Norm

  3. #3
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    Change the void in the method defintion to the name of the class you want to return and add a return statement that returns the instance.
    I tried that, but it's not working (error message saying missing return statement).

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Please post the full text of the error message.

    Check the code in the method to be sure ALL exit points from the method use a return statement.
    The compiler thinks there is a way to exit the method that does not use the return statement.
    Norm

  5. #5
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Here is complete error message text:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
    at sales.Product.getFromId(Product.java:158)
    at sales.Sales.main(Sales.java:18)
    Java Result: 1

    "Sales" is the name of main class, from which I call method(getFromID)

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    What is the code around line 158 in Product.java?

    Have you checked that there is NO way to exit the method without a return statement?
    Norm

  7. #7
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    What is the code around line 158 in Product.java?

    Have you checked that there is NO way to exit the method without a return statement?
    public Products(int ProductID, String name, String category, Double price)
    {
    this.ProductID = ProductID;
    this.name = name;
    this.category = category;
    this.price = price;
    }


    public static Product getFromId(int productID)
    {
    Connection conn = null;
    {
    try
    {
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
    System.out.println(conn.isClosed());
    }
    catch(Exception ex)
    {
    System.out.println("Connection not established");
    }

    I am not sure what you mean by "checking that there is NO way to exit the method without a return statement.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Where is line 158 in Product.java?

    Where is the return statement?
    Last edited by Norm; August 23rd, 2011 at 12:26 PM.
    Norm

  9. #9
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    Where is line 158 in Product.java?

    Where is the return statement?
    iimport java.sql.*;

    public class Products {
    private int ProductID;
    private String name;
    private String category;
    private Double price;
    public void setId(int ProductID) { this. ProductID = ProductID; }
    public int getId() { return this.ProductID; }
    public void setName(String name) { this.name = name; }
    public String getName() { return this.name; }
    public void setCategory(String category) { this. category = category; }
    public String getcategory () { return this.category; }
    public void setPrice(double price) { this.price = price; }
    public double getCijena() { return this.price; }

    public Products(int ProductID, String name, String category, Double price)
    {
    this.ProductID = ProductID;
    this.name = name;
    this.category = category;
    this.price = price;
    }


    public static Product getFromId(int productID)
    { //line 158//
    Connection conn = null;
    {
    try
    {
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
    System.out.println(conn.isClosed());
    }
    catch(Exception ex)
    {
    System.out.println("Connection not established");
    }
    try
    {
    Statement st = conn.createStatement();
    st.executeQuery("select * from products WHERE ID_product="+productID);
    ResultSet rs = st.getResultSet();
    while (rs.next())
    {
    int IdP = rs.getInt(1);
    String Pname = rs.getString(2);
    String Pcategory = rs.getString(3);
    Double Pprice = rs.getDouble(4);
    Product p=new Product(IdP, Pname, Pcategory, Pprice);
    return p; //return statement//
    }
    }
    catch (SQLException s){
    System.out.println("SQL statement not executed!");
    }

    finally {
    try
    {
    if(conn!=null && !conn.isClosed())
    conn.close();
    System.out.println(conn.isClosed());
    }
    catch(Exception ex) { }

    }
    }

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Where is the return statement at the end of the class?
    Norm

  11. #11
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    Where is the return statement at the end of the class?
    Do you mean like this:

    import java.sql.*;

    public class Products {
    private int ProductID;
    private String name;
    private String category;
    private Double price;
    public void setId(int ProductID) { this. ProductID = ProductID; }
    public int getId() { return this.ProductID; }
    public void setName(String name) { this.name = name; }
    public String getName() { return this.name; }
    public void setCategory(String category) { this. category = category; }
    public String getcategory () { return this.category; }
    public void setPrice(double price) { this.price = price; }
    public double getCijena() { return this.price; }

    public Products(int ProductID, String name, String category, Double price)
    {
    this.ProductID = ProductID;
    this.name = name;
    this.category = category;
    this.price = price;
    }


    public static Product getFromId(int productID)
    { //line 158//
    Connection conn = null;
    {
    try
    {
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
    System.out.println(conn.isClosed());
    }
    catch(Exception ex)
    {
    System.out.println("Connection not established");
    }
    try
    {
    Statement st = conn.createStatement();
    st.executeQuery("select * from products WHERE ID_product="+productID);
    ResultSet rs = st.getResultSet();
    while (rs.next())
    {
    int IdP = rs.getInt(1);
    String Pname = rs.getString(2);
    String Pcategory = rs.getString(3);
    Double Pprice = rs.getDouble(4);
    Product p=new Product(IdP, Pname, Pcategory, Pprice);
    return p; //return statement//
    }
    }
    catch (SQLException s){
    System.out.println("SQL statement not executed!");
    }

    finally {
    try
    {
    if(conn!=null && !conn.isClosed())
    conn.close();
    System.out.println(conn.isClosed());
    }
    catch(Exception ex) { }

    }
    return p;
    }


    I tried it, but it's still not working.

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    it's still not working.

    Please explain


    Cross posted at
    http://www.javaprogrammingforums.com...html#post40484
    Norm

  13. #13
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    it's still not working.

    Please explain


    Cross posted at
    http://www.javaprogrammingforums.com...html#post40484
    I am not sure what exactly to explain.

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    it's still not working.
    There are thousands of reasons for a program to NOT WORK.
    Which one is causing your program problems?
    Norm

  15. #15
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Now, compiler tells me "cannot find symbol(variable p).

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured