how to invoke Result Set method in java? [duplicate] - java

This question already has an answer here:
Java Reflection: "java.lang.NoSuchMethodException"
(1 answer)
Closed 5 years ago.
ResultSet data_result = dao.getDetails();
Method resultset_method;
resultset_method = data_result.getClass().getMethod("getInt", Integer.class);
it is giving error:
java.lang.NoSuchMethodException: org.apache.commons.dbcp.DelegatingResultSet.getInt(java.lang.Integer)

The ResultSet's getInt method has two overloads, one that takes an int for the index and one that takes a String for a column name. You're attempting to retrieve a getInt() method, which indeed does not exist.
You need to supply the types that the method takes - eg, if you were going by name,
resultset_method = data_result.getClass().getMethod("getInt", String.class);

Related

How can I use '::' operation in this case? [duplicate]

This question already has answers here:
java8: method reference from another method reference
(3 answers)
Closed 1 year ago.
I want to know how to convert lambda expression printing string length to using '::' operation.
String[] arr = new String[]{"1", "234", "56"};
Arrays.stream(arr).forEach(s -> System.out.println(s.length()));
Excellent question! Here you could do so twice. Once to map each String to an int by calling length() on each String and a second time by calling println(int) on System.out. Like,
Arrays.stream(arr).mapToInt(String::length).forEach(System.out::println);

How to use var in Java [duplicate]

This question already has an answer here:
Any difference between `var x` and `int x`? [duplicate]
(1 answer)
Closed 1 year ago.
I am stuck on how to use var. Is there any difference between this two?
var input = (String) result.get("some field from DB");
String input = (String) result.get("some field from DB");
In java 10 and above when you use var you are allowing the compiler to attempt to decide what the variables type should be. In your example your casting a String so it will give that variable type String.
In your second example you are specifically telling the compiler that your variable is of type String.
The second is safer if you know what your variable type will be as it leaves no confusion down to the compile.

Why do we create object variables like this? [duplicate]

This question already has answers here:
Differences between new Integer(123), Integer.valueOf(123) and just 123
(6 answers)
create a new Integer object that holds the value 1?
(2 answers)
Advantage of using new Integer(a) over a
(3 answers)
using new(Integer) versus an int
(3 answers)
Closed 3 years ago.
I am new to learning Java, and I was told to create object variables like this:
Integer a = new Integer(2);
Instead of like this:
Integer a = 2;
Can someone explain why is creating object variables the 2nd way bad?
edit: I am adding this here cause I am getting mixed answers
Which one am I supposed to use and when?
The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
(Emphasis mine.)
See also: Autoboxing

How do I assign List<Project> to Project[ ] in Java? [duplicate]

This question already has answers here:
Converting 'ArrayList<String> to 'String[]' in Java
(17 answers)
Closed 3 years ago.
I am trying to initialize an array object with a get method which returns an arraylist object. I have tried using .toArray() to convert but it didn't work.
Would Project[] projects = list.toArray(new Project[[0]]) work? The reason it doesn't work normally is because by default toArray returns an Object[], and the JVM is unable to cast that to a Project[]. Passing in the project array allows it to determine the type of the desired array.

Error with Conditional Operator in Java [duplicate]

This question already has answers here:
Why can't I use the ternary ? operator to select between two function calls?
(4 answers)
Java: Ternary with no return. (For method calling)
(6 answers)
Closed 4 years ago.
c.getCollectibles()[i][j].isCollected() ? collectiblePanels[i][j].setSplash(c.getCollectibles()[i][j].getIcon()) : repaint();
I am writing a program in Java and have an error in my program. I am using a conditional operator. c.getCollectibles() returns a two-dimensional array of a Collectible class, and isCollected() is a non-static public method in the Collectible class that returns a boolean value.
collectiblePanels is a two-dimensional array of a class that contains a setSplash() method. The setSplash() method takes in the same type that getIcon() returns (the specifics are irrelevant to the error). Overall, setSplash() is void.
repaint() is a random void method. Again, specifics are irrelevant to the error.
The specific error is below:
The left-hand side of an assignment must be a variable
Syntax error on token "?", invalid AssignmentOperator
Syntax error on token ":", ; expected

Categories