简体   繁体   English

创建数据库连接类java

[英]Creating a database connection class java

I am in process to create a Database connection class but having some doubts about implementation. 我正在创建一个数据库连接类,但对实现有一些疑问。 Idea is to create a flexible and easy to use connection manager class and since it will be used in our own application at some specific points so at the moment not much concerned about multi-threading etc. 想法是创建一个灵活且易于使用的连接管理器类,因为它将在我们自己的应用程序中的某些特定点使用,所以目前并不太关心多线程等。

here are the few requirements 这里有一些要求

  1. Configuring generic properties using a property file. 使用属性文件配置通用属性。
  2. handling the dialect for different providers. 为不同的提供者处理方言。

Please suggest some guidelines to make this class flexible and easy to enhance.Any link/resource of any good such implementation will be really helpful. 请建议一些指导方针,使这个课程灵活,易于增强。任何良好的这种实现的任何链接/资源将是非常有帮助的。

I assume, you will be using JDBC. 我假设你将使用JDBC。 You can externalize all parameter that are required to create Connection object for particular database 您可以外部化为特定数据库创建Connection对象所需的所有参数

1) Database connection URL (include port number) 2) Username 3) Password 4) Database driver 1)数据库连接URL(包括端口号)2)用户名3)密码4)数据库驱动程序

You can keep all these parameters in external properties file. 您可以将所有这些参数保留在外部属性文件中。 You can use factory pattern (say DatabaseConnectionFactory) to create database connection so that if you decide to apply certain attributes while creating new instances of connection or reusing the connection instances, it will be easy to manage. 您可以使用工厂模式(比如DatabaseConnectionFactory)来创建数据库连接,这样,如果您决定在创建新的连接实例或重用连接实例时应用某些属性,那么它将很容易管理。 You should make DatabaseConnectionFactory singleton. 您应该使DatabaseConnectionFactory单例。

What you need to take care of is the generation of SQL queries as it varies depending upon the Databases. 您需要注意的是SQL查询的生成,因为它根据数据库而有所不同。

Use JPA then. 然后使用JPA。 You will thank me for choosing that in a few month and you will set this up as fast as if you try to do it yourself. 你会感谢我在几个月内选择它,你会尽可能快地设置它,就像你自己尝试一样。

If all you're asking for is relating to concerns about design then you might want to go read up on some common design patterns and choose one that is best for your situation. 如果你要求的只是关于设计的问题那么你可能想要阅读一些常见的设计模式,并选择一个最适合你的情况。

Design Patterns 设计模式

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

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