Use JSP to access databases with JavaBeans, JDBC-ODBC (transfer)

xiaoxiao2021-03-06  61

JSP (JavaServer Pages) is advocated by Sun Microsystems, a dynamic web technology standard for many companies involved. With JSP technology, web page developers can use HTML or XML identity to design and format the final page. Use the JSP ID (TAG) or a Scriptle (Scriptlet) to generate dynamic content on the page. The logic of generating content is packaged in the identification and javabeans components, and bundled in a small script, all scripts run on the server.

JavaBeans is a Java-based software component, JavaBeans, and ActiveX controls, which can create a complete set of reusable object libraries through package business logic. JSP provides perfect support for integrating JavaBeans components in web applications, which not only shorten the development time (you can directly utilize test and trusted existing components), avoid duplication, but also bring more JSP applications. Multi-scalability. The JavaBeans component can be used to perform complex computing tasks or is responsible for interacting with the database, such as data extraction, etc., this article will be introduced in JSP to access customer information databases through JDBC-ODBC Bridge through JDBC-ODBC Bridge through an instance.

Database connection is the most important part of the dynamic website. The technology connecting the database in Java is JDBC (Java Database Connectivity). Many database systems have a JDBC driver, and the Java program is connected to the database through the JDBC driver, performing queries, extracting data, etc. Sun has also developed JDBC-ODBC Bridge. With this technology Java program, you can access databases with ODBC drivers. Most database systems have an ODBC driver, so Java programs can access such as Oracle, Sybase, MS SQL. Server and MS Access and other databases. The following describes how to implement a customer information query program with Access.

1. First create an Access database customers.mdb, where the table Customers has field ID (automatic increment, and set-to-primary keyword), name (text, length 10), address (text, length 30), INFO (Remarks).

2. Add the System DSN to the ODBC DataSource module of Control Panel, name Customers, and point to Customers.mdb.

3. Create a JavaBean named dbconn.java and save the default document root directory of the web server that supports JSP. Dbconn.java is mainly the connection operation of the package and the database, as follows:

Import java.sql. *;

Public clas dbconn {

StringDBDriver = "sun.jdbc.odbc.jdbcodbcdriver";

String connStr = "JDBC: ODBC: CUSTOMERS";

Connection conn = NULL;

ResultSet RS = NULL;

PUBLIC DBCONN {

Try {

Class.Forname (dbdriver);

// Load the database driver

}

Catch (java.lang.classnotfoundexception e) {

System.err.println ("DBCONN ():" E.GetMessage ());

}

}

Public ResultSet ExecuteQuery (String SQL) {RS = NULL;

Try {

Conn = drivermanager.getconnection (connStr);

/ / Establish a link with DBMS

Statement Stmt = conn.createstatement ();

RS = stmt.executequery (SQL);

}

Catch (Sqlexception EX) {

System.err.println ("Aq.executeQuery:" ex.getMessage ());

}

Return RS;

}

}

4, DBCONN.JAVA has been edited, in the DOS state, compile DBCONN.JAVA to form the corresponding class file with JDK's Javac command.

5. Create a Customers.jsp file, call the above-compiled JavaBeans in JSP, as follows:

"text / html; charSet = GB2312">

Customer Information Survey </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> <b> Customer Information Investigation </ B> </ P></p> <p><% @ Page language = "java" import = "java.sql. *"%></p> <p><jsp: usebean id = "dbconn1" scope =</p> <p>"Page" Class = "dbconn" /></p> <p><%</p> <p>ResultSet RS = DBCONN1.EXECUTEQUERY</p> <p>("SELECT * from customers");</p> <p>While (rs.next ()) {</p> <p>OUT.PRINT ("<li>" rs.getstring ("name") "</ li>");</p> <p>Out.print ("<li>" rs.getstring ("address") "</ li>");</p> <p>Out.print ("<li>" rs.getstring ("info") "</ li>");</p> <p>}</p> <p>Rs.close ();</p> <p>%></p> <p></ body></p> <p></ html></p> <p>Several properties are defined within the <JSP: UseBean> tag, where the ID is an identifier of the BEAN within the entire JSP page, and the Scope property defines the living time of the BEAN, and the class attribute illustrates the bean class file.</p> <p>It turns out that JSP is an ideal architecture for developing web applications, using JavaBeans components that run across platforms, JSP provides excellent solutions for separation processing logic and display style.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-118765.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="118765" 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 = 'kkKBTJdHhmt6MEasCONjZ3ARbe5psvOHL_2BynG1UwUBvnJwsJ2HTq8s1A1W6ATLXdBPdsVcP0tw8W49GjPetRlw_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>