简体   繁体   English

根据表结构在运行时动态创建CRUD的表单

[英]Creating form for CRUD dynamically at run time depending upon the table structure

I am stuck at a problem and I have been googling around and read quite few resources and feel direction right now. 我陷入了一个问题,并且一直在谷歌上搜索,阅读了很少的资源,现在就感到了方向。

HERE is my problem. 这是我的问题。 I am working on an application which will act like CRUD application for a MySQL database with variable tables and structures, I cannot have fixed no. 我正在开发一个应用程序,该应用程序将像具有可变表和结构的MySQL数据库的CRUD应用程序一样工作,我无法固定编号。 of tables and structures. 表和结构。

I am directionless at creating form for variable table structure. 我无能为力地为可变表结构创建表格。 What i want to achieve is when a user selects the table name from combobox relevant no. 我要实现的是,当用户从组合框相关编号中选择表名称时。 of fields should be created after retrieving the table structure of that particular table. 应该在检索特定表的表结构之后创建字段的字段。

Achievement so far: -I have been able fetch structure/schema, data types, data of the table. 迄今为止的成就:-我已经能够获取结构/架构,数据类型,表数据。 -Can write queries to create, update, delete the same. -可以编写查询来创建,更新,删除相同的内容。

can't succeed in: -I want to create JTextField with unique object names of JTextField relevant to associated column name in the table. 无法成功:-我想用与表中关联列名称相关的JTextField唯一对象名称创建JTextField。 -Column names are String type how can I covert them to Object of JTextField. -列名是字符串类型,我如何将它们隐蔽到JTextField的对象中。

For example 例如

String f1 = "fieldname_1";  // In reality these string values will be fetched from database resultset containing column names as identifier.
String f2 = "fieldname_2";  // In reality these string values will be fetched from database resultset containing column names as identifier.
JTextField f1 = new JTextField ();

or 

String[] f = {"fieldname_1", "fieldname_2"}; 
JTextField f[0] = new JTextField ();

but it gives error such as f1 is already defined 但是它给出了错误,例如已经定义了f1

Note : I want to have input field names to be unique so that i will be easier to access there data after input. 注意:我希望输入字段名称是唯一的,以便在输入后可以更轻松地访问那里的数据。

Corrections to the question are welcome, If my approach to handle the problem is wrong give a brief idea/approach to pursue it in right direction. 欢迎对此问题进行更正,如果我处理该问题的方法有误,请提供简短的想法/方法以朝正确的方向进行。

Regards 问候

You can't create unique variable names based on the column name. 您不能基于列名创建唯一的变量名。

Instead you could create a HashMap to map the column name to the text field used in the form: 相反,您可以创建一个HashMap来将列名映射到表单中使用的文本字段:

HashMap<String, JTextField> textFields = new HashMap<String, JTextField>();

Then you have a method that adds the text field to the form for each column name. 然后,您可以使用一种方法将文本字段添加到表单中每个列名称。 Something like: 就像是:

provide void addTextField(JPanel form, String columnName)
{
    Jlabel label = new JLabel( columnName );
    form.add( label );
    JTextField textField = new JTextField(10);
    form.add( textField );
    textFields.put(columnName, textField);
}

Of course you would need to modify for the proper layout you want to use. 当然,您需要为要使用的正确布局进行修改。

Then when you want to access the text field for the given column you can use: 然后,当您要访问给定列的文本字段时,可以使用:

JTextField textField = textFields.get(columnName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM