I'm pretty new to the whole SQL and Java thing, but here I'm actually really confused.
I have a method which is called by invoking a few array parameters, as following
Method(String[], int, int[], int[])
I'm doing a ResultSet to check to see if there are any rows available where there is an ID match for the searched item.
ResultSet group = statement.executeQuery("SELECT * FROM `circulation` WHERE `ItemID` = "+itemID);
However, I'm completely stumped here on how to construct the method.
The String[], and int[] parameters are what is stored in the database, while the standard int parameter is the itemID. I have no idea how to go about doing this to call the Method correctly.
Darn, I thought I was doing good, too.
Arguments are normally used to provide information to the method being called. To get information from the method being called, you normally use a return value.
Your method seems to search rows in a database table named circulation
for a given item ID. The item ID should thus be an argument. The found rows should be a returned value. The signature of the method should thus be:
Circulation[] findByItemId(int itemId);
Or better, using collections instead of arrays:
List<Circulation> findByItemId(int itemId);
What is Circulation
? It's a class that represents what a row of the circulation table contains. So if the table contains an ID, a name and an item ID, this class would look like
public class Circulation {
private int id;
private String name;
private int itemId;
// constructor, getters, and other useful methods omitted for brevity
}
Your findByItemId()
method will thus create an empty list of Circulation, then iterate over the rows of the result set, construct a Circulation
instance for each row, and add the instance to the list. And then it will return that list.
Finally, you're using concatenation to build a SQL query taking parameters. This is dangerous. You should use a prepared statement instead. Read the tutorial about them.
Take a look at Apache commons-dbutils. It is a simple wrapper around JDBC and lets you convert your resultsets into any standard datastructures
You can use function
rs.getArray()
with columnName or ColumnLabel. But this function return objects array and you can cast it to Integer and String.
I'm pretty new to the whole SQL and Java thing, but here I'm actually really confused.
I have a method which is called by invoking a few array parameters, as following
Method(String[], int, int[], int[])
I'm doing a ResultSet to check to see if there are any rows available where there is an ID match for the searched item.
ResultSet group = statement.executeQuery("SELECT * FROM `circulation` WHERE `ItemID` = "+itemID);
However, I'm completely stumped here on how to construct the method.
The String[], and int[] parameters are what is stored in the database, while the standard int parameter is the itemID. I have no idea how to go about doing this to call the Method correctly.
Darn, I thought I was doing good, too.
Arguments are normally used to provide information to the method being called. To get information from the method being called, you normally use a return value.
Your method seems to search rows in a database table named circulation
for a given item ID. The item ID should thus be an argument. The found rows should be a returned value. The signature of the method should thus be:
Circulation[] findByItemId(int itemId);
Or better, using collections instead of arrays:
List<Circulation> findByItemId(int itemId);
What is Circulation
? It's a class that represents what a row of the circulation table contains. So if the table contains an ID, a name and an item ID, this class would look like
public class Circulation {
private int id;
private String name;
private int itemId;
// constructor, getters, and other useful methods omitted for brevity
}
Your findByItemId()
method will thus create an empty list of Circulation, then iterate over the rows of the result set, construct a Circulation
instance for each row, and add the instance to the list. And then it will return that list.
Finally, you're using concatenation to build a SQL query taking parameters. This is dangerous. You should use a prepared statement instead. Read the tutorial about them.
Take a look at Apache commons-dbutils. It is a simple wrapper around JDBC and lets you convert your resultsets into any standard datastructures
You can use function
rs.getArray()
with columnName or ColumnLabel. But this function return objects array and you can cast it to Integer and String.
0 commentaires:
Enregistrer un commentaire