How to return class instance?
I have the next code:
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?
Re: How to return class instance?
I'm assuming you want to return your Products object, so all you need to do is change the method declaration from void, to a return type of Products.
then in your method, use return p;
E.g
To write a method which returns a String, you would do:
Code java:
public String getString(){
String x = "Hello World";
return x;
}
Re: How to return class instance?
Quote:
Originally Posted by
newbie
I'm assuming you want to return your
Products object, so all you need to do is change the method declaration from
void, to a return type of
Products.
then in your method, use
return p;
E.g
To write a method which returns a String, you would do:
Code java:
public String getString(){
String x = "Hello World";
return x;
}
I tried that, but it's not working.
I get error message saying:
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)
Re: How to return class instance?
Well the answer is in the error message, " missing return statement".
Take the String example, if you declare a method such as it returns a String, you really do need to return a String.
If you're still having trouble, be sure to post the code snippet to which your error message is referring to.
Re: How to return class instance?
Quote:
Originally Posted by
newbie
Well the answer is in the error message, " missing return statement".
Take the String example, if you declare a method such as it returns a String, you really do need to return a String.
If you're still having trouble, be sure to post the code snippet to which your error message is referring to.
Here is the code snippet (to which error message is referring to):
class Product
Code :
public static Product getFromId(int productID)
{//this is the line 158//
main class(Sales)
Re: How to return class instance?
Your class is Products not Product
Re: How to return class instance?
Quote:
Originally Posted by
newbie
Your class is Products not Product
It was, but I changed it to Product, because it should contain data for one product.
Re: How to return class instance?