Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of connecting to the database in java web applications

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the methods for java web applications to connect to the database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

DriverManager

Get database connection

Then use statement

Preparedstatement

Resultset operates the database.

The drawback of this approach is that database names, user names and passwords need to be hard-coded in the code. Once these things change, the whole program needs to be recompiled.

2 javax.sql.* usage

DataSource, which uses connection pooling. For such resources in web applications, we can configure them in context.xml so that we only need to look up the table and use JNDI to obtain connection pooling connections. The database name, username and password only need to be changed by modifying the context.xml file.

Context.xml can be configured in 2 places

(1) tomcat server tomcat/conf/context.xml

Add directly to the file

Auth= "Container"

Type= "javax.sql.DataSource"

DriverClassName= "org.gjt.mm.mysql.Driver"

Url= "jdbc:mysql://localhost/testforjavajdbc"

Username= "root"

Password= "root"

MaxActive= "100"

MaxIdle= "30"

MaxWait= "10000" / >

The server loads resources when it is turned on, and the application uses JNDI

This is the end of the content of "what are the methods for java web applications to connect to the database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report