Joining Objects in MySQL - JAVA - java

I used JOIN to join tables together and retrieve information. After retrieving objects I am adding that information to a table.
my query:
"SELECT L.LibPassport AS LibPassport,L.Object as LObject,G.Passport AS GPassport,"
+ "G.Object AS GObject,O.* FROM Orders AS O, Guest AS G, Librarian AS L WHERE O.Passport = G.Passport";
Firstly it gives me an error as Columns LObject not found then I changed all to only as Object , query worked smoothy but then its not adding all objects to List.
Method for sql is below:
public List<Object> getReport() {
List<Object> obj = new ArrayList<Object>();
Object x = null;
String query = "SELECT L.LibPassport AS LibPassport,L.Object as Object,G.Passport AS GPassport,"
+ "G.Object AS Object,O.*,O.Object AS Object FROM Orders AS O, Guest AS G, Librarian AS L WHERE O.Passport = G.Passport";
try {
state = conn.prepareStatement(query);
rs = state.executeQuery();
//Librarian
while(rs.next()) {
ByteArrayInputStream bais = new ByteArrayInputStream(rs.getBytes("Object"));
ObjectInputStream ois = new ObjectInputStream(bais);
x = ois.readObject();
obj.add(x);
ois.close();
bais.close();
}
}catch(SQLException | IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
and then I am accessing and differentiating using instanceof e.g.
if(obj.get(i) instanceof Transaction) {
a = (Transaction) obj.get(i);
fill.add(4,a.getBorrowDate());
fill.add(5,a.getReturnDate());
}
But its not getting all the objects, it either doesn't access or only 1 object. however the same query in mysql shows me all objects.
If you want further details please let me know.

Related

oracleConnection.createARRAY not working with WebLogic Data Source Connection

So, I was using a datasource which was defined in Spring, which was working fine. Then I updated my project to take the datasource from the Weblogic server which the application is running on. This too, works fine for most calls to the database, except for one scenario - This scenario is involved sending a list of objects to the database, based on database types which are defined in Java by using Structs.
The full method is:
#Override
public List<String> saveAllocation(String originalId, List<Parcel> parcels) throws SQLException {
if(originalId == null || parcels == null) {
return null;
}
List<String> results = null;
String result = null;
String log = null;
OracleConnection oracleConnection = (OracleConnection)jdbcTemplate.getDataSource().getConnection();
try {
OracleCallableStatement cs = (OracleCallableStatement) oracleConnection.prepareCall("{ call PACKAGE.Update(?, ?, ?, ?) }");
Struct[] cpoList = new Struct[parcels.size()];
for(int i = 0; i < parcels.size(); i++) {
Object[] obj = new Object[] { parcels.get(i).getParcel_id(), parcels.get(i).getPublicID().toUpperCase() };
Struct struct = oracleConnection.createStruct("SCHEME_NAME.PARCEL_OBJ", obj);
cpoList[i] = struct;
}
Array array = oracleConnection.createARRAY("SCHEME_NAME.PARCEL_TAB", cpoList);
cs.setString(1, originalId);
cs.setArray(2, array);
cs.registerOutParameter(3, Types.VARCHAR);
cs.registerOutParameter(4, Types.VARCHAR);
cs.executeUpdate();
log = cs.getObject(3).toString();
result = cs.getObject(4).toString();
results = new ArrayList<>();
results.add(result);
results.add(log);
} catch(SQLException e) {
//Log exception
return results;
} catch(Exception e) {
//Log exception
return results;
} finally {
if (cs != null) {
cs.close();
}
}
return results;
}
}
The database objects are defined as:
PARCEL_OBJ
create or replace TYPE parcel_obj AS OBJECT
(PARCEL_ID VARCHAR2(11),
PUBLIC_ID VARCHAR2(20));
PARCEL_TAB
create or replace TYPE parcel_tab IS TABLE OF parcel_obj;
The application fails on the line
Array array = oracleConnection.createARRAY("SCHEME_NAME.PARCEL_TAB", cpoList);
The exception message is:
java.sql.SQLException: Fail to convert to internal representation: weblogic.jdbc.wrapper.Struct_oracle_sql_STRUCT#187>
My JNDI connection is defined in my application.properties like:
spring.datasource.jndi-name=jdbc/pio
Any help would be appreciated!
As the documentation mentions.
By default, data type objects for Array, Blob, Clob, NClob, Ref,
SQLXML, and Struct, plus ParameterMetaData and ResultSetMetaData
objects are wrapped with a WebLogic wrapper.
In some cases setting the wrapping parameter to false can improve significantly the performance and allows the application to use a native driver.
I don't see a problem disabling that option since it is causing the problem when calling objects like struct in the first place.

Java: return List of objects of the type that calls the method

what i am trying to do is return a java List of objects of any type that calls the method.
List<Playlist> playlist = Table.selectAll();
Those java objects (in this case Playlist) should each contain a row of values from my SQL ResultSet.
public class Table extends Database {
public List<this> selectAll() {
List<this> newList = new List<this>();
String tableName = this.getClass().getSimpleName().toLowerCase();
Field[] fields = this.getClass().getDeclaredFields();
Connection connection = getConnection();
PreparedStatement preStat = null;
ResultSet resultSet = null;
try {
preStat = connection.prepareStatement("SELECT * FROM " + tableName);
resultSet = getFromDB(preStat);
} catch (SQLException e) {
e.printStackTrace();
}
// create a list of Objects of class ‘this’ (in this case Playlist)
// and using the fields obtained from .getDeclaredFields()
// assign values to each Object from the ‘preStat’ results
// then put those objects into ‘newList’
return newList;
}
}
What am I doing wrong here? should I pass in a List to the method instead of getting a return back, but then how would I create an Object from each ResultSet, assign it values using .getDeclaredFields() then assign the object to the List?
List<this> newList = new List<this>();
is not going to work, it must be
List<PlayList> newList = new List<>();
Then in your comments do
while (resultSet.next()) {
PlayList l = new PlayList();
l.setId(resultSet.getInt(1));
l.setYourStringField(resultSet.getString(2));
l.setAnotherStringField(resultSet.getString(3));
newList.add(l);
}
resultSet.close();
preStat.close();
connection.close();
Being setId, setYourStringField and setAnotherStringField property setters of your PlayList object.

How to write sql innerquery in hibernate?

presently my method uses basic jdbc concept like this,
public static ArrayList<VehicleDetailsBean>
getAllVehicleDetails(String groupId,String clientId)
throws ClassNotFoundException, SQLException {
ArrayList<VehicleDetailsBean> vehicleDetailsList =
new ArrayList<VehicleDetailsBean>();
try {
Statement statement = connection.createStatement();
String sql ="SELECT a.vehicleno,a.lat,a.lng,a.status, "+
"a.rdate,a.rtime from latlng a,vehicle_details b where"+
"a.vehicleno=b.vehicleno and b.clientid='"+
clientId+"' and b.groupid in(select groupid from group_details"+
" where groupname='"+groupId+"' and clientid='"+clientId+"')";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
VehicleDetailsBean vehicleDetailsBean=new VehicleDetailsBean();
vehicleDetailsBean.setVehicleno(rs.getString("vehicleno"));
vehicleDetailsBean.setLat(rs.getString("lat"));
vehicleDetailsBean.setLng(rs.getString("lng"));
vehicleDetailsBean.setStatus(rs.getString("status"));
vehicleDetailsBean.setRdate(rs.getInt("rdate"));
vehicleDetailsBean.setRtime(rs.getString("rtime"));
vehicleDetailsList.add(vehicleDetailsBean);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
it returns an ArrayList. Now I want to change it to hibernate SO I will change above code as,
query = session.createQuery(hqlquery);//I am not getting how to write hqlquery
List<Object[]> groupList = query.list();
for(Object[] arr : groupList){
System.out.println(Arrays.toString(arr));
}
by doing this it returns List object but my method has to return ArrayList. So can any one help me to write query and return the result as ArrayList.
You can do it the ugly way:
query = session.createQuery(hqlquery); // Take a walk in Hibernate Docs for HQL Queries
List<?> groupList = query.list();
ArrayList<VehicleDetailsBean> result = new ArrayList<VehicleDetailsBean>(groupList.size());
for (Object o : groupList) {
result.add((VehicleDetailsBean) o);
}
return result;
If you want to return it as ArrayList, you can just addAll it with your ArrayList.
List<VehicleDetailsBean> groupList = (List<VehicleDetailsBean>) query.list();
vehicleDetailsList.addAll(groupList);
return vehicleDetailsList;
You can see similar question here and a way to cast here
EDIT
Or try to add entity
List<LatitudeBean> bean = (List<LatitudeBean>) session.createSQLQuery(query)
.addEntity(LatitudeBean.class).list();
//add to ArrayList here

Creating a list with database values and store in DTO

I have a my main class, a DTO, and a DAO.
what I want to do is to read the database table CUSTOMER(two fields NAME, SURNAME) then write it to a txt file. I cant seem to get it to store the values in the DTO so that my main class can get it.
My DTO exist of two fields, NAME and SURNAME with the getters and setters.
The problem lies in my DAO with the List
*please keep in mind I'm a student.
This is what I have done so far:
result, writes to file like this: [name surname, name surname, name surname,]
I need it to write to the file pipe deliminated " | "
public CustomerDTO getDetails(Connection conn) throws SQLException {
CustomerDTO customerDTO = new CustomerDTO();
ResultSet rs = null;
PreparedStatement pstmnt = null;
try {
// SELECT NAME, SURNAME FROM CUSTOMER
StringBuffer stringBuffer = new StringBuffer("SELECT " + DBConstants.CUST_NAME + ", ");
stringBuffer.append(DBConstants.CUST_SURNAME);
stringBuffer.append(" FROM " + "CUSTOMER");
//build string
String query = stringBuffer.toString();
//prepared statement
pstmnt = conn.prepareStatement(query);
//execute preparestatement
rs = pstmnt.executeQuery();
//keep reading next line in table - .next() method means next line
List myList = new ArrayList();
while (rs.next()) {
String ss = rs.getString("NAME");
String sss = rs.getString("SURNAME");
myList.add(ss + " " + sss);
customerDTO.setName(myList.toString());
customerDTO.setSurname(myList.toString());
}
} catch (Exception e) {
e.getMessage();
}
rs.close();
//return DTO with details.
return customerDTO;
}
I am not sure where or how you are writing this file is but in tracing the code, here is what I think might be wrong.
In this WHILE LOOP:
while (rs.next()) {
String ss = rs.getString("NAME");
String sss = rs.getString("SURNAME");
// You'r adding an element here that would have a value like this: "John Doe"
myList.add(ss + " " + sss);
// Single instance of a DTO whose value. State values will always be replaced with the
// string represenation of the myList variable.
//
// Both properties will always contain the same value no matter what.
customerDTO.setName(myList.toString());
customerDTO.setSurname(myList.toString());
}
I think what you should really be doing is something like this:
// Notice the use of Generics (Look it up). It is more compile time safe and better practice.
ArrayList<CustomerDTO> customerDTOS = new ArrayList<CustomerDTO>();
while (rs.next()) {
String ss = rs.getString("NAME");
String sss = rs.getString("SURNAME");
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setName(ss);
customerDTO.setSurName(sss);
customerDTOS.Add(customerDTO);
}
return customerDTOS;
In short, your method should return a list of CustomerDTOs and then you can use them to write to your file.
Good Luck.
The method should be declared as
public List<CustomerDTO> getDetails(Connection conn) throws SQLException {
...
}
since its goal is to return a list of customers (one per row in the table).
Inside the method, you should have the following:
// create the result list, initially empty
List<CustomerDTO> result = new ArrayList<CustomerDTO>();
// iterate through the rows
while (rs.next()) {
// TODO: create a new CustomerDTO, and populate it with the row's data
// TODO: add this DTO to the result list
}
return result;
Then, the caller of this method will iterate through the list of CustomerDTOs, and write them to a file. Each method has its responsibility: the DAO handles the database retrieval, but doesn't handle the file IO.
here's a complete example:
public void caller() {
Connection conn = null;
// TODO: setup connection
List list = getDetails(conn);
for (int i=0; i<list.size(); i++) {
CustomerDTO dto = list.get(i);
write(dto.getName(), dto.getSurname());
}
}
public List getDetails(Connection conn) throws SQLException {
List myList = new ArrayList();
ResultSet rs = null;
PreparedStatement pstmnt = null;
try {
// SELECT NAME, SURNAME FROM CUSTOMER
StringBuffer stringBuffer = new StringBuffer("SELECT " + DBConstants.CUST_NAME + ", ");
stringBuffer.append(DBConstants.CUST_SURNAME);
stringBuffer.append(" FROM " + "CUSTOMER");
//build string
String query = stringBuffer.toString();
//prepared statement
pstmnt = conn.prepareStatement(query);
//execute preparestatement
rs = pstmnt.executeQuery();
//keep reading next line in table - .next() method means next line
while (rs.next()) {
String ss = rs.getString("NAME");
String sss = rs.getString("SURNAME");
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setName(ss);
customerDTO.setSurname(sss);
myList.add(customerDTO);
}
} catch (Exception e) {
e.getMessage();
}
rs.close();
//return list of DTOs.
return myList;
}
I leave the implementation of the write method to you.
Also. consider using Generics if your java allows it.

Hibernate - Traversing createSQLQuery results and reading into appropriate object

I am pretty new to Hibernate / Java (JSF 2.0) and I am attempting to call a custom query and read the results into an object that I have created Logins. Logins has two setter functions, setLoginDate(Date date) and setUserId(Integer userId) my function looks like so, The issue I am having is how to transform the result set and read in the appropriate values into a temp loginList
public List<Logins> getUserLogins() {
Session session = getSessionFactory().openSession();
List<Logins> loginList = null;
Login temp = null;
try {
String SQL_QUERY = "SELECT login_date, user_id FROM user_logins";
Query query = session.createSQLQuery(SQL_QUERY);
List results = query.list();
for(ListIterator iter = results.listIterator(); iter.hasNext(); ) {
** THIS IS THE PART I AM NOT CLEAR ON ***
temp.setLoginDate(resutls.get(0));
temp.setUserId(results.get(1));
loginList.add(temp);
temp = null;
*****************************************
return loginList;
}
} catch(HibernateException e) {
throw new PersistanceDaoException(e);
} finally {
session.close();
}
}
missing part:
Object[] row = (Object[]) iter.next();
temp = new Login();
temp.setLoginDate(row[0]);
temp.setUserId(row[1]);
you may need to cast row[i] to your target object for example if login date is a date object: temp.setLoginDate((Date) row[0]);
for a better solution, try to use ResultTransformer
you can find more about ResultTransformer in Hibernate Docs.

Categories