如何有效地实现数据库功能? [英] How to implement database functionality effectively?

查看:79
本文介绍了如何有效地实现数据库功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用MySQL数据库的Java桌面应用程序。数据库有6个表。每个表,像往常一样,应该允许CRUD(创建,读取,更新和删除)操作。



我设计了6 * 4 = 24 JPanels,4 JPanels 。每个JPanel都有组件来接受用户输入并执行设计的CRUD操作。例如,JPanel3是为Table1的Create操作设计的。



现在我想知道以下内容:



Q1。最好编写24个函数,每个函数对一个特定的表执行一个特定的函数。



Q2。这种情况是非常正常的,因为每个应用程序通常都有很多表。那么,所有这些应用程序都使用这种方法为每个表的每个操作编写每个函数?



Q3。并且每个CRUD操作都需要数据库连接,所以最好在用户启动应用程序时连接到数据库。





在用户点击保存或编辑或删除或创建按钮时更好地进行数据库连接?



Q4。从所有24个函数共享的实例变量引用连接会更好吗?



还欢迎任何其他建议。

解决方案

A1。否。设计应遵循需求和领域逻辑,而不是技术考虑。通常,对于每个表,用户可以单独访问所有CRUD操作是没有意义的。



A2。现在,大多数应用程序使用对象/关系映射器,如 Hibernate 这种事情。但是,仍然应该有在顶部执行相关操作的应用程序逻辑。



A3 / 4。使用数据库连接池。 O / R映射器通常自动执行。


I am developing a Java Desktop Application which uses MySQL database. The DB has 6 tables. Every table, as usual, should allow CRUD (Create, Read, Update and Delete) operations.

I have designed 6*4 = 24 JPanels, 4 JPanels for each tables. Each JPanel have Components to take user input and perform the CRUD operation for which it is designed. For instance, a JPanel3 is designed for Create operation for Table1.

Now I want to know the following:

Q1. Would it be better to write 24 functions, each performing a specific function for a specific table?

Q2. This kind of situation is very normal as every application generally has many tables. So, Are all those applications use this approach of writing each function for each operation for each table?

Q3. As it is a Swing Application and every CRUD operation need the database connection, so Would it be better to make a connection to the database when the user starts the application?

or

Would it be better to make the database connection at the time when user clicks on the "save" or "edit" or "delete" or "create" button?

Q4. Would it be better to refer the connection from an instance variable which is shared by all the 24 functions? or Would it be better to have every function its own Connection?

Any other suggestions are also welcomed.

解决方案

A1. No. The design should be guided by the requirements and the domain logic, not by technical considerations. Usually, it does not make sense to have all CRUD operations separately accessible to the user for each and every table. Write functions to perform those operations together that belong together.

A2. No. Nowadays, most applications use an Object/Relational mapper like Hibernate for this kind of thing. But still, there should be application logic on top that executes related operations together.

A3/4. Use a DB connection pool. O/R mappers generally do that automatically.

这篇关于如何有效地实现数据库功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆