I have the next 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?