Get the database fields programmatically in Java - java

I have a jrxml file (sample shown below) which has the database query embedded in it. Now this query will return different columns against different databases. Since the columns are varying, I am planning to programmatically load the jrxml file, read the fields returned from the query (embedded in jrxml) and then place them on the jrxml
Have 2 questions
How do I get the field names returned from the query (embedded in jrxml)
How do we iterate through those fields so that they can be placed on the jrxml
Amy sample code would be appreciated.
Please note my preference is to use Jasper API's only.

How do you intend to query the database if you do not know the column names? The only case I can think of is that you are always going to select all the columns.
What I think you need is a parameterized query which will allow you to pass column names as parameters. See this page on using report parameters.
If you really want to always select all the table columns then before filling the report you will have to retrieve the table metadata and pass the column names to the report as parameters. If you are using JDBC then you simply need to call java.sql.Connection.getMetaData() and query the MetaData object for column names. However, hardcoding SELECT * is potentially dangerous because your result sets will keep growing in size as new columns get inserted into the table.

Related

Access data from a BIRT dataset within a Java class

I am relatively new with BIRT eclipse. I created a java project and a report file. The report has an Oracle DB connection, and I am working with one table. I want to access BIRT datasets with Java, so I can manipulate data rather than using only JavaScript. I have an idea in which I could connect them through importing the Java class to the onFetch() method on the dataset, but I'm unsure.
What you want to use are "computed columns". For a data set you can add computed columns additional to the fetched columns from your data source. In the computation script you can reuse the fetched columns as input for your computation. As a result your data set contains your fetched columns and your computed columns.
This example adds 1 to the fetched value of the column PK:

JDBC claims table does not exist

I am using the embedded h2 database inside of the play framework. I am trying to insert some test data into it, then retrieve that data.
as you can see, the right side shows me that there is indeed a suscribers table, but when being run, I get this:
What am I doing wrong?
Try with dbName.tblName, for example:
insert into dbName.tblName ...
and also:
You are inserting 2 columns out of 3 columns per table. I think you have to mention column names as well.

Can a Derby database contain rows with different numbers of columns?

ie, I want to dynamically create extra columns for specific users if required from the JSP pages of my web app - is this possible, or is there another way of achieving the same thing?
Short answer is no. Every row in a table must have the same number of columns.
If there is no applicable value for a column one typically inserts NULL (SQL NULL which is different from Java null). Alternatively you could change your data model and put the optional values in a different table, and use a join when you want to read the optional columns.
Finally, you could also represent the optional info in a Java object and serialize that into a Blob which you store in your table, but I would caution you against this approach since it prevents you form querying on the values in the Blob and you get an upgrade problem if the format of the Blob object changes.
Hibernate allows you to dynamically create database table provided
you know rows and columns
If you dont want to add that and still keep it simple and sweet use "<% scriptlet %>" codes in jsp containing Java code to create or modify table
Instead of using scriplet you could use Struts / JSF/ Spring tags for cleaner code

Metadata of Check constraints SQL Server

I have a SQL Server database that holds a table where a varchar column has a check constraints on it to make sure only a few different words can be entered as a value (names).
Like this
CONSTRAINT chk_Names CHECK (name IN ('John', 'Eva', 'Carl', 'Fred'))
What I want do do is to populate a combobox in java with these names, and I don't want to manually enter them since they might change in the database. I want to populate it from metadata.
But I haven't been able to find a way to get the information from the database either with the INFORMATION_SCHEMA or sys.objects (or from DatabaseMetaData in java for that matter)
I'm quite new to SQL Server but is it possible to get that information somehow?
Regards
/Fred
It sounds like you should move the list of names to a table. You're Java form could select the data from the table.
And, because the data can change, it will be better to update the table than to change the check constraint. You can change the check constraint to a foreign key constraint too.
You can also find the check-constraint definitions in INFORMATION_SCHEMA.CHECK_CONSTRAINTS. The expression is in the CHECK_CLAUSE column; and, you'll have to extract the values from the expression.

how to create column in mysql table at runtime

In my web application I have a form in that I am creating dynamic select boxes and text boxes as per need(It can be more than 1), default there is one pair of select box and text box and my table has two columns for storing values of that select box and text box and values are getting store in my table , but problem is if I add more than one pair of select And text box in my form how to store values in my table? Could it be possible to create columns in table at RUN TIME as per need.
If you can count the number of text boxes and select boxes you can use sql's alter table query at runtime to add columns dynamically.
You created a bad schema for your application.
To simply solve this try schema like this:
groupid, selectbox, textbox
and store the data in more rows.
For example if there is 2 pairs of boxes then you store two rows:
1, 'select', 'text'
1, 'select2', 'text2'
And so on.
You can easily create by using the good old JDBC; you can see an example here. As suggested by others, this is not a proper design approach.
I do not know if it is possible by using JPA since the table creation is a property that you need to specify in persistence.xml . JPA can automatically create tables, but you will lose all data each time you want persist data to an existing table (unless there is some override configuration parameter which is unknown to me).

Categories