Based on the JBoss Development Practice in the Win2K environment, the configuration and testing of the database connection pool: This section is written on the previous section, in the previous section we configure the JBoss server and test it. Let's take a look at how to use JSP and Servlet to connect to the database in JBoss, and use its connection pool, you must see this section after reading the next section. First, configure the mysql database 1, install the mysql database, this is very simple, directly on www.mysql.org up and down, then install it directly, if I just install mySQL in the c: / mysql directory. Set the user to root, the password is empty. 2. Create a database in MySQL, name: Test; create a table in this library, table name Tree. Use the script as follows: Create Database Test; Create Table Tree (NodeId Int (11) Not Null, NodeText Varchar (60), Primary Key (`NodeId`) 3, then insert two data USE TEST; INSERT INTO Tree Values ('1', 'Test Data 1'); Insert Into Tree Values ('2', 'Test Data 2'); 4, download and install MySQL JDBC driver, go www.mysql.org download. The downloaded file name is: mysql-connector-jdbc-3.0.10-stable.zip, unwound with WinZip, find MySQL-Connector-Java-3.0 in the MySQL-Connector-JDBC-3.0.10-Stable directory. 10-stable-bin.jar file, copy it into the lib directory under the JBoss installation directory, namely, Copy to C: / JBoss / Server / All / Lib Directory (note that we use JBoss) It has three start-up methods with all functions, and JBoss has three start-up mode. It is started with Run -C , these three methods are: All, with all JBoss server features; Default, this is the default method; minimal, this is Minimal configuration. These three methods are respectively listened to ALL, DEFAULT and MINIMAL directories in the installation directory). At the same time, we have to pay attention to: In the JBoss installation directory, there is a lib directory, which is a C: / jboss / lib directory. This directory is stored in all JAR packages needed when the JBoss server is started. You can't Put your external JAR package in this directory. If you want to start the default, you need to put the JDBC driver into the C: / JBoss / Server / Default / lib directory.
5, configure the JBoss database configuration file: Newly build a mysql-ds.xml file in the C: / JBoss / Server / All / Deploy directory, note that the file name must be ended with - (中 线) DS, this XML file The content is as follows: XML Version = "1.0" encoding = "UTF-8"?> mysql jndi-name> JDBC: mysql: //10.0.0.18: 3306 / Test connection-url> org.gjt.mm.mysql.driver driver-class> root user-name> < Password> password> local-tx-dataword> datasources> Interpretation: Indicates the JNDI name, just find the JNDI name in the program (I still remember In the last section, we used the search EJB JNDI, that is, this is to find the database connection pool); is the connection string, the front is fixed back, keeping your database is located. IP address and mysql default port, then follow the database name such as Test; is a fixed database drive class library; is the username used to connect the database, I use root here, You can use other to be the password of the above user, my root user is empty password. Be sure to pay attention to the name of this file and the location of the store is: c: /jboss/server/All/Deploy/mysql-ds.xml 6, write the JSP file for testing.
In the c: /jboss/myproject/jsp/hello.war directory in the previous section, create a TestMysql.jsp file, as follows: <% @ Page ContentType = "Text / HTML; Charset = GB2312"%>
Test mysql database title> <% @ page import = "javax.naming.initialcontext"%> <% @ page import = "java.sql. *"%> test mysql database h3> <% initialContext CTX = New InitialContext (); DataSource DS = (DataSource) CTX.lookup ("java: / mysql"); // Here you want to correspond to MySQL-DS.XML CONNECTION CONN = ds.getConnection (); statement stmt = conn.createstatement ); ResultSet RS = Stmt.executeQuery ("SELECT * from Tree"); while (rs.next ()) {Out.Println (rs.getstring ("nodetext") "
");} conn.close ();%> body> html> then copy the directory of this file Hello.wa to the C: / JBoss / Server / All / Deploy / Directory, if you are in the previous section Bes have this directory here, just copy the TestMysql.jsp file. 7. Test the JSP connection database to restart the JBoss server, run -c all, if your server is running before you copy the JDBC driver to the server / all / lib directory, you must have the server. Then enter in IE or other browser: http: // localhost: 8080 / hello / testmysql.jsp, see what it is, is there two lines of data? Yes, this is right. As for the method of using the database connection pool in servlet and EJB, I don't write this. Go to test it yourself. Second, configure the SQL Server2000 database to configure the SQL Server database, and use its connection pool process basically, I will write some short: the process is as follows: 1. Install a SQL Server 2000 database, or you have it in other It can also be on the computer. Set a user to connect it, I use SA here, password is empty.
2, run the SQL Server client, run the following script, generate databases and tables and insert data: Create Database test Go Create Table Tree (NodeId Int Primary Key, NodeText Varchar (60)) Go Insert Into Tree Values ('1', ' SQL Server Test Data 1 ') Go INSERT INTO TREE VALUES (' 2 ',' SQL Server Test Data 2 ') GO 3, download and install the JDBC driver for SQL Server, download address is: http://www.microsoft. COM / DOWNLOADS / DETAILS.ASPX? FamilyId = 86212d54-8488-481D-B46B-AF29BB18E1E5 & DIS PlayLANG = EN is a executable installation package, run it directly, default to C: / Program Files / Microsoft SQL Server 2000 Driver The for JDBC is below. 4, after installation, copy the three JAR files in the C: / Program Files / Microsoft SQL Server 2000 Driver for JDBC / lib directory (msbase.jar; mssqlser.jar; msutil.jar) to C: / JBoss / Server / ALL / LIB directory. 5. Configure JBoss to use the MSSQL2K configuration file, create a new file in the C: / JBoss / Server / All / Deploy directory named: MSSQL-DS.XML, its contents are as follows: XML Version = "1.0" encoding = " UTF-8 "?> mssql jndi-name> jdbc: Microsoft: SQLServer: //10.0.0.18: 1433; DatabaseName = TEST connection-url> com.microsoft.jdbc.sqlser.sqlserverDriver driver-class> sa user-name> password> local-tx -DataSource> datasources> The solution does not have to be, the contents of each content and MySQL above are almost the same. This file must be placed in a C: / jboss / server / all / deploy directory.