留言板

How can we get the lsit of column names form the table

Manju Nath,修改在8 年前。

How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
I am trying to get the list of column names from the table. (Let's say table name is: USER_).

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ page import="com.liferay.portal.service.ClassNameLocalServiceUtil" %>
<%@page import="java.util.ArrayList" %>
<portlet:defineobjects />
<theme:defineobjects />
    &lt;%
    long userClassNameId = ClassNameLocalServiceUtil.getClassNameId("com.liferay.portal.model.User");
    List<user> columns =  
// How can I fetch the list of column names in to columns list ?
    %&gt;</user>

Is there any predefined methods to get the list of columns OR any other simplest way to achieve the same?
thumbnail
Tina Agrawal,修改在8 年前。

RE: How can we get the lsit of column names form the table

Expert 帖子: 297 加入日期: 08-1-3 最近的帖子
You can use http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredFields()

Tina
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
Can you please elaborate the answer. I didn't get solution from your suggestion.
Thank you
thumbnail
Tina Agrawal,修改在8 年前。

RE: How can we get the lsit of column names form the table

Expert 帖子: 297 加入日期: 08-1-3 最近的帖子
Try this -

User.class.getDeclaredFields()
And then iterate the fields and check.


Tina
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
I tried that, but the field list contains null. No columns are retrieved. Any suggestion please
thumbnail
Ravi Kumar Gupta,修改在8 年前。

RE: How can we get the lsit of column names form the table

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
Try this. This will list all the tables and columns.


try {
		        DataSource dataSource = (DataSource) PortalBeanLocatorUtil.locate("liferayDataSource");
		        Connection connection =  dataSource.getConnection();
		        DatabaseMetaData md = connection.getMetaData();
		        ResultSet rs = md.getTables(null, null, "%", null);
		        while (rs.next()) {
		        	System.out.println("Table : "+rs.getString(3));
		        	
		        	ResultSet cols = md.getColumns(null, null, rs.getString(3), null);
		        	while(cols.next()){
		        		System.out.println("\tCol: - " + cols.getString(4));
		        	}
		        }
		  
		    } catch (SQLException e) {
		    	LOGGER.error("SQLException: "+ e.getMessage());
		    }


Imports will be

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
From where can I import the Datasource? I am getting an error "DataSource cannot be resolved a type". Any suggestion?
thumbnail
Ravi Kumar Gupta,修改在8 年前。

RE: How can we get the lsit of column names form the table

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
import javax.sql.DataSource;
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
Able to fetch the all the tables with respective columns. Thank you so much
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
I have a weird problem, If I print a column names in the console the column names are showing fine. But If I print the column names in the web page the column names of each character is separating with hi-fen (-) as attached in the screenshot.

My Code is :

<liferay-portlet:actionurl portletConfiguration="true" var="configurationURL" />
<aui:form action="<%= configurationURL %>" method="post" name="fm">
&lt;%
ArrayList<string> al = new ArrayList<string>();
try {
    DataSource dataSource = (DataSource) PortalBeanLocatorUtil.locate("liferayDataSource");
    Connection connection =  dataSource.getConnection();
    DatabaseMetaData md = connection.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
       System.out.println("Table : "+rs.getString(3));
       String tableVal = rs.getString(3);
       if(tableVal.equalsIgnoreCase("USER_"))
       {
    	   ResultSet cols = md.getColumns(null, null, rs.getString(3), null);
           while(cols.next()){
               System.out.println("\tCol: - " + cols.getString(4));
               String str = cols.getString(4);
           	   al.add(str);
           }  
       }
   }
} catch (SQLException e) {
	System.out.println("SQLException: "+ e.getMessage());
}

Iterator<string> itr = al.iterator();
while(itr.hasNext())
{
	%&gt;
		<aui:input name="<%= itr.next() %>" type="checkbox" />
	&lt;%
}

%&gt;
	 <aui:button-row>
         <aui:button type="submit" />
      </aui:button-row>
</string></string></string></aui:form>


What 's wrong in my code? And why is it showing differently on UI than console?
thumbnail
Ravi Kumar Gupta,修改在8 年前。

RE: How can we get the lsit of column names form the table

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
That's weird.. I could get it without hyphen.. Check if some javascript doing something like this.. or there can be some other reason..
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 帖子: 11 加入日期: 15-3-27 最近的帖子
I haven't used any javascript. I used only the code which you provide me. Nothing extra in it. I tried multiple ways to display it properly but it is not happening. Please suggest some solution
thumbnail
Ravi Kumar Gupta,修改在8 年前。

RE: How can we get the lsit of column names form the table

Liferay Legend 帖子: 1302 加入日期: 09-6-24 最近的帖子
Try the same code on a vanilla liferay.
Manju Nath,修改在8 年前。

RE: How can we get the lsit of column names form the table

New Member 发布: 1 加入日期: 15-1-29 最近的帖子
Got the issue. All the characters are in the string are in upper case so for the same reason it is showing like that on UI. I have converted the string in to lowercase Now it seems to be fine on UI.

Thank youemoticon