-
August 23rd, 2011, 06:35 AM
#1
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?
-
August 23rd, 2011, 08:00 AM
#2
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
-
August 23rd, 2011, 11:15 AM
#3
Re: classInstanceInitalization
Originally Posted by Norm
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).
-
August 23rd, 2011, 11:22 AM
#4
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
-
August 23rd, 2011, 11:31 AM
#5
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)
-
August 23rd, 2011, 12:08 PM
#6
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
-
August 23rd, 2011, 12:23 PM
#7
Re: classInstanceInitalization
Originally Posted by Norm
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.
-
August 23rd, 2011, 12:24 PM
#8
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
-
August 23rd, 2011, 12:37 PM
#9
Re: classInstanceInitalization
Originally Posted by Norm
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) { }
}
}
-
August 23rd, 2011, 12:38 PM
#10
Re: classInstanceInitalization
Where is the return statement at the end of the class?
Norm
-
August 23rd, 2011, 01:15 PM
#11
Re: classInstanceInitalization
Originally Posted by Norm
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.
-
August 23rd, 2011, 01:15 PM
#12
Re: classInstanceInitalization
it's still not working.
Please explain
Cross posted at
http://www.javaprogrammingforums.com...html#post40484
Norm
-
August 23rd, 2011, 01:21 PM
#13
Re: classInstanceInitalization
Originally Posted by Norm
I am not sure what exactly to explain.
-
August 23rd, 2011, 01:22 PM
#14
Re: classInstanceInitalization
There are thousands of reasons for a program to NOT WORK.
Which one is causing your program problems?
Norm
-
August 23rd, 2011, 02:39 PM
#15
Re: classInstanceInitalization
Now, compiler tells me "cannot find symbol(variable p).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|