I am fetching values from database and I have a webservice which returns a list of values fetched from database which I am sending it to front-end view layer on request from angular like a webservice call
The problem is I am getting incorrect json values. It getting keys instead of values. When I see it in chrome console the json is like:
{"jobName":"NASA Scientist",
"jobPrimarySkill":null,
"jobRole":"JOB_ROLE",
"jobDesignation":"JOB_EXP", // keyname instead of value (Incorrect values)
"jobDescription":"JOB_DESCRIPTION", // keyname instead of value (Incorrect values)
"jobSalaryRange":"JOB_POSITIONS", // keyname instead of value (Incorrect values)
"jobExp":"JOB_SAL_RANGE", // keyname instead of value (Incorrect values)
"jobPositions":"JOB_POSTEDBY", // keyname instead of value (Incorrect values)
"jobPostedBy":null}
I have my back end code like this:
#Path("/FetchJobSummary")
public class FetchJobSummaryService {
FetchJobSummaryDAO dao = new FetchJobSummaryDAO();
#GET
#Produces(MediaType.APPLICATION_JSON)
public List fetch() {
System.out.println(dao.getJobSummaries());
// prints object [com.RTH.WebServices.JobSummaries#6d90d6c5,
com.RTH.WebServices.JobSummaries#6d90d6c5,
com.RTH.WebServices.JobSummaries#6d90d6c5,....
return dao.getJobSummaries();
}
}
DAO class:
public class FetchJobSummaryDAO {
public List getJobSummaries() {
JobSummaries jobSummaries = new JobSummaries();
List<JobSummaries> jobSummaryList = new ArrayList<JobSummaries>();
try {
Connection con = DBConnection.getConnection();
String query = "select JOB_NAME,JOB_DESCRIPTION,JOB_ROLE,JOB_PRIMARY_SKILL,JOB_DESIGNATION,JOB_EXP,JOB_SAL_RANGE, JOB_POSTEDBY from TBL_JOBPOSTING";
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription("JOB_DESCRIPTION");
jobSummaries.setJobRole("JOB_ROLE");
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation("JOB_DESIGNATION");
jobSummaries.setJobExp("JOB_EXP");
jobSummaries.setJobSalaryRange("JOB_SAL_RANGE");
jobSummaries.setJobPostedBy("JOB_POSTEDBY");
jobSummaryList.add(jobSummaries);
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) {
System.out.print(", ");
}
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " " + columnValue); //Prints correct values
}
}
} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;
}
I am sure its the problem somewhere in java than in frontend but not to figure out what is wrong
you have
jobSummaries.setJobRole("JOB_ROLE");
instead of
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
so you should have
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);
As you are taking "JOB_PRIMARY_SKILL" from result set using rs.getString, try
rs.getString("JOB_DESIGNATION"), maybe you are missing out that.
Your error is from here :`
while (rs.next()) {
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription("JOB_DESCRIPTION");
jobSummaries.setJobRole("JOB_ROLE");
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation("JOB_DESIGNATION");
jobSummaries.setJobExp("JOB_EXP");
jobSummaries.setJobSalaryRange("JOB_SAL_RANGE");
jobSummaries.setJobPostedBy("JOB_POSTEDBY");
jobSummaryList.add(jobSummaries);
it should be :
` while (rs.next()) {
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);
`
Related
I have a phonebook project I'm trying to make for school.
I want to be able to change a value of a certain field.
I print the field that are changeable.
before i had PRIMARY KEY it changed everyone for example if i changed the name, it would change everyone that is on the list name.
Now I tried to catch a specific one with a id as primary key and it throws this exception:
Parameter index out of range (2 > number of parameters, which is 1).
(this code now works if someone will see it in the future :)
I incremented the contactNum in the editConatct() to match it with the id field in my MySQL data base )
my edit code:
protected void editContawct(){
System.out.print("Which One You Want To Edit: ");
ArrayList<String> fields = editContactHelper();
ArrayList<Person> people = checkMoreThanOne();
int contactNum = menu(people);
int option = menu(fields);
System.out.println("Please Enter The Changes");
String changes = MyUtills.readStringFromUser();
String sql = "UPDATE " + DB_NAME + " SET " + fields.get(option) + " = ? WHERE ID = ?";
try {
statement = db.prepareStatement(sql);
statement.setString(1, changes);
statement.setInt(2,contactNum + 1); //contactNum `incremented to match 'id' field in my MySQL.`
statement.executeUpdate();
System.out.println("worked ?");
} catch (SQLException e) {
e.printStackTrace();
}
}
//todo add a param that accepts a String that is the data base name.
private ArrayList<String> editContactHelper(){ // should accept data base name
ArrayList<String> fieldsName = new ArrayList<>();
//todo switch the constant to the accepted argument (data base name). so if someone else wants to use this method :)
String sql = "SELECT * FROM " + DB_NAME; // should be databaseName;
try {
ResultSet resultSet = db.prepareStatement(sql).executeQuery();
ResultSetMetaData meta = resultSet.getMetaData();
for (int i = 1; i <= meta.getColumnCount(); i++) {
if(meta.getColumnName(i).equals("id")){
continue;
}
fieldsName.add(meta.getColumnName(i));
}
System.out.println("it worked Homie");
return fieldsName;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
Each ? is your query should relate to one parameter
But you have a mismatch
statement.setString(1, changes);
statement.setInt(2,contactNum);
as you only have one ?
I have code
public static String getData(String query) {
String output = "";
try {
String connectionUrl = "jdbc:sqlserver://localhost:1234;databaseName=123;user=123;password=123";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "select smth from tableName where smth";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
output = (String) rs.getObject(1);
}
rs.close();
}
catch (Exception e) {
return "ERROR while retrieving data: " + e.getMessage();
}
return output;
}
It works if value is string. But if it integer? Or boolean? How to modify this method so it would be universal, no matter what type data I get I still return it as string?
First retreive the result in ResultSet rs,
then you can write the code like below.
You can check the instance of the object and than assign the value.
String str;
Object obj = (Object)rs.getObject(1);
if(obj instanceof String){
//do you work here for string like below
str=(String)obj;
}
else if (obj instanceof Integer){
//do your work for Integer
}
// same you can do for other types
in this line
output = (String) rs.getObject(1);
if string then use
output = rs.getString(1);
if int
output = rs.getInt(1);
click oracle for more info
You can't accurately do that without using ResultSetMetaData class to get the column type.
Get the column data according to the type of the column.
You are getting the value from the resultset presuming that it is always a String and trying to typecast the Object instance. You should make use of the retrieve methods based on the type. Most of the cases, we will be knowing the datatype of the column values from which we retried the data. You can write the program based on the column's type. that's why ResultSet API has a method for each datatype.
For String
rs.getString(1);
For Int
rs.getInt(1)
Please read the documentation of ResultSet
while (rs.next())
{
String[] data;
data = new String[100];
data[i] = rs.getString("smth");
i = i + 1;
}
Try this you got your data in array.. use array instead of object.
what about toString()?
while (rs.next()) {
output = rs.getObject(1).toString();
}
I want to count the numbers of entries in resultset and then store these values in an array and pass this array to create a graph.
ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+" as
call from tablename"); // this statement will select the unique entries in a
particular column provided by jtextfield
int count=0;
while(rs.next())
{ ++count; } // This will count the number of entries in the result set.
Now I want to store the values of result set in an array of string. I used the following code
String[] row = new String[count];
while(rs.next())
{
for (int i=0; i <columnCount ; i++)
{
row[i] = rs.getString(i + 1);
}
}
Error : Invalid Descriptor Index.
Please suggest how to copy the result of resultset in array.
For example if I enter priority in jTextField , the result set will contain
priority1
priority2
priority3
In your first while loop you read all the entries in the ResultSet, so when executing the second while loop there's nothing else to read. Also, the index of ResultSet#getXxx starts at 1, not at 0. Also, since you don't know the amount of rows that you will read, it will be better using a List backed by ArrayList instead.
Considering these, your code should look like:
ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+
" as call from tablename");
List<String> results = new ArrayList<String>();
while(rs.next()) {
results.add(rs.getString(1));
}
Based in your comment, I extended the sample:
public List<String> yourRandomQuery(String columnName) {
Connection con = null;
ResultSet rs = null;
List<String> results = new ArrayList<String>();
try {
String baseQuery = "SELECT DISTINCT %s AS call FROM tablename";
con = ...; //retrieve your connection
ResultSet rs = stmt.executeQuery(String.format(baseQuery, columnName));
while(rs.next()) {
results.add(rs.getString(1));
}
} catch (SQLException e) {
//handle your exception
e.printStacktrace(System.out);
} finally {
closeResource(rs);
closeResource(con);
}
return results;
}
//both Connection and ResultSet interfaces extends from AutoCloseable interface
public void closeResource(AutoCloseable ac) {
try {
if (ac != null) {
ac.close();
}
} catch (Exception e) {
//handle this exception as well...
}
}
public void someMethod() {
//retrieve the results from database
List<String> results = yourRandomQuery(jTextField.getText());
//consume the results as you wish
//basic example: printing them in the console
for(String result : results) {
System.out.println(result);
}
}
Try this
ResultSet rs = stmt.executeQuery( "SELECT distinct "+jTextField.getText()+" as
call from tablename");
List<String> list=new ArrayList<>();
while(rs.next())
{
list.add(rs.getString(1));
}
Why not just create a HashSet<String> and write into that. Note that HashSet is unordered, just like your query. By using a collection that is of arbitrary size you don't need to determine the require dsize in advance.
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.
I'm using Spring-orm and HibernateTemplate to execute a native SQL query (DB is Oracle 11 for the reference), like this:
#Override
public List<Object> executeNativeQuery(final String queryStr, final Map<String, String> params) {
List<Object> results = this.template.execute(new HibernateCallback<List<Object>>() {
#Override
public List<Object> doInHibernate(Session session) throws HibernateException, SQLException {
// Get the query
Query query = session.createSQLQuery(queryStr);
// Assign parameters to the query, if any
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
query.setString(entry.getKey(), entry.getValue());
}
}
// fire the query
#SuppressWarnings("unchecked")
List<Object> res = query.list();
return res;
}
});
return results;
}
I've managed to successfully execute the query and get the results back. But I couldn't figure out a way to also get the resulting column names, and I'm starting to think that's not possible using this approach.
My problem is that I have to execute a query that comes from user input and I have no clues about parameter names.
Any ideas?
I finally found a way through it, so I post it hoping it'll be useful for others. I was doing it the wrong way, the correct way (at least for what my needs are) is to use doWork.
instad of:
session.createSQLQuery(queryStr);
I had to get the connection like this:
session.doWork(new Work() {
#Override
public void execute(Connection con) throws SQLException {
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(queryStr);
ResultSetMetaData md = rs.getMetaData();
int col = md.getColumnCount();
System.out.println("Number of Column : " + col);
System.out.println("Columns Name: ");
for (int i = 1; i <= col; i++) {
String col_name = md.getColumnName(i);
System.out.println(col_name);
}
} catch (SQLException s) {
System.out.println("SQL statement is not executed!");
}
}
});
Try the following to get column names in Hibernate:
public ArrayList<String> getTableDesc(String tableName){
System.out.println("getFieldNames:start"+tableName);
Object[] a;
List<Object[]> fieldNames = new ArrayList<Object[]>();
ArrayList<String> tabFieldNames = new ArrayList<String>();
Session session = getHibernateTemplate().getSessionFactory().openSession();
try{
String queryStr = "desc "+tableName;
fieldNames = (List<Object[]>) session.createSQLQuery(queryStr).list();
for(int i=0;i<fieldNames.size();i++){
a = fieldNames.get(i);
tabFieldNames.add(a[0].toString());
}
}
catch(Exception e){
System.out.println("exception "+e);
}
finally{
session.close();
}
System.out.println("getFieldNames:end"+tabFieldNames.toString());
return tabFieldNames;
}
You can use ResultTransformer class to map the query result to an entity class.
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#d0e17313
EDIT 1:
for( int i= 0; i< ((Object[])res.get(0)).length ; i++){
//do something with data
res.get(0)[i]
}