Sorry for typo. this is not "i" but its "j" that will iterate
the expected result of this query is: 1, test but now its printing selectedcolums=1 and j=2 that means loop is running right but selectedColums+= is getting just one value.
Can I use selecteColums String to store more than one Strings with + operator?
Your declare i as a variable at the start of your program. Why?
You will only concatenate a String if you have two results returned. So you should print the contents of a.aa from the first row concatenated with cc
from the second row. If you have any more than two results you will get an exception.
I would revisit your logic if I were you.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Paul Sturrock wrote:Your declare i as a variable at the start of your program. Why?
first I was trying to store as Array that's why I mentioned on top but the logic was not useful
You will only concatenate a String if you have two results returned. So you should print the contents of a.aa from the first row concatenated with cc
from the second row.
I am trying to concentrate on string but due to incompetency I am unable to concatenate two or more resulted strings.
I am trying to store the resultset in string and to pass it in a method. Please help me again
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
I am trying to do like this but getting wrong output i.e. 1,
its not getting the next string. Why?
Again, why are you using two variables to track a single number? Could you not just use a single variable?
You know you are looking for two values because you define these in your SQL. Would this not be easier:
Because you assign a value to to your variable slectedColums in a loop this will also concatenate whatever is in each row of your ResultSet. Is that what you intend?
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
This is what I need rs.getString(1) + rs.getString(2); to automate.
I want to generate rs.getString(int) and then to store them in a string as well as to pass them into another method
OK. We'll what you are doing is not safe (see my previous comments).
If you need to know about a ResultSet's meta data there is a class that will describe this (java.sql.ResultSetMetaData) so you can safely navigate through the various columns that may be present.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Honestly I never did any work with ResultSetMetaData class but googled on the web and found that this is data about data not to display records/resultSet. I don't know how you recommend this for my case?
Still I am not understanding that why my this code is not working by getting two columns instead of first one
Did you read the JavaDocs? Always better than trying random stuff found elsewhere. There is an example of what you need to do right at the top of the class.
Still I am not understanding that why my this code is not working by getting two columns instead of first one
Get the column indexed at j and assign to the element i in the array aa. First time round j== 1 and i = 0, so you get the field "aa" from the first row in the ResultSet
concatenate this to the variable slectedColums
Increment i and j
So if your result set has one row, slectedColums will be the value in the first column of the first row. If the ResultSet has two rows it will be the value of the first column in the first row concatenated with the value of the second column in the second row. If it has three rows, slectedColums will be the value of the first column in the first row, plus the value of the second column in the second row, plus the value of the third column in the third row etc.
What it won't be is the contents of all columns in the first row. And when you hit a column that is not a text data type, or j references a number higher than the number of columns in your select statement you will get an exception.
What ResultSetMetaData will give you is an understanding of the number of columns in a ResultSet and the type of those columns. Form this its fairly straight forward to write code to get the correct value from each column based on an index.
Your code is still utterly dependent on the number of results in your ResultSet. Why are you using the variable j? And for that matter why the intermediary step of assigning to an array?
Your code is also dependent on the data type of the columns in your ResultSet always being text. If they are not, unexpected things will occur.
Hi Actually ResultSetMetaData would be useful to know the Column count of the query, Also we could even know even the column names if required. It got many other features.
I got your point .. and hope the below code would be useful for you
All the best >
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Hi Ravi,
Its throwing exception:
and if I am doing j=1 then
Its printing just first column twice not second column
Smarty Ravi's code prints the result of the toString() method of ArrayList. If you want different formatting you will need write code to handle that yourself.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Thanks Ravi and Paul. I learned a lot
Thanks again
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Hi again,
I am unable to pass the data as separate parameters to the method as the method is receiving this data as one parameter. what should I do in this case?
I am unable to pass the data as separate parameters to the method as the method is receiving this data as one parameter. what should I do in this case?
Can you give me the signature of your method or your method code. So that we could analyze it and support you.
Looks like you have not imported the ArrayList class.
As a general rule it makes sense to code method names to use the interface List, rather than ArrayList. This leaves you the room to change the implementation.
So you are accessing an element of an array which doesn't exist. So you've possible got an array or List with n entries and you are trying to access the n + 1 entry.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
I am checking again & again the above code but unable to understand where it indexing more then its length
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
I change to this code and its working fine
Thanks Paul and Ravi.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Hello again,
This returns one record obviously as we are getting 0th and 1th element but how I'll display more than one record as I am not able to set the length of the records in loop and if I am giving length by myself then its not working at all
As you are having more than one record you should add each record seperately into a arraylist. At last you should add all arraylists to one arraylists .
try this code :
Also change the below code as follows
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Thanks Ravi for helping me again but its throwing the following errors: