Retrieve column names from java.sql.ResultSet
You can get this info from the ResultSet
metadata. See ResultSetMetaData
e.g.
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
String name = rsmd.getColumnName(1);
and you can get the column name from there. If you do
select x as y from table
then rsmd.getColumnLabel()
will get you the retrieved label name too.
How to get column name list from java.sql.ResultSet
try this...
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnCount = rsmd.getColumnCount();
// The column count starts from 1
for (int i=1; i<=columnCount; i++ ) {
String name = rsmd.getColumnName(i);
// Do stuff with name
}
Java - How to get Column name on Result Set
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName[] = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
System.out.println(columnName[i-1]);
}
Java: How to retrieve SQL resultSet data through column name?
Interface java.sql.ResultSet
has a collection of getter functions. The pattern is
getXxx(int columnIndex)
getXxx(String columnLabel)
Where Xxx is the data type to be fetched. The second variant fetches a value using the column header label.
To adust the cursor to a specific row, the function is
absolute(int rownum)
If the call returns false it means there are no rows at that number. Caveat, not all drivers and queries allow navigation to any row number like that.
Java : Retrieve Column names from ResultSet
Ok I found it.
I changed the PLSQL Type TYPE_MYOBJECT before and I did not give again the grant to the user of the application. That is the reason why it says "No data found".
Just doing the :
grant execute on TYPE_MYOBJ to USER_OF_APPLICATION;
solves the problem.
how to get a column name from resultset in java
You need the ResultSetMetaData
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
and for your use case, check out getColumnName()
How can I print the column names from 3 tables joining JDBC?
As suggested in https://stackoverflow.com/a/696794/11226302
You need to get the ResultSet meta data to programmatically get your column names from db.
Else, you can manually enter the names as suggested in other answers.
Java ResultSet - get Column name based on Index
I think you need to look at ResultSet.getMetaData()
which returns the meta-data associated with a ResultSet
.
You can then iterate over the columns (use getColumnCount()
to find out how many there are) to find the column with the given name, checking with getColumnName()
. Don't forget that column indexes are 1-based, rather than 0-based. Something like:
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount();
for (int i = 1; i <= count; i++)
{
if (metaData.getColumnName(i).equals(desiredColumnName))
{
// Whatever you want to do here.
}
}
If you need to do this for a lot of names, you may want to build a HashMap<String, Integer>
to map them easily.
Related Topics
Why Doesn't Java Offer Operator Overloading
Can Enums Be Subclassed to Add New Elements
How to Randomly Pick an Element from an Array
Java Maximum Memory on Windows Xp
Java Event-Dispatching Thread Explanation
Which Overload Will Get Selected for Null in Java
How to Configure Port for a Spring Boot Application
Why Should Java 8's Optional Not Be Used in Arguments
Why Is Creating a Thread Said to Be Expensive
Why Java Needs Serializable Interface
Java - Method Name Collision in Interface Implementation
Converting Java Objects to JSON with Jackson
Unrecognized Ssl Message, Plaintext Connection? Exception
How to Execute In() SQL Queries with Spring's Jdbctemplate Effectively