是否可以从库 jar 连接到数据库
我有一个问题陈述。我正在尝试创建一个库 jar,它将帮助任何人从数据生成报告。我正在为此创建一个 springboot java jar。
输入:SQL 语句和报告类型(CSV、PDF 等)
现在的问题是我希望该实用程序由多个项目/应用程序使用。但问题是我不知道如何连接到使用该库的项目或应用程序的数据库。我无法提供所有数据源,因为这对我来说是不可能的。可能有数百个应用程序可以使用这个实用程序 jar,所以我无法在我的 jar 中创建那么多数据库连接,而且如果将来某些数据源发生变化怎么办?是否有任何通用方法可以用来实现相同的目标或者是否可以以某种方式构建 jar,以选择并连接正在使用它的应用程序或项目的数据源。
PS 有没有什么工具可以帮助我解决同样的问题陈述,即传递 sql,它会为我连接到数据库并获取结果。
I have a problem statement. I am trying to create a library jar which will help anyone to generate report from data. I am creating a springboot java jar for same.
Inputs : SQL statement and Type of report (CSV,PDF etc)
Now the problem is I want this utility to be used by multiple projects/Applications. But the problem is I am not aware how I can connect to database of the project or application which is using this library. I can not provide all the datasources b'cos that is not possible for me. There can be 100s of application which can be using this utility jar so no way i can create that many database connection in my jar and also what if some of the datasource changes in future.Is there any generic way which I can use to achieve same or is this possible somehow to build jar in a way which will pick and connect the datasource of the application or project which is using it.
P.S. are there any tools which i can use to help me out with the same problem statement i.e. pass the sql and it will connect to database for me and get me the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以通过将所有配置放入每个项目中的文件中来实现这一目标。您的JAR文件将使用该文件,并根据该项目配置连接到相对数据库。
您的配置文件即;命名db_connect.properties在您使用的每个项目中都应相同。在此文件中,应该有键和值,jar将使用它们连接到该特定数据库。
在这里,我将显示我的数据库。我的自定义罐子使用了一些额外的功能,您可以忽略它们。
Yes you can achieve this by putting all your configurations in a file in every project. Your jar file will use that file and connect to the relative database according to that project configurations.
Your Configuration file i.e; named db_connect.properties should be same in every project you are using. Inside this file there should be keys and values, which will be used by the jar to connect to that particular database.
Here i am showing my database.properties file for a project which is used by a jar. My custom jar is using some extra features, you can ignore them.