简体   繁体   中英

How to use datasources in Intellij IDEA

Hello just starting to learn JAVA and I am trying to create a web application. So far I have managed to get the Tomcat server working and connect my Intellij IDE to my SQL sever instance creating a datasource.

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. 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)? 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.

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.

The database in question is created using Microsft SQL server and the driver used is jdbc.

IDEA is just an 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). Instead, have a look at the Properties class which provides pretty much what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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