Java development: using JNDI for database connection and resource management
In Java development, JNDI (Java Naming and Directory Interface) is a standard API. For managing naming and directory services. It can be used not only to access naming services, but also to connect to databases, manage resources, etc. This article will introduce how to use JNDI for database connection and resource management, and provide detailed code examples.
<resource-ref> <description>Database Connection</description> <res-ref-name>jdbc/myDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
Here, we define a JNDI data source named jdbc/myDB
and specify its type as javax.sql .DataSource
.
context.xml
file): <Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/myDB" username="root" password="password" maxTotal="100" maxIdle="30" maxWaitMillis="10000" />
Here, we configure a file named jdbc/myDB
The data source specifies the connection URL, user name, password and other information, as well as some configurations of the connection pool.
// 创建JNDI上下文 Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); // 获取数据源 DataSource ds = (DataSource) envCtx.lookup("jdbc/myDB"); // 获取数据库连接 Connection conn = ds.getConnection();
Here, we first create the initial context of JNDI initCtx
, and then obtain the environment context envCtx
through this context. Next, we obtained the previously configured data source jdbc/myDB
from the environment context. Finally, obtain the database connection through the data source.
try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("User: " + id + ", " + name); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); }
Through the above steps, we can use JNDI for database connection and resource management. The advantage of using JNDI is that database connection information can be centrally managed in the configuration file without hard-coding connection parameters in the code, making the code more concise and maintainable.
Summary:
This article introduces how to use JNDI for database connection and resource management. By configuring JNDI data sources, server configuration data sources, obtaining database connections, and using and closing connection sample codes, we can easily use JNDI to manage database connections and resources in Java development. I hope this article can be helpful to developers who are learning and using JNDI.
The above is the detailed content of Java development: How to use JNDI for database connection and resource management. For more information, please follow other related articles on the PHP Chinese website!