how to create column in mysql table at runtime - java

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).

Related

Get records that don't have their ID in a seperate String Set - DynamoDB

I'm using Amazon Dynamo DB to down load a number of records to Android.
I have 2 Tables.
Table 1 contains a Set of Strings containing ID's
Table 2 has records each with an individual ID.
I want to download 10 records from Table 2 only if the record ID does not appear in the Set of strings in Table 1.
I can do this by downloading all the records in table 2 and then not saving /displaying the ones that appear in the String Set in table 1. However is there a way to only download the ones that don't appear in the String Set?
Any ideals would be appreciated.
Many Thanks
In order to query a dynamodb you need the attribute to either be a range key or partition key which in terns need to be scalar so you cannot directly query what you want. Your best chance of doing what you want if I understand your requirement is scan operation. Scan the whole table(2) and then use queryExpression to filter the results you get using a nested query in table one. This demands you to make the "Set of Strings" either either partition or range key in your first table.

Get the database fields programmatically in 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.

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

Loading a Single column out of 50 Using Load() of Hibernate

Suppose there are 50 columns in database table. Each column contains image(binary data). You need to display only one one image out of 50 at a time. How would you achieve the same via hibernate? Keep in mind that executing load on the table will result in loading complete row while we need only one column data.
Pavnesh, I am answering the question in a geenral fashion. If you just one one column from the table create a named query and return that single column, and retrieve the data in your DAO class with proper datatype.
and if you want just a single row use query.setMaxresult function and give the value as 1 and then you can select the particular column value from the row,but however in this case only the top row it will return.

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.

Categories