Reputation: 27
I'm making a database program. I want the user to be able to define their own columns, as many as they want. How would I then define each record in its class file?(Since the properties would be different user to user)
It's part of a school assignment-it's going to hold different scores and the likes for the teacher for different students they can add, but they will also be able to add a new assignment, test(a column) .
Upvotes: 1
Views: 306
Reputation: 11
You actually dont need a class. Basically, in the Java world all classes needs to be compiled before they can be executed. So dynamically adding fields to a class based on property is not a good approach. Rather, I would recommend you to use collections. This can be achieved like:
List<Map<String,Object>> data;
You can have the Map like column name as key and value of the column value. The keys can be configured in properties and data can be stored in any order.
Upvotes: 1
Reputation: 30865
In this type of task the main problem is how to store the configuration. You do not need to create a class for each structure modification.
You should divide your task into three.
A1: How to extend a table ?
If you want to extend a table with column you should have some data to do that.
So you should get from the user:
Having those data you can easily, create an ALTER statement to add column.
A2: How to execute query on a dynamic table ?
We have two option here, clever or normal.
Ist up to you how you will retrive those data what is importart is how would you store them in yor application. I suggest you to create some object that will map the table.
The result set
TABLE_A | COLUMN_1 | String
TABLE_A | COLUMN_2 | Integer
TABLE_A | COLUMN_3 | Boolean
Simplyfied code
UserTable userTable = new UserTable("TABLE_A");
userTable.addColumn(new UserTableColumn("COLUMN_1",String.class));
userTable.addColumn(new UserTableColumn("COLUMN_2",Integer.class));
userTable.addColumn(new UserTableColumn("COLUMN_3",Boolean.class));
Having such structure you are abre to easily create a query against such table.
A3: How to manipulate data in dynamic table ?
This is the tricky part. There exists various of ways to do this. The less complicated is to create an object that have a key-value structure. where key is the column name and value is the value. What is important is to define the primary-key, the indentyfier that allow you in simple way to establish that this is new row so you should insert it or old one that need only update.
public class TableRow {
private Map<UserTableColumn,Object> rowData = new HashMap<UserTableColumn,Object>();
public Object getValue(UserTableColumn column);
public void setValue(UserTableColumn column, Object value);
}
I hope this short explanation meets your needs.
Good luck !
Upvotes: 0
Reputation: 26737
you need "something" that generates the classes against your Database table.
Have a look at Hibernate
There are different ways to do that:
http://www.hibernate.org/subprojects/tools.html
http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables
Upvotes: 0
Reputation: 5102
You need this kind of database design, so you do not need to add new "columns" (class properties) or anything:
TEACHERS (Class called Teacher)
________
TeacherId
Name
STUDENTS (Class called Student)
________
StudentId
Name
ASSIGNMENTS (Class called Assignment)
___________
AssignmentId
TeacherId (REF)
Name
GRADES (Class called Grade)
______
AssignmentId (REF)
StudentId (REF)
Grade
You should be able to translate to a Java class design using Map
s.
I am specifically giving DB design ("pseudocode") and not Java code since this is homework.
FYI: REF stands for reference (foreign key in DB language).
Upvotes: 2
Reputation: 32407
If you want to add and remove columns to a database while it's running, that usually indicates your design is wrong. Your 'columns' should probably correspond to rows in a single table.
Upvotes: 1