JSP + Javabean step-by-step tutorial (4)

xiaoxiao2021-03-06  55

JavaServer Pages JavaBeans' database Operation Application has already talked a simple JavaBean application counter example. Of course, in the actual procedure process, more or the database is related to the database, so we will focus on Javaserver in this section. How to operate the database Pages and Javabeans. Here we choose a relatively representative example, that is, user registration management, because this is frequently used online, whether it is registered Email, award-winning survey, purchase items or join communities, etc. will involve a user Registration questions; in another aspect, it is more representative, involving a record of records of the database, records common operations such as record display, so we will register the knife with the user. The program is developed in Oracle JDeveloper3.1, the operating environment is WiIN2000 Tomcat3.1, and the database system uses Oracle8.16i. First we create a database DEMODB, which is like Username Varchar2 (20) User name Password Varchar2 (20), the field, as shown in the following parameters varchar2 (20) Email Address HOMEPAGE VARCHAR2 (50) Home SIGNS VARCHAR2 (200) Signature Regtime Date Registration time We build several JavaBeans and JavaServer Pages file DB.java files (package database connections and some underlying operations) adduser.java files (readable user data reading and add operation) newuser.jsp file (user new page for input User Registration Information) DONEWUSER.JSP file (User Registration Information Add) listuser.jsp file (all registered user information lists) In order to facilitate everyone to see code, in many places have made detailed annotations and explanations, as for javabean involved The Java grammatical structure, please refer to Java books. DB.java file description: This JavaBean package database connection and some underlying operations, derived classes can call these methods directly, and provide a tochinese method, mainly used to process the Chinese data.

// copyright (c) 2000 http://jspbs.yeah.net package lyf; / ** * a class class. *

* @Author liuyufeng * // declared class library file import oracle.jdbc.driver. *; Import java.sql. *; Import java.lang. *; Import java.io. *; import java.util. *; Public class db {// member variable initialization connection conn = null ; // Database connection resultt rs = null; // Record set string username = ""; // User name String password = ""; // password string email = "; // email string homepage ="; // Home string signs = ""; // Sign // DB builder public db () {Try {// Register Database Driver to Oracle Drive Class.Forname (New Oracle.jdbc.driver.OracleDriver ());} catch (java.lang.classnotfoundexception e) {// This way is to facilitate debugging procedures, error printing mydb (), know where it is error, "mydb ():" E.getMessage ()) }}}}} // ExecuteQuery method The query operation // entry parameter for the record is the SQL statement, returns the ResultSet object public resultSet ExecuteQuery (String SQL) {RS = NULL; try {// Establish a database connection, using Oracle Thin connection method, demo is the host name, DEMODB is a database, the two DEMOs behind are user name and password conn = drivermanager.getConnection "JDBC: Oracle: Thin: @demo: 1521: Demodb", "Demo", "Demo"); statement stmt = conn.createstatement (); // Execute database query operation RS = stmt.executeQuery (SQL);} catch (SQLEXCEPTION EX) {System.err.Println ("DB.ExecuteQuery:" EX.getMessage ());} RETURN RS;} // ExecuteUpdate method is used to perform ADD or UPDATE record operation // entry parameter is SQL statement , Successfully returns true, otherwise false public boolean executeUpdate (String SQL) {boolean bupdate = false; rs = null; try {// set up a database connection, other parameters illustrate the same conn = drivermanager.getConnection

JDBC: Oracle: Thin: @Demo: 1521: DemoDB "," Demo "," Demo "); statement stmt = conn.createstatement (); int RowCount = stmt.executeUpdate (SQL); // If not success, bupdate Will return 0 if (RowCount! = 0) BUPDATE = true;} catch (sqlexception ex) {// Print error message system.err.println ("db.executeUpdate:" ex.getMessage ());} return bupdate; } // tochinese method is used to handle a string to Chinese processing / / otherwise it will be ??? Such PUBLIC STATIC STRING TOCHINESE (STRING STRING) {Try {if (Strvalue == Null) {Return null;} Else {strval = new string ("ISO8859_1"), "ISO8859_1"), "GBK"); Return Strvalue;}} Catch (Exception E) {Return Null;}}} addUser.java file description: Main user data read Take and add actions, derive from DB, addNewuser method to the addition of user data, checkuser () method is used to check if the user name is repeated, and some SET / GET methods are used to process attributes, dousernew.jsp The file will be used.

// copyright (c) 2000 http://jspbbs.yeah.net package lyf; / ** * a class class. *

* @Author Liuyufeng * // Import java class library import java.sql. *; Import java.lang. *; import oracle.jdbc.driver. *; // adduser is derived from DB, member variables and methods with DB, PUBLIC CLASS ADDUSER Extends DB {// Builder Public Boolean AddNewuser () {Boolean Boadduser = False; try {// Make user registration records to add operations, generate SQL statement string ssql = new string ("INSERT INTO User (RegTIME, HOMENAME, PASSWORD, Email, HomePage, Signs)"); SSQL = SSQL VALUES (sysdaye "" Username "," password "", " email " "," homepage "", " SIGNS " ")"; // a debug-in method, you can print SQL Statement to facilitate viewing error system.out.println (ssql); // call the EXECUTEUPDATE method of the parent class, and set the return value if (SUPER.EXECUTEUPDATE (SSQL)) BoadDUser = true;} catch (Exception) EX) {// error handling system.rr.println ("adduser.addnewuser:" ex. maxMessage ());} Finally {// Regardless of whether it is wrong, return value return bagduser;}} // checkuser () Method is used to check if the user name is repeated // If you repeat a false public boolean checkuser () {boolean bagduser = false; try {// Build SQL Query Statement String SSQL = "Select * from user Wher e username = "" username "" "; // calls the parent class's ExecuteQuery method if ((Super.executeQuery (SSQL)). Next ()) {// Query the record set is empty Boadduser = false;} else {BOADDUSER = True;}} Catch (Exception EX) {// error handling system.err.println ("adduser.addnewuser:" ex.getMessage ());} finally {// Return value return bagduser;}} / / Attribute SET / GET method, unanimous of the request / * Actually all the GET / SET methods below are repetitive labor, in order to avoid repetitive copy paste, I wrote a software JSP code Faster, as long as entering one The field name of the series, all GET / SET methods can be generated automatically,

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

New Post(0)