简体   繁体   English

如何在Intellij IDEA中使用数据源

[英]How to use datasources in Intellij IDEA

Hello just starting to learn JAVA and I am trying to create a web application. 您好,刚开始学习JAVA,并且我正在尝试创建一个Web应用程序。 So far I have managed to get the Tomcat server working and connect my Intellij IDE to my SQL sever instance creating a datasource. 到目前为止,我已经设法使Tomcat服务器正常工作,并将Intellij IDE连接到创建数据源的SQL Server实例。

My question is how to properly access this datasource in code. 我的问题是如何在代码中正确访问此数据源。 I want o create a method that gets the Connection object from the datasource. 我想创建一个从数据源获取Connection对象的方法。 However I am at loss when it comes how to do it. 但是,当涉及到如何做时,我感到茫然。

My basic idea is to create a class containing the Connection GetConncetion() method, that returns the connection object, this should be achieved by calling the Datasource.getConnection(), but how do I make the datasource in question the one that is created by my IDE (Intellij IDEA)? 我的基本想法是创建一个包含Connection GetConncetion()方法的类,该类返回连接对象,这应该通过调用Datasource.getConnection()来实现,但是如何使有问题的数据源成为由我的IDE(Intellij IDEA)? I have located the DataSources.local.xml with the datasource name so I would like use this information to access it, however I dont know how to do it. 我使用数据源名称定位了DataSources.local.xml,因此我想使用此信息来访问它,但是我不知道该怎么做。

I have quite a clear idea how to do this using DriverManager, however I would like to learn how to use this more convinient datasource method. 我非常清楚如何使用DriverManager进行此操作,但是我想学习如何使用这种更方便的数据源方法。

The database in question is created using Microsft SQL server and the driver used is jdbc. 所涉及的数据库是使用Microsft SQL Server创建的,并且使用的驱动程序是jdbc。

IDEA is just an IDE. IDEA只是一个IDE。 Your application shouldn't be IDE or editor dependent, and IDEA therefore doesn't give you such facilities (and Java doesn't depend on them). 您的应用程序不应依赖于IDE或编辑器,因此IDEA不会为您提供此类功能(Java不依赖它们)。 Instead, have a look at the Properties class which provides pretty much what you want. 相反,请看一下提供几乎所需内容的Properties类。

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

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