0
I know that with the resultset we can do several things with a SELECT done in some table. For example: I know that we can take a table and print its contents, listing which columns we want with methods such as: getString and getInt.
Conection con = ........
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Clientes");
while(rs.next()) {
String nome = rs.getString("Nome");
System.out.println("Nome do Cliente: " + nome);
}
But if I just want to show you all the content of any table I receive by parameter, they don’t serve those methods. There would have to be something like:
System.out.println (rs.getAll());
That is, I want a method that takes the result of my SELECT * and shows on the screen, regardless of which or how many columns are in the table.
Exsite on Resultset this?
:The Excellent. I didn’t know some guys you used there. It helped a lot. Thank you!
– Lucas Pletsch
One more thing: Is this "prepareStatement()" method not used only when, in our system, we often have the characteristic of executing the same "statement? In other cases we could directly use a Statement st; st.executeQuery(); ?? What is best practice?
– Lucas Pletsch
Felipe Marinho: a problem with using Stringjoiner, is that it simply puts one string on the side of the other. What generates your table misaligned, depending on how many characters each table item has: [ID = 1,NAME = Programmer,NIVEL = technician] [ID = 2,NAME = Systems Analyst,NIVEL = technical] [ID = 5,NAME = Tester,NIVEL = technical] [ID = 6,NAME = Tester,NIVEL = technical]
– Lucas Pletsch
@Lucaspletsch On the
PreparedStatement
: besides this benefit you cited, it also help prevent SQL Injection if used correctly. In this example I gave, the use of it is not necessary, you can use a simpleStatement
. As to theStringJoiner
: he just joins the Strings with the delimiters, prefix and suffix informed. If you want a more elaborate formatting (which displays the data as a table), you need to check which is the longest value of each column to set its minimum size.– Felipe Marinho
About Stringjoiner: exact. I just haven’t figured out how to edit the "column" width yet. kkk
– Lucas Pletsch
@Lucaspletsch Suppose the
String
calling formaior
be the largestString
of the column, and thats
be any otherString
of that column. To edit the "width" just doint v = (maior.length() - s.length()) / 2
. Now just addv
blank spaces before and afters
(casemaior.length() - s.length()
be odd, you will have to add one more space before or afters
).– Felipe Marinho