Printing all columns from "Select *" query in java

java, sql

Solution

This line is the problem:

String all = rs.getString("*");

you have to provide the column name in the getString() method. Presently it assumes `*` as a column name which is not correct and hence shows an error.

You have to provide the column name in that like this:

String all = rs.getString("Column_Name");

EDIT:-

You may try this if you dont want to write the names of the columns

ResultSetMetaData md = rs.getMetaData(); 
int colCount = md.getColumnCount();  

for (int i = 1; i <= colCount ; i++){  
String col_name = md.getColumnName(i);  
System.out.println(col_name);  
}

Problem

I want to know how to print "Select *" query in java ? I tried this function but it give me this message "invalid column name "after execution. ``` public static void ask() throws Exception { try { java.sql.Statement s= conn.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM DB1.T1"); System.out.println("**************Resultats**************"); while ( rs.next() ) { String all = rs.getString("*"); System.out.println(all); } conn.close(); } catch (Exception e) { System.err.println("exception! "); System.err.println(e.getMessage()); } } ```

Original source