J2EE development practices based on JBoss in Win2K environment ----- 2, database connection pool configuration and test

zhaozj2021-02-16  54

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: mysql JDBC: mysql: //10.0.0.18: 3306 / Test org.gjt.mm.mysql.driver root < Password> 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> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312> <% @ page import =" javax.sql.datasource "% > <% @ page import = "javax.naming.initialcontext"%> <% @ page import = "java.sql. *"%> <body> <h3> 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") "<br>");} 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.</p> <p>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 "?> <Datasource> <loCal-TX-Datasource> <jndi-name> mssql </ jndi-name> <connection-url> jdbc: Microsoft: SQLServer: //10.0.0.18: 1433; DatabaseName = TEST </ connection-url> <driver-class> com.microsoft.jdbc.sqlser.sqlserverDriver </ driver-class> <user-name> sa </ user-name> <password> </ 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24527.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="24527" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '5yqQHUNr6oRkyrwtmpjqePqXHqHGP6bvFv0FC7ZW0mqd292HXnIu_2FsMffj0hzbwml0qQjEvCxf_2Fasu_2FlPigAbg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>