resultset to get values from next row - arrays

hi i'm new to java how ever i googled and got code to connect to database and retrieve the values.
the problem is ResultSet gets the value only from FIRST ROW of the table
here is the code..
String stmt = "select * from tablename";
ResultSet resultSet = statement.executeQuery(stmt);
while(resultSet.next())
{
String val = resultSet.getString(3);
System.out.println(val);
}
it returns only the 3rd value (because i have mentioned that and i need that column value only) of the first row, but i need to get the every 3rd value (column) of every row and store it in a array.

The code you have posted should already be getting the 3rd value in each row. There's nothing obviously wrong with what you've shown. It's just a matter of trying different things and figuring out what it is that you think you know that isn't true.
Verify the query works by itself in a SQL client like Squirrel or whatever you have handy.
Add printlns or logging to verify you're actually executing the part of the code that has the query.
Add some text to the println in your code so that if the value is blank you'll still see something get written.
Change the query to state the column explicitly instead of leaning on *, so it reads like select mystuff from tablename, and see if you can make that query work.
Once you are ready to add the part where you store the values, you would be better off using an ArrayList than an array, because the list will resize as you add things to it. ArrayList is backed by an array, the ArrayList takes care of the resizing for you. If you really do need an array you can call the toArray method on the list to create one once it's populated. So it would look like:
List<String> list = new ArrayList<String>();
while(resultSet.next())
{
String val = resultSet.getString(3);
list.add(val);
System.out.println(val);
}
String[] vals = list.toArray(new String[] {});

Related

concatenate two columns in a datagridviewrow and find that value in a list

I have trying to filter out data but am having a tough time getting the statement correct.
I have a DataGridView that has several columns. Two of the columns I would like to concatenate but also see if that concatenation exists in a list.
This is the list I want to compare to
List<string> findList = new List<string>{"1A","2A","1B","2B","1C"};
This is the searching I can't get right.
List<string> filteredList = (List<string>)dgvSheet.Rows.OfType<DataGridViewRow>().Any(r => findList.Contains(r.Cells["Priority"].Value.ToString().Concat(r.Cells["Probability"].Value.ToString()).Select( p => p.Cells["RID"].Value.ToString());
I've tried several versions but this one seems to express what I'm trying to do.

How to write a List(of String) into a SQL Server table where each string is a comma separated list of values

We have survey data for each survey on a server. The data is separated with one method I can download the column names (via pooling) and with another I can download the data (via SSE). I have accomplished to write a procedure that creates a datatable dynamically for each survey I choose to download. Now I have to get the data into that table. For this I have streamed the data via SSE into a List(of String) where each element comprises a comma separated string.
This looks like
For Each x As String In stringList
Console.WriteLine(x)
Next
would give me
(1)data:[1,2,1,5,2,6,John,Winchester,234]
(2)data:[5,3,2,4,1,6,Mike,Lenchester,555]
...
Each Element of the List is a dataset I have to put into one column of my datatable. So I guess I have to loop through the List Object and the pick each element between the comma and write them into the columns.
So my problem now is to get the data into the database.
Usually I provide an approach but this time I have no clue how to start.
I tried to experiment with this
.Parameters.Add("#id", SqlDbType.varchar(max)).Value = x
but ended up in frustration.
Could anyone give me something to start with? The data size is up to 500MB if I store it in a .txt file.

How to remove last character of last row in tmap using Talend?

I am extracting two columns using textractjson and passing it to a tmap component where I am concatenating both of the columns as single one.
Also, I want a comma at the end of each row except the last row.
I am not sure how to do this.
Example:
There are two columns in tmap as input:
FirstName
LastName
In output, I have concatenated them to:
FirstName+LastName+","
The problem is I dont want the comma in the last row.
This depends on your job layout and input structure.
You seem to use tExtractJSON, which could mean you get the data from a REST call or out of a database.
Since there is no fixed row amount because of the JSON data structure, you wouldn't be able to use ((Integer)globalMap.get("tFileInputDelimited_1_NB_LINE")). Again, since we don't know your job layout, this depends on it.
If this is not the case, I would count the rows in a tJavaRow component first and then add a second tJavaRow where I'd concat the strings (wouldn't do that in the tMap), but omit the last comma. I'd be able to find the last row with the count I did first. This depends on your Java skills.
You may also concatenate all the rows in a global variable using a tJavaRow, with a comma at the end for each row.
Then, start a new subjob (onSubjobOk) then using a tJava, remove the last character (so the last comma).
Pretty simple, don't have to know how many rows from the input flow but supposing you want the result as a single string (here contained in a global variable).
I might be wrong also. Basically concatenate FirstName and LastName , Create record number column using Numeric.Sequence() function and use one more context variable and store the same sequence number here(it might store last value) also in tJavaRow component.
output_row.name = input_row.FirstName+""+input_row.LastName+","+;
output_row.record_number = Numeric.sequence("recNo", 1, 1);
context.lastNumber = Numeric.sequence("recNo", 1, 1);
create a method in custom java routine.
public static string main _nameChange(String name,Integer record_number){
if(context.lastNumber == record_number){
name = name.substring(0,name.length()-1);
return name;
}
else{
return name;
}
}
Now call _nameChange method within tmap component. Now you can trim the last row's last character.
Ror reference check this

Can I reference a field by name using a variable?

I can get a value from a .net data table that has columns named "Col1" and "Col2" like this:
DataTable dt = new DataTable()
// some more code that fills it
Console.Writeline("{0}, {1}", dt.Rows[0]["Col1"], dt.Rows[0]["Col2"]);
I could also use a variable if my datatable has a lot of columns
string x = // something that will be one of the columns in the table
dt.Rows[i][x] = "Some new value"
Is anything like this possible in NAV with a Record variable?
Well "like" this but not exactly. You can use RecordRef type to get reference to a field. But to interact with the certain field you will still need to adress it by its field number. You can iterate through all fields in the table and check their names to find the one you need. Not performant though.

Export column value to another task SSIS

I have 1 .csv file that I import. Because of the layout of the file I need to import it again to get a certain value that needs to be used for the next Data Flow task that will use that value for every row.
Below is an example of the file:
I need STATEMENT DATE for all records.
This is how the package looks like:
Inside Get Statement Date task I have the following Data Flow:
I don't really want to export the column I just want it to go into a variable that I can use in the next task And I am not sure were this should be done.
I am still learning how SSIS works.
Any help would be much appreciated.
Use a script component as Destination and assign that column to your variable.
Keep in mind the variable Datatype should be compatible.
Declare a variable in class ScriptMain to store your value:
public DateTime t;
On Input0_ProcessInputRow(Input0Buffer Row):
t = Row.StatementDate;
On PostExecute:
Variables.teste = t;
It will assign it the number of rows you actually have.(it's not a problem since you are only retrieving one row I guess). Only the last row will actually be saved to your variable.
Use RecordSet Destination. This can be used to store temporary results which will be stored in a variable of type object. You can use this along with Foreach loop to process each record. you can refer this link for more info.
RecordSet Destination

Resources