Fórum
How can we get the lsit of column names form the table
Manju Nath, modificado 8 Anos atrás.
How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
I am trying to get the list of column names from the table. (Let's say table name is: USER_).
Is there any predefined methods to get the list of columns OR any other simplest way to achieve the same?
<%@ 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 />
<%
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 ?
%></user>
Is there any predefined methods to get the list of columns OR any other simplest way to achieve the same?
Tina Agrawal, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
You can use http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredFields()
Tina
Tina
Manju Nath, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
Can you please elaborate the answer. I didn't get solution from your suggestion.
Thank you
Thank you
Tina Agrawal, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
Try this -
User.class.getDeclaredFields()
And then iterate the fields and check.
Tina
User.class.getDeclaredFields()
And then iterate the fields and check.
Tina
Manju Nath, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
I tried that, but the field list contains null. No columns are retrieved. Any suggestion please
Ravi Kumar Gupta, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Try this. This will list all the tables and columns.
Imports will be
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, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
From where can I import the Datasource? I am getting an error "DataSource cannot be resolved a type". Any suggestion?
Ravi Kumar Gupta, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
import javax.sql.DataSource;
Manju Nath, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
Able to fetch the all the tables with respective columns. Thank you so much
Manju Nath, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
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 :
What 's wrong in my code? And why is it showing differently on UI than console?
My Code is :
<liferay-portlet:actionurl portletConfiguration="true" var="configurationURL" />
<aui:form action="<%= configurationURL %>" method="post" name="fm">
<%
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())
{
%>
<aui:input name="<%= itr.next() %>" type="checkbox" />
<%
}
%>
<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?
Ravi Kumar Gupta, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
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, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Postagens: 11 Data de Entrada: 27/03/15 Postagens Recentes
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
Ravi Kumar Gupta, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Try the same code on a vanilla liferay.
Manju Nath, modificado 8 Anos atrás.
RE: How can we get the lsit of column names form the table
New Member Mensagem: 1 Data de Entrada: 29/01/15 Postagens Recentes
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 you
Thank you