iterate result set to create object as required - java

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();

Related

Getting an Array from my ResultSet and adding it to an Object

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?

How to use BeanMapHandler with field to column mapping with key as ID in a nested SELECT query?

I have a nested SQL query to fetch employee details using their ID.
Right now I am using BeanListHandler to fetch data as a List<Details> but want to store it as a Map<String, Details> where the ID I originally pass needs to be the key for easy retrieval instead of searching the List with streams every time.
I have tried to convert to Maps but I am not sure of how to map the ID as String nor how to get the original ID passed to the inner Query as a column in the final result.
MainTest.java:
String candidateId = "('1111', '2222', '3333', '4444')";
String detailsQuery =
"select PARTNER, BIRTHDT, XSEXM, XSEXF from \"schema\".\"platform.view/table2\" where partner IN \r\n"
+ "(select SID from \"schema\".\"platform.view/table1\" where TYPE='BB' and CLASS='yy' and ID IN \r\n"
+ "(select SID from \"schema\".\"platform.view/table1\" where TYPE='AA' and CLASS='zz' and ID IN"
+ candidateId + "\r\n" + "))";
Map<String, Details> detailsView = queryRunner.query(conn, detailsQuery, new DetailsViewHandler());
Details.java:
public class Details {
private String candidateId;
private String birthDate;
private String maleSex;
private String femaleSex;
// getter and setter
}
DetailsViewHandler.java:
public class DetailsViewHandler extends BeanMapHandler<String, Details> {
public DetailsViewHandler() {
super(Details.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
}
public static Map<String, String> getColumnsToFieldsMap() {
Map<String, String> columnsToFieldsMap = new HashMap<>();
columnsToFieldsMap.put("PARTNER", "candidateId");
columnsToFieldsMap.put("BIRTHDT", "birthDate");
columnsToFieldsMap.put("XSEXM", "maleSex");
columnsToFieldsMap.put("XSEXF", "femaleSex");
return columnsToFieldsMap;
}
}
Is there a way to get the ID (candidateId) in the result and what am I missing in terms of creating the key-value pairing ?
From the doc https://commons.apache.org/proper/commons-dbutils/apidocs/org/apache/commons/dbutils/handlers/BeanMapHandler.html
of constructor which you are using
public BeanMapHandler(Class<V> type,
RowProcessor convert)
// Creates a new instance of BeanMapHandler. The value of the first column of each row will be a key in the Map.
Above should work.
You can also try overriding createKey like so
protected K createKey(ResultSet rs)
throws SQLException {
return rs.getString("PARTNER"); // or getInt whatever suits
}

Parse String into Java Objects

I need to convert Strings of this format into an Array of objects.
[{name=Nancy Chapman, email=nchapman0#comcast.net}, {name=Jimmy Fisher, email=jfisher1#photobucket.com}]
Is there any easy way to convert this without having to do it completely manually?
UPDATE:
I am pulling these values from a custom SQL database (Amazon Athena). And the custom JDBC does not support getArray() so it looks like I need to manually parse the columns that contain an Array of Structs. It is unfortunately a limitation of the DB and I have no control over it. This is the format the SQL database returns when I call getString() on the column.
SQL Table Definition
id (int)
threadid (int)
senderemail (string)
sendername (string)
subject (string)
body (string)
recipients (array<struct<name:string,email:string>>)
ccrecipients (array<struct<name:string,email:string>>)
bccrecipients (array<struct<name:string,email:string>>)
attachments (array<binary>)
date (timestamp)
Java Objects
MessageObj
public class MessageObj {
private int id;
private int threadId;
private String senderEmail;
private String senderName;
private String subject;
private String body;
private List<RecipientObj> recipients;
private List<RecipientObj> ccRecipients;
private List<RecipientObj> bccRecipients;
private List<File> attachments;
private Calendar date;
}
RecipientObj
public class RecipientObj {
private String email;
private String name;
}
Parsing the data.
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
// Retrieve table column.
int id = rs.getInt("id");
Integer threadId = rs.getInt("threadid");
String senderEmail = rs.getString("senderemail");
String senderName = rs.getString("sendername");
String subject = rs.getString("subject");
String body = rs.getString("body");
//How to convert recipients into ArrayList? rs.getArray("recipients") not supported.
//... Code here to add into an ArrayList of MessageObj.
}
Perhaps I'm misunderstanding your question but what you typed should work if you clean it up a bit so that it looks like this:
[{ name:"Nany Chapman", email:"nchapman0#comcast.net"},
{ name:"Nany Chapman", email:"nchapman0#comcast.net"}]
Rather than use the equal sign, use a colon and put quotation marks around your values.

How to map a non-persistent user defined class so that Hibernate will load bean with instance from SQLQuery

I am new to hibernate and am having difficulty trying to get it to work for anything other than a direct table mapping scenario. Basically, I have a user defined Java class called: BookInvoice. This class is a non-persistent (not a db table) and uses columns from previously defined and Annotated) db tables. Every time I try to label it as an #Entity it tells me I cant because it is not an existing db table. How do I map it so that I dont get the
Unknown entity: com.acentia.training.project15.model.BookInvoice
error message that I have been experiencing.
My sql queries are good and I am able to get the info from the db; however, they come back as class Object and I am not permitted to cast them into my desired BookInvoice class in order to send it back to the calling method. Below pls find snipets of my work thus far . . .
Please note all of my regular classes that conform to existing db tables queries work fine, it is just the ones that are non-persistent that I am having issues with.
PurchaseOrderInvoiceDAO:
List<BookInvoice> bInvoiceList = null;
final String bookInvoiceQuery =
"SELECT Books.ID, PO_Details.QUANTITY, Books.ISBN, Books.TITLE, Books.AUTHOR, Author.Name, Books.PUBLISHED, Books.COVER, Books.SERIES, Books.SERIES_NO,\n" +
" Books.SUBJECT_ID,Books.PRICE\n" +
" FROM Purchase_Order, PO_Details, Books, Author\n" +
" WHERE Purchase_Order.ID=?\n" +
" AND Purchase_Order.ID=PO_Details.PO_ID\n" +
" AND PO_Details.Book_ID=Books.ID\n" +
" AND Books.AUTHOR=Author.ID";
Query bookInvoicQ = getSession().createSQLQuery(bookInfoQuery).addEntity(BookInvoice.class);
bookInvoicQ.setInteger(0, id);
bList = (List<Books>) bookInvoicQ.list();
BookInvoice class:
public class BookInvoice {
Integer id = null;
Integer quantity = null;
String isbn = null;
String title = null;
Integer authorId = null;
Date publishedDate = null;
String cover = null;
String series = null;
Integer seriesNo = null;
Integer subjectId = null;
Double price = null;
public BookInvoice(final Integer id, final Integer quantity, final String isbn, final String title,
final Integer authorId, final Date publishedDate, final String cover,
final String series, final Integer seriesNo, final Integer subjectId, final Double price) {
this.id = id;
this.quantity = quantity;
this.isbn = isbn;
this.title = title;
this.authorId = authorId;
this.publishedDate = publishedDate;
this.cover = cover;
this.series = series;
this.seriesNo = seriesNo;
this.subjectId = subjectId;
this.price = price;
}
public BookInvoice(){}
public Integer getId() {
return id;
}
etc. . . .
Stack Trace Snippet:
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
Unknown entity: com.acentia.training.project15.model.BookInvoice
File: org/hibernate/impl/SessionFactoryImpl.java
Line number: 693
Stacktraces
org.hibernate.MappingException: Unknown entity:
com.acentia.training.project15.model.BookInvoice
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:335)
org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:376)
org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:355)
org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:171)
org.hibernate.loader.custom.sql.SQLCustomQuery.(SQLCustomQuery.java:87)
org.hibernate.engine.query.NativeSQLQueryPlan.(NativeSQLQueryPlan.java:67)
org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:166)
org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:160)
org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
com.acentia.training.project15.bo.PurchaseOrderInvoiceBO$PurchaseOrderInvoiceDAO.getById(PurchaseOrderInvoiceBO.java:196)
...
Ok! Finally broke down and talked to my supervisor about this. He explained that I am doing like waaaaaay to much extra work on this.
Basically, if I set the #Entity class/DB mappings up correctly then they will get all of the right information (ie. #OneToMany, etc.) from the mappings of the classes that correspond directly to the DB tables. Basically, Hibernate will go down as many levels (PO_Details ->Payments->Books, etc) they would give me all the additional information that I need and I wouldn't need to create my own custom classes.

Iterartor - How to traverse for specific records

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();
}

Categories

Resources