JSP JSP JSP in JBuilder9 + WebLogic7 uses JDBC

zhaozj2021-02-16  52

JBUILDER9 WebLogic7 actual articles

JSP calls JDBC articles through JavaBean

Author: Kay Huang

E_mail: hk_sz@163.com

Application of JSP JavaBean JDBC Oracle

First, start JBUILDER

Start JBuilder: Start / Program / Borland JBuilder 9 Enterprise Trial / Borland JBuilder 9 Enterprise Trial

Before operating the following steps, check if your configuration and parameters are consistent with the tool articles of JBuilder9 WebLogic7 actual combat (Oracle9i), "JBuilder9 WebLogic7 actual combat tool (JDBC 1)".

There are no detailed description of the part if you don't understand, please refer to "JSP servlet articles".

Second, create a project

Create a project called Search.

Third, set the Web Application

Create a Web Application called Search.

Fourth, create a JSPS

4.1 Creating a search.jsp file;

4.2 Replacing all code in the Code Area with the following code:

<% @ page contenttype = "text / html; charset = GBK"

IMPORT = "java.util. *" import = "java.sql.resultset" import = "search.dbbean"%>

</p> <p>Search</p> <p></ title></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff"></p> <p><center></p> <p><h1></p> <p>JSP -> Javabean -> JDBC -> Oracle</p> <p></ h1></p> <p></ center></p> <p><%</p> <p>Dbbean conn = new dbbean ();</p> <p>ResultSet RS = NULL;</p> <p>RS = conn.executeQuery ("Select * from count");</p> <p>ArrayList Validates = New ArrayList ();</p> <p>While (rs.next ())</p> <p>{</p> <p>String Num1 = rs.getstring ("Num1"). Trim ();</p> <p>String Num2 = rs.getstring ("Num2"). Trim ();</p> <p>String result = rs.getstring ("result"). Trim ();</p> <p>%></p> <p><table width = "100%" cellspacing = "0" Cellpadding = "5" align = "center"></p> <p><tr></p> <p><TD> <b> Num1 </ b> </ td></p> <p><TD> <b> Num2 </ b> </ td></p> <p><TD> <b> Result </ b> </ td></p> <p></ TR></p> <p><tr></p> <p><TD> <% = Num1%> </ td></p> <p><TD> <% = Num2%> </ td> <td> <% = result%> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><%</p> <p>}</p> <p>Conn.closeConnection ();</p> <p>%></p> <p></ body></p> <p></ html></p> <p>V. Create Javabean</p> <p>5.1 Select File / New ... in the menu bar, pop up the Object Gallery window;</p> <p>5.2 Click the General tab;</p> <p>5.3 Select JavaBean and then click OK to show the JavaBean wizard page;</p> <p>5.4 Enter dbbean after Class Name; click OK to complete.</p> <p>5.5 Replace all of the code in the code area:</p> <p>Package Search;</p> <p>Import java.io. *;</p> <p>Import java.sql. *;</p> <p>Import java.util. *;</p> <p>Import javax.naming. *;</p> <p>Import javax.sql. *;</p> <p>Public Class Dbbean</p> <p>Implements Serializable</p> <p>{</p> <p>DataSource DS = NULL;</p> <p>CONTEXT CTX = NULL;</p> <p>Connection conn = NULL;</p> <p>Statement Stmt = NULL;</p> <p>ResultSet RS = NULL;</p> <p>Public void getConnection ()</p> <p>{</p> <p>Try</p> <p>{</p> <p>// Get the initial context information of WebLogic Server JBDI</p> <p>CTX = GetInitialContext ();</p> <p>System.out.println (ctx.tostring ());</p> <p>/ / Find the JNDI name as the data source of JDBC / TestTxDataSource, establish a data source object</p> <p>DS = (javax.sql.datasource) CTX.lookup ("JDBC / TesttxDataSource");</p> <p>System.out.println (ds.toString ());</p> <p>/ / Establish a data connection via a data source object</p> <p>CONN = ds.getConnection ();</p> <p>System.out.println (Conn.toToString ());</p> <p>/ / Establish a statement object</p> <p>STMT = conn.createstatement ();</p> <p>}</p> <p>Catch (java.sql.sqlexception es)</p> <p>{</p> <p>System.out.println ("MySQLException" es.getMessage ());</p> <p>}</p> <p>Catch (Exception EX)</p> <p>{</p> <p>System.out.println ("Conn IS Error" EX.GETMESSAGE ());</p> <p>}</p> <p>}</p> <p>Public void closeconnection ()</p> <p>{</p> <p>Try</p> <p>{</p> <p>IF (rs! = null)</p> <p>// Close the result set</p> <p>Rs.close ();</p> <p>IF (Stmt! = NULL)</p> <p>// Turn off the statement object</p> <p>Stmt.close ();</p> <p>IF (CONN! = NULL)</p> <p>// Close connection</p> <p>CONN.CLOSE ();</p> <p>}</p> <p>Catch (java.sql.sqlexception e)</p> <p>{</p> <p>System.out.println ("MyCloseConnectioinsql" E.GetMessage ());</p> <p>RS = NULL;</p> <p>STMT = NULL;</p> <p>CONN = NULL;</p> <p>}</p> <p>}</p> <p>Public ResultSet ExecuteQuery (String SQL) {</p> <p>RS = NULL;</p> <p>Try</p> <p>{</p> <p>IF (conn == null)</p> <p>GetConnection ();</p> <p>IF (CONN! = NULL)</p> <p>STMT = conn.createstatement ();</p> <p>RS = stmt.executequery (SQL);</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>System.out.println ("ExecuteQuery Error Here:" E.getMessage ());</p> <p>}</p> <p>Return RS;</p> <p>}</p> <p>Public Boolean ExecuteUpdate (STRING SQL)</p> <p>{</p> <p>Boolean bupdate = false;</p> <p>Try</p> <p>{</p> <p>IF (conn == null)</p> <p>GetConnection ();</p> <p>IF (CONN! = NULL)</p> <p>{</p> <p>STMT = conn.createstatement ();</p> <p>Int rowcount = stmt.executeUpdate (SQL);</p> <p>IF (RowCount! = 0)</p> <p>BUPDATE = TRUE;</p> <p>}</p> <p>}</p> <p>Catch (SQLException EX)</p> <p>{</p> <p>System.err.Println ("Conn.executeUpdate" ex.getMessage ());</p> <p>}</p> <p>Return BUPDATE;</p> <p>}</p> <p>// getInitialContext () method is used to get the initial context environment of WebLogic Server for JNDI queries.</p> <p>Private static context getinitialcontext () THROWS Exception</p> <p>{</p> <p>// URL, USER, PASSWORD corresponds to WebLogic Server's address, port number, system user, and password</p> <p>String URL = "T3: // localhost: 7001";</p> <p>String User = "training";</p> <p>String password = "training";</p> <p>Properties Properties = NULL;</p> <p>Try</p> <p>{</p> <p>Properties = new property ();</p> <p>Properties.put (Context.Initial_Context_Factory,</p> <p>"WebLogic.jndi.wlinitialContextFactory");</p> <p>Properties.Put (Context.Provider_URL, URL);</p> <p>Properties.Put (Context.security_principal, user);</p> <p>Properties.put (Context.security_credentials, password);</p> <p>Return New InitialContext (Properties);</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>Throw e;</p> <p>}</p> <p>}</p> <p>/ * Bean test</p> <p>Public static void main (string args [])</p> <p>{</p> <p>ResultSet RS = NULL;</p> <p>Try</p> <p>{</p> <p>DBBean conndata = new dbbean ();</p> <p>// Establish a result set object</p> <p>RS = connData.executeQuery ("Select * from count");</p> <p>// Traverse the result set object, access each record, output data</p> <p>While (rs.next ())</p> <p>{</p> <p>String Num1 = rs.getstring ("Num1"). Trim (); string num2 = rs.getstring ("num 2"). Trim ();</p> <p>String result = rs.getstring ("result"). Trim ();</p> <p>System.out.Println ("Validate: / '" Num1 " " Num2 "=" </p> <p>Result "/" IS ");</p> <p>}</p> <p>}</p> <p>Catch (Exception E)</p> <p>{}</p> <p>} * /</p> <p>}</p> <p>After running, run the result.</p> <p>My article is the first Auro Forum (www.newer.com.cn/bbs) and programmers forum (www.9cbs.net), welcome to reprint, but please keep the author and the name of the revision, thank you.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-28154.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="28154" 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.043</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 = 'XIxn5oda9uE0YADC_2FzbJ9EtaduTpCN_2FAFI_2BNObNz_2F3IQV6yvewS0V1If2NIPDHrMzhJ3JVaTbAxirK5V'; 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>