Access databases and implementation by web.xml

xiaoxiao2021-03-06  82

Access databases and implementation by web.xml

[Keyword] web.xml [author] lyilei [date] 2004-5-18

At the beginning of the project, many problems in the project from the ASP to JSP, due to their own levels and capabilities, and there are many problems in the project. One of the most serious questions is to access the parameter configuration of the database.

Due to the time limit of the completion of the project, the starting database connection string is solved by writing a fixed string, that is, hardcodes. The situation is roughly: Write a JavaBean to encapsulate the database, and then write the hard-coded string of the access database in this bean object, then other parts use the bean to access the database.

The code is approximately as follows: ---------------------------------------------- -------- String strClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String strConnUrl = "jdbc: microsoft: sqlserver: //192.168.2.203: 1433; DatabaseName = herovod30"; String strUserName = " SA "; string struserpass =" manager "; class.forname (strclassname); conn = drivermanager.getConnection (StrConnurl, strusername, struserpass);

Later, when the project was basically completed, because our projects need to be packaged into a product, there is a problem with an installer. This hardcodes have to be changed, and the same principle is the least change, the project leader chooses the use of the configuration file to solve the database parameter configuration. Place the various configuration information of the database into the text profile ini, then read configuration information from the configuration file when accessing the system. This method seems to be a better method, but if the current path information is not delivered, the system cannot get the configuration file from the virtual directory and obtain the file from it.

Recently, by checking the information written by others and books about JSP, the Web.xml release descriptor is found to place these configuration information. Because it corresponds to each virtual directory, the information is automatically loaded when the server loads each virtual path information. This will achieve the purpose of accessing the database by it. The specific implementation process is made below.

[Implementation] Use a bean object init to read information about the database configuration parameters. After reading, place these values ​​to global information, convenient for other places. Use the database to access the DBCONN object to open and close the connection of the database, it is a base class. All classes that need to process database data are derived from dbconn, and then can be handled anywhere. [Realization Process] 1. Web.xml file content:

Web.xml ----------------------------------------------- --------------

dbdriverclassname Oracle.jdbc.driver.OrgLEDriver

dbdrivername jdbc: Oracle: Thin: @ 192.168.2.204: 1521: VOD30

dbusername system

dbuserpassword manager Hello hello Hello / hello

Hello / servlet / *

2. The generated by theinit class

Initinit.java ----------------------------------------------- ------- / **** * Get the initialization parameter variable of the system from the web.xml document, where the initialization parameter of the database is obtained. * @Author: liu yilei * @date: 2004-5-18 * @version: 1.00 * / package init;

Import java.io. *; import javax.servlet. *; import javax.servlet.http. *;

Public class init {public void getApplicationvar (servletconfig config) {string dbconnclass = null; string dbconndriver = null; string dbconnusername = null; string dbconnuserpassword = null;

ServletContext context = config.getServletContext ();

dbConnClass = (String) context.getAttribute ( "dbConnClass"; dbConnDriver = (String) context.getAttribute ( "dbConnDriver"; dbConnUserName = (String) context.getAttribute ( "dbConnUserName"; dbConnUserPassword = (String) context.getAttribute ( "dbConnUserPassword "; if (dbConnClass == null || dbConnDriver == null || dbConnUserName == null || dbConnUserPassword == null) {dbConnClass = context.getInitParameter (" dbDriverClassName "; dbConnDriver = context.getInitParameter (" dbDriverName "; dbConnUserName = context.getInitParameter ( "dbUserName"; dbConnUserPassword = context.getInitParameter ( "dbUserPassword"; context.setAttribute ( "dbConnClass", dbConnClass); context.setAttribute ( "dbConnDriver", dbConnDriver); context.setAttribute ( "dbConnUserName", dbConnUserName) Context.setttribute ("dbconnuse Rpassword ", dbconnuserpassword);}}}

3.Dbconn.java design

DBCONN / DBCONN.JAVA --------------------------------------------- ---------------- / **** * Access the database's universal class library file, including the general method of importing the database driver and accessing the database. * @Author: liu yilei * @date: 2004-5-14 * @version: 1.00 * / package dbconn;

Import java.io. *; import java.sql. *; import javax.servlet. *; import javax.servlet.http. *;

Public class dbconn {private string dbconnclassname = null; private string dbconndriver = null; private string dbusername = null; private string dbuserpassword = null;

Connection conn = null; statement stmt = null; resultset = NULL;

Public dbconn () {

}

public void openDbConn () {try {Class.forName (dbConnClassName);} catch (ClassNotFoundException e) {System.err.println ( "dbConn.openDbConn () register database driver:" dbConnClassName e.getMessage ());}

try {conn = DriverManager.getConnection (dbConnDriver, dbUserName, dbUserPassword);} catch (SQLException e) {System.err.println ( "dbConn.openDbConn () establishing data connections:" dbConnDriver e.getMessage ());} }

Public void closedbconn () {if (conn! = null) {Try {conn.close ();} catch (sqlexception e) {system.err.println ("dbconn.closedbconn () off data connection:" E.getMessage ());} Finally {conn = null;}}} / ** * Set General Information * / Public Void SetDbConnclassName (String DbconnclassName) {this.dbconnclassname = dbconnclassname;}

Public string getdbconnclassname () {return dbconnclassname;}

Public void setdbconndriver (string dbconndriver) {this.dbconndriver = dbconndriver;}

Public string getdbconndriver () {return dbconndriver;}

Public void setdbusername (String dbusername) {this.dbusername = dbusername;

Public string getdbusername () {return dbusername;

Public void setdbuserpassword (String dbuserpassword) {this.dbuserpassword = dbuserpassword;}

Public string getDbuserpassword () {return dbuserpassword;}};

4. Other access and operational databases

DBCONN / PROG.JAVA --------------------------------------------- -------------- Package Dbconn;

import java.io. *; import java.sql *;. import javax.servlet *;. import javax.servlet.http *;. public class prog extends dbConn {public String getStrProgInfo () {String strReturn = null; String sql = "SELECT SG_NAME from Sys_Group";

OpenDbconn ();

Try {stmt = conn.createstatement (); rs = stmt.executeQuery (SQL);

IF (rs.next ()) {strreturn = rs.getstring (1);} rs.close (); stmt.close ();} catch (exception e) {system.rr.println ("prog.getstroginfo () : " E.getMessage ());

Closedbconn (); RETURN STRETURN;}};

5. Specific use and operation methods:

Index.jsp ----------------------------------- -------- <% @ page language = "java"%> <% @ page contenttype = "text / html; charSet = GB2312"%>

<% init.getApplicationvar (config);%>

<% prog.setDbConnClassName ((String) application.getAttribute ( "dbConnClass"); prog.setDbConnDriver ((String) application.getAttribute ( "dbConnDriver"); prog.setDbUserName ((String) application.getAttribute ( "dbConnUserName"); prog.setDbUserPassword ((String) application.getAttribute ( "dbConnUserPassword"); String retn = prog.getStrProgInfo (); out.println (RETN);%> [Note] You can place the database to configure the parameter package to achieve the purpose of the package, that is, put the configuration information into a bean object, then place the object to Application. in.

转载请注明原文地址:https://www.9cbs.com/read-95173.html

New Post(0)