I have to read from a ^ delimited file and then pulate a table. Table has nullable integer columns. When there is no value specified in the input file, the column should be made to null.
I need to process the input data first and then insert into the table. So I am reading them into a valueObject with integer data elements. But if I then try to create table rows from the valueObject, integers get converted into 0 values.
How do I avoid this?
Thanks
I think you have 2 possible ways:
1/ If you use primitive int: you can use a special code, for example, you can use -1 (in case all other values are positive), you can use Integer.MAX_INTEGER or Integer.MIN_INTEGER. Then, when you put data into the database, you can check for this special code, if the value is equal to the special code, you put the null instead.
2/ You can use an Integer object, then, if you see no value in file, you you point that object to null. Then when you write to database, it should just work
Related
I want to print 4 different values from my database table in 4 different Java Textfields when u click in the corresponding row.
You can use String.valueOf(arg) to convert to string your values. Also if the values coming from your database are numeric, you might want to call getDouble or getFloat on your ResultSet object too. Don't forget to remove the casts in the query as you will tranform the values in the Java code instead, like this:
textFieldEröffnungspreis.setText(String.valueOf(rs.getDouble("Eröffnungspreis")));
I want to read values (integers) from a CSV file, and insert them into a Database Table.
However, for a certain column of the table, if the values received from the CSV file don't belong to an interval, the value to write in the Database should be NULL.
For example in my Java App, I do : insert.setNull(3, java.sql.Types.INTEGER); in that cases.
But how can I do this in Talend, if the column type is 'NUMBER' ?
Just use a tMap component between yout tFileInput and database output instances to carry this simple transformation. Inside theis component, on the bottom right pane (the output schema) select the checkbox to let your field be nullable. Be careful, since fields coming from CSV are not nullable by default.
Finally, on the upper right pane (output transformation), on the target field (the one you forced to be nullable) write a ternary expression. For example:
(assuming your incoming connection is called input_row)
(input_row.field >= 0 && input_row.field <= 3) ? <value to write if inside interval> : null
What I want to say is that if the field is nullable on both Talend side and on the target DB table, the boring java null is your friend.
I am working with a program that i have a record for every user. My users have a property with key, PhoneNumber , and its value is an array of strings, [454457,897356]. For example if i wanted to use cypher query:
Start n=node(1)
Return n
It returns 1 record for my node(one row) that the value of column PhoneNumber is an array.
But i want to have record numbers according to the number of values in my array, means that for my example, the query returns 2 records(2 rows) and all of its attributes be the same but in the PhoneNumber column one of them has the value 454457 and the other has the value 897356. Is any way to do that? do i change my cypher query or make some changes in my java code?
Thanks.
There is no way to do that yet, within Cypher. I've submitted a request for it, though:
https://github.com/neo4j/neo4j/issues/30
I have a method insert() which inserts a list of values into a table which is chosen by the user.
The problem is that since the user gets to choose the table, the method does not know how many values that are to be inserted and of which type they are. I've solved the variable amount of values with a loop that uses a stringbuilder to insert the correct amount of "?"-chars into the values part of the query.
I also have a loop that splits the values received into a String array, but I then have a problem with ints being processed like strings. Can I get around this using some trick with sql-syntax, or do I need to fetch information about which kind of data type each value is?
And if I have to fetch info about the data types, how do I do that? (Preferably an sql query that returns nothing but the types since I want to use the result directly in my java code).
Firstly, what I suspect you are doing is wrong, or certainly sub-optimal.
Assuming you were adamant this is how you want to go about it, you need to retrieve a row from your table and then call getMetadata() on the ResultSet.
You would end up with something like:
rs.getMetaData().getColumnTypeName(int column)
Once you know the column type, you can parse/sanitize your user entered data accordingly.
I need to parse the value of a database column that generally contains integers, based on the result Set generated from a JDBC call. However, one particular row of the column has two integers in it (ie, "48, 103"). What will be the return value of resultSet.getInt() on that column?
It will throw an exception.
I think you are taking the wrong approach here. The getXXX() is supposed to match the data type of the table. Is the data type on the table listed as VARCHAR? If that case you should use getString() to get the data and then parse it with the String.spilt(",") if the , exists (you can use String.indexOf() to verify is the comma is there or not).
You'll almost certainly get a SQLException (or possibly a NumberFormatException). The actual interface just says that the result set will return "the value of the designated column... as an int". The exact details will be implementation-specific, but I doubt you'll get anything sensible from a value of "48, 103".
(Personally I think it's an error if the driver lets you call getInt on that column in any case, even for "sensible" values. A string is not an int, even if it's a string representation of an int, and the conversion should be done manually by the developer.)
I'd expect it to throw an exception. If it does give you a value, it won't be what you want. I'd get the values as strings and parse them, splitting on commas and trimming spaces.
I believe it's a NumberFormatException.