1. Overview of JavaBean
1. Javabean is a reusable component written in Java. To some extent, it is a special Java class conforming to some naming methods or design specifications for complex computing.
2. Common attributes of JavaBean are as follows:
1) Simple attributes: A simple attribute is usually a variable with a pair of get/set methods, and the attribute name corresponds to the method name.
2) index property: the index property is similar to the simple property, but the returned value is the array value.
3) binding property: the binding property is used to notify the listener that the attributes of a JavaBean Component have changed. When the attributes change, a propertychange time is triggered to notify other objects.
4) Constraint attributes: the constraint attributes are similar to the bound attributes. However, changes in the attribute values will take effect only after being verified by the listener. That is, when a property value changes, A Java object that is connected to this property value can reject the changes to this property and throw an exception. The propertyvetoexception exception is used to reject the changes in the property value.
3. Three main features of JavaBean:
1) constructor without Parameters
2) Private attributes
3) GET/Set Method for operating attribute values
2. A simple JavaBean instance
JavaBean: studentinfobean
Public class studentinfobean <br/>{< br/>/* <br/> * student ID <br/> */<br/> Public int stuid; <br/>/* <br/> * Student name <br/> */<br/> Public String stuname; <br/>/* <br/> * contact number <br/> */<br/> Public String telephone; </P> <p>/* <br/> * set the student ID <br/> */<br/> Public void setstuid (int id) <br/>{< br/> This. stuid = ID; <br/>}</P> <p>/* <br/> * obtain the student ID <br/> */<br/> Public int getstuid () <br/>{< br/> return this. stuid; <br/>}</P> <p>/* <br/> * set the Student name <br/> */<br/> Public void setstuname (string name) <br/>{< br/> This. stuname = Name; <br/>}</P> <p>/* <br/> * obtain the Student name <br/> */<br/> Public String getstuname () <br/>{< br/> return this. stuname; <br/>}</P> <p>/* <br/> * set the contact number <br/> */<br/> Public void settelephone (string telephone) <br/>{< br/> This. telephone = telephone; <br/>}</P> <p>/* <br/> * contact number <br/> */<br/> Public String gettelephone () <br/>{< br/> return this. telephone; <br/>}< br/>}
Write the JSP page below
<% @ Page Language = "Java" pageencoding = "UTF-8" contenttype = "text/html; charset = UTF-8" %> <br/> <% @ page import = "Java. util. * "%> </P> <p> <JSP: usebean id =" student "Scope =" page "class =" com. jspkongfu. ch07.studentinfobean "/> <br/> <JSP: setproperty name =" student "property =" stuid "value =" 2008001 "/> <br/> <JSP: setproperty name = "student" property = "stuname" value = "文"/> <br/> <JSP: setproperty name =" student "property =" Telephone "value =" 13501010202 "/> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> </P> <p> <HTML> </P> <p> <pead> <br/> <title> <br/> Student Information <br/> </title> <br/> </pead> </P> <p> <body> <br /> <center> <br/> <JSP: getproperty name = "student" property = "stuname"/> students! Welcome! <Br> </P> <p> your basic information is as follows: <br> </P> <p> Student ID: <JSP: getproperty name = "student" property = "stuid"/> <br/> name: <JSP: getproperty name = "student" property = "stuname"/> <br/> Tel: <JSP: getproperty name = "student" property = "telephone"/> <br/> </center> <br/> </body> <br/> </ptml>
3. source code of a database processing JavaBean
// Introduce Java. SQL package <br/> Import Java. SQL. *; </P> <p> public class databasebean <br/> {<br/>/* <br/> * connection object <br/> */<br/> Public connection connection; </P> <p>/* <br/> * SQL statement <br/> */<br/> Public String sqlstr; </P> <p>/* <br/> * query condition <br/> */<br/> Public String Params []; </P> <p>/* <br/> * query result <br/> */<br/> Public resultset result; </P> <p>/* <br/> * set the connection <br/> */<br/> Public void setconnection (string drivername, String jdbcurl, string username, string password) throws exception <br/>{< br/> // defines a temporary connection object and stores the intermediate value <br/> connection tempconnection; </P> <p> // register the JDBC driver <br/> class. forname (drivername); <br/> tempconnection = drivermanager. getconnection (jdbcurl, username, password); <br/> tempconnection. setautocommit (false); </P> <p> This. connection = tempconnection; <br/>}</P> <p>/* <br/> * obtain a connection <br/> */<br/> Public Conn Ection getconnection () <br/>{< br/> return this. connection; <br/>}</P> <p>/* <br/> * set an SQL statement <br/> */<br/> Public void setsqlstr (string SQL) <br/>{< br/> This. sqlstr = SQL; <br/>}</P> <p>/* <br/> * obtain an SQL statement <br/> */<br/> Public String getsqlstr () <br/>{< br/> return this. sqlstr; <br/>}</P> <p>/* <br/> * Set query conditions <br/> */<br/> Public void setparams (string [] Param) <br/>{< br/> This. params = Param; <br/>}</P> <P>/* <br/> * obtain query conditions <br/> */<br/> Public String [] getparams () <br/>{< br/> return this. params; <br/>}</P> <p>/* <br/> * obtain the query result <br/> */<br/> Public resultset getresult () <br/>{< br/> try <br/> {<br/> preparedstatement PS = connection. preparestatement (sqlstr, Java. SQL. resultset. type_scroll_insensitive, Java. SQL. resultset. concur_read_only); </P> <p> If (Params! = NULL) <br/>{< br/> for (INT I = 0; I <Params. length; I ++) <br/>{< br/> ps. setstring (I + 1, Params [I]); <br/>}</P> <p> // query, return the result <br/> result = ps.exe cutequery (); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (Ex); <br/>}</P> <p> return result; <br/>}</P> <p>/* <br/> * Create a new table <br/> */<br/> Public void createtable () throws sqlexception <br/> {<br/> try <br/> {<br/> preparedstatemen T PS = connection. preparestatement (sqlstr); <br/> // execute the create table operation <br/> ps.exe cuteupdate (); </P> <p> // close the connection <br/> ps. close (); <br/> connection. commit (); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (Ex); </P> <p> // if the transaction fails, roll back <br/> connection. rollback (); <br/>}</P> <p>/* <br/> * add data records <br/> */<br/> Public void insertdata () throws sqlexception <br/>{< br/> try <br/>{< br/> preparedstatement PS = Connection. preparestatement (sqlstr); </P> <p> If (Params! = NULL) <br/>{< br/> for (INT I = 0; I <Params. length; I ++) <br/>{< br/> ps. setstring (I + 1, Params [I]); <br/>}</P> <p> // perform the add data operation <br/> ps.exe cuteupdate (); </P> <p> // close the connection <br/> ps. close (); <br/> connection. commit (); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (Ex); </P> <p> // if the transaction fails, roll back <br/> connection. rollback (); <br/>}</P> <p>/* <br/> * update data <br/> */<br/> Public void updatedat A () throws sqlexception <br/>{< br/> try <br/>{< br/> preparedstatement PS = connection. preparestatement (sqlstr); </P> <p> If (Params! = NULL) <br/>{< br/> for (INT I = 0; I <Params. length; I ++) <br/>{< br/> ps. setstring (I + 1, Params [I]); <br/>}</P> <p> // perform the data update operation <br/> ps.exe cuteupdate (); </P> <p> // close the connection <br/> ps. close (); <br/> connection. commit (); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (Ex); </P> <p> // if the transaction fails, roll back <br/> connection. rollback (); <br/>}</P> <p>/* <br/> * delete data <br/> */<br/> Public void deletedat A () throws sqlexception <br/>{< br/> try <br/>{< br/> preparedstatement PS = connection. preparestatement (sqlstr); </P> <p> If (Params! = NULL) <br/>{< br/> for (INT I = 0; I <Params. length; I ++) <br/>{< br/> ps. setstring (I + 1, Params [I]); <br/>}</P> <p> // perform the data deletion operation <br/> ps.exe cuteupdate (); </P> <p> // close the connection <br/> ps. close (); <br/> connection. commit (); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (Ex); </P> <p> // if the transaction fails, roll back <br/> connection. rollback (); <br/>}< br/>}