I'm trying to use ResultSet.getArray() to grab an int[] array from my database. I'm using postgreSQL, DBeaver, and Java.
ResultSet rs = s.executeQuery(sql);
ArrayList<Theater> theaterList = new ArrayList<>();
while(rs.next()) {
Theater t = new Theater(
rs.getInt("theater_id"),
rs.getArray("theater_numbers"),
rs.getString("theater_loc")
);
theaterList.add(t);
return theaterList;
}
Error message: "The constructor Theater(int, Array, String) is undefined"
I keep getting flagged for not having a constructor of the appropriate type:
import java.sql.Array;
public Theater(int theater_id, Array theater_numbers, String theater_loc) {
super();
this.theater_id = theater_id;
this.theater_numbers = theater_numbers;
this.theater_loc = theater_loc;
Here's the beginning of the class:
public class Theater {
private int theater_id;
private Array theater_numbers;
private String theater_loc;
Not sure how to make the types agree with one another.
How do I make the types agree?
Related
ImageItem imageItems[] = new ImageItem[data.length()];
for (int i=0; i<data.length(); i++) {
JSONObject object = data.getJSONObject(i);
Log.e("RESPONSE INFO::::", "id:" + object.get("id").toString());
imageItems[i].imageId = object.get("id").toString(); //NullPointerException
imageItems[i].imageURI = object.get("source").toString();
imageItems[i].thumbURI = object.get("picture").toString();
imageItems[i].createdTime = object.get("created_time").toString();
imageItems[i].link = object.get("link").toString();
}
Above is some kind of banal problem that can't resolve. Im still getting the NullPointerException at line with comment. At first I thought somethin's wrong with JSONobjects, but I'm sure that object.get("id").toString(); returns the right String. Something must be wrong with imageItems[] array.
ImageItem is a simple class with few String fields:
public class ImageItem {
public String imageId = null;
public String imageURI = null;
public String thumbURI = null;
public String createdTime = null;
public String link = null;
}
Any ideas what i'm missing here?
EDIT: I should mention that the ImageItem class is inner class of another class AlbumGallery. Now I'm getting error: No enclosing instance of type AlbumGallery is accessible. Must qualify the allocation with an enclosing instance of type AlbumGallery (e.g. x.new A() where x is an instance of AlbumGallery). with imageItems[i] = new ImageItem()
at the top of your for loop add imageItems[i] = gallery.new ImageItem()
I'm completely newbie to Stored Procedure.
Got a requirement like the Input type to be passed is of XML datatype to DB2 stored procedure and output as ResultSet.
//Stored Procedure:
CREATE PROCEDURE GetGrpInfo (IN inpdoc XML)
DYNAMIC RESULT SETS 1
P1: BEGIN
Declare tin VARCHAR(9);
Declare max_records INT;
Declare start_index INT;
DECLARE rcount INT;
DECLARE total_count INT;
..........
..........
//Java Code:
private final static String myXML = "inpdoc";
private final static String SpOutput = "RESULT";
#Autowired
public void setDataSource(DataSource db2DataSource) {
String procedureName = "schemaname.GetGrpInfo";
this.db2DataSource = db2DataSource;
this.jdbcCall = new SimpleJdbcCall(this.db2DataSource)
.withProcedureName(procedureName)
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames(myXML)
.declareParameters(new SqlParameter(myXML, Types.SQLXML))
.returningResultSet("GroupInfo", new BeanPropertyRowMapper<GroupInfo>(){
public GroupInfo mapRow(ResultSet rs, int rowNum)
throws SQLException {
GroupInfo gInfo = new GroupInfo();
gInfo.setGroupId(rs.getString("GroupID"));
gInfo.setGroupName(rs.getString("GroupName"));
gInfo.setGroupTaxId(rs.getString("GroupTIN"));
return gInfo;
}
});
}
String input1 = "<Input><TIN>"+tin+"</TIN><MaxRecords>"+max_records+" </MaxRecords><StartIndex>"+start_index+"</StartIndex></Input>";
SqlParameterSource in = new MapSqlParameterSource().addValue(myXML, input1);
Map result = jdbcCall.execute(in);
return (String)result.get("GroupInfo");
Not sure whether use of returningResultSet is good at here and also Types.SQLXML. Tried with various formats but not sure whether my approach is correct.
Hello guys.I have following result set and now i want it to be saved in a java bean.I have two java beans(pojo).
public class Topic{
private int topic_id;
private String topic;
private List<SubTopic> subTopicList;
//getter setter
}
and
public class SubTopic{
private int sub_topic_id;
private String sub_topic;
//getter and setter
}
Now i want to set my Topic object in such a way that it contains one topic and list of all its subtopic.But i am having problem on iterating the result set.To make it more clear a Topic object that includes Cardiology should have,
topic_id=73
topic=Cardiology
List<SubTopic> subTopic=//this includes id and name of SubTopic and SubTopic2
Same another object should be for Allergy,Athma,Immunology.
ResultSet rs = pstmt.executeQuery();
//now how to iterate rs to create list of topic object in required way
Use a map to store your topics:
Map<Long, Topic> topicsById = new HashMap<>();
while (rs.next()) {
Long topicId = rs.getLong(1);
String topicName = rs.getString(2);
Long subTopicId = rs.getLong(3);
String subTopicName = rs.getString(4);
Topic topic = topicsById.get(topicId);
if (topic == null) {
topic = new Topic(topicId, topicName);
topicsById.put(topicId, topic);
}
topic.addSubTopic(new SubTopic(subTopicId, subTopicName);
}
Collection<Topic> allTopics = topicsById.values();
I have a custom an arraylist setup to hold data for me.
However when I call the arraylist items all the items are the same.
My arraylist loader.
public static ArrayList<Animal> getTblHerd() throws Exception {
CC_H2 db = new CC_H2();
db.Connect(Variables.getStrConn(), Variables.getStrUser(),
Variables.getStrPassword(), "Embedded");
ResultSet rs = db.query("Select HERD_ID FROM tblHerd ORDER BY HERD_ID ASC");
ArrayList<Animal> alAnimals = new ArrayList<Animal>();
while (rs.next()) {
int i = rs.getInt("HERD_ID");
alAnimals.add(new Animal(i));
}
db.Disconnect();
return alAnimals;
}
Animal Constructor
public Animal(int intAnimal_ID) throws Exception{
CC_H2 db = new CC_H2();
db.Connect(Variables.getStrConn(), "admin", "", "Embedded");
ResultSet rs = db.query("Select * FROM tblHerd WHERE HERD_ID = "
+ intAnimal_ID);
while (rs.next()) {
setIntHerd_id(rs.getInt("Herd_ID"));
setStrHerd_Tag_Letter(rs.getString("Herd_Tag_Letter"));
setIntHerd_Tag_Num(rs.getInt("Herd_Tag_Num"));
setStrHerd_Tag_Color(rs.getString("Herd_Tag_Color"));
setStrHerd_Sex(rs.getString("Herd_Sex"));
setStrHerd_Type(rs.getString("Herd_Type"));
setDtHerd_Birthdate(rs.getDate("Herd_Birthdate"));
setIntHerd_Sire(rs.getInt("Herd_Sire"));
setIntHerd_Dam(rs.getInt("Herd_Dam"));
setIntHerd_Owner(rs.getInt("Herd_Owner"));
setDtHerd_TimeStamp(rs.getDate("Herd_TimeStamp"));
setStrHerd_Status(rs.getString("Herd_Status"));
setDtHerd_Status_Date(rs.getDate("Herd_Status_Date"));
}
db.Disconnect();
}
I think the code you listed should work as expected, the error is probably somewhere else.
Check the data in your database
Check the setters in the while loop do the right thing
Check that the fields in the Animal class are not static
If the problem persists, try to put something like System.out.println(rs.getInt("Herd_ID") + ": " + rs.getString("Herd_Tag_Letter")); into the where loop and check the output
Hope some of this helps :-)
you have created array list static
just remove static from there
I have executed a query using JDBC and traversing the resultset I have stored all fields in List in java.
List<String> dataList=new ArrayList<String>();
while(res.next())
{
dataList.add(res.getString(1));
dataList.add(res.getString(2));
dataList.add(res.getString(3));
dataList.add(res.getString(4));
dataList.add(res.getString(5));
dataList.add(res.getString(6));
dataList.add(res.getString(7));
}
Iterator<String> it= dataList.iterator();
As I have added directly into list so how can I get this 7 fields while traversing the iterator.
Means:
while(it.hasNext())
{
String f1=it.next();
}
Like wise everytime I want 7 fields at a time
and next 7, next 7....... so on
Using this while loop how can I get those 7 fields (one row in table having 7 field) at a time.
I get little bit confuse here. Please help me.
Thanks
What you want to do is actually create another object that stores all seven of the values.
Then create a list of these entries so that you can access one row at a time, which is what I think you are asking.
First create a class for the row.
private static class Entry {
String[] row;
public Entry ( ResultSet r ) {
row = new String [ 7 ];
for (int i = 1; i <= 7; i++) {
row[i] = r.getString(i);
}
}
}
Using that, you can then create a list of Entry objects.
List<Entry> entryList = new ArrayList <Entry> ();
while(res.next())
{
entryList.add ( new Entry ( res ) );
}
Then, you can go ahead and loop through entryList and get any specific entry you would want.
Of course, if you have specific values, it might be wise to create instance variables of type String for Entry rather than an array of Strings.
By that I mean you could do this:
private static class Entry {
String column1; // rather than name column1 use what the column semantically represents
String column2;
// ...
public Entry ( ResultSet r ) {
column1 = r.getString(1);
// ...
}
This way, you can also calls like r.getInt(i) for certain columns which have an different type other than String.
Good luck!
I think your List declaration should be
List<Any DAO Object> instead of List<String>
While fetching from resultset, create a DAO object, add all fetched data into that object and then add that object into the list.
Then you can iterate and get each DAO object at each iteration.
You can use DatabaseMetaData class,
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/testdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) throws Exception {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
DatabaseMetaData metadata = connection.getMetaData();
ResultSet resultSet = metadata.getColumns(null, null, "users", null);
while (resultSet.next()) {
String name = resultSet.getString("COLUMN_NAME");
String type = resultSet.getString("TYPE_NAME");
int size = resultSet.getInt("COLUMN_SIZE");
System.out.println("Column name: [" + name + "]; type: [" + type + "]; size: [" + size + "]");
}
connection.close();
}