Results 1 to 2 of 2
  1. #1
    nera is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default MissingReturnStatement

    I have this code:

    Java 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 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"); 
    }
     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;
        }
    }
       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) { }
    
     }
    }
    When I try to execute method getFromId (from main class), I get error message:
    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

    Line 158 is(Product class)
    Java Code:
    public static Product getFromId(int productID)
    {    //line158//
    Line 18 is (Sales class)
    Java Code:
    public class Prodaja {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Proizvod.getfromId(1001)                 //line18//
    Any suggestions?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

Posting Permissions

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