⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<PRE CLASS="CODE"><A NAME="pgfId-1087880"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087881"></A>  public RegistrationPK ejbCreate(String theuser, String password, </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087882"></A>                                  String emailaddress, String creditcard) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087883"></A>                                  throws CreateException, RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087884"></A>    System.out.println(&quot;registration create&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087885"></A>    this.theuser=theuser;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087886"></A>    this.password=password;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087887"></A>    this.emailaddress=emailaddress;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087888"></A>    this.creditcard=creditcard;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087889"></A>    this.balance=0;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087890"></A>    Connection con = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087891"></A>    PreparedStatement ps = null;;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087892"></A>    try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087893"></A>      con=getConnection();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087894"></A>      ps=con.prepareStatement(&quot;insert into registration (theuser, </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087895"></A>      password, emailaddress, creditcard, balance) values (?, ?, ?, ?, ?)&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087896"></A>      ps.setString(1, theuser);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087897"></A>      ps.setString(2, password);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087898"></A>      ps.setString(3, emailaddress);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087899"></A>      ps.setString(4, creditcard);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087900"></A>      ps.setDouble(5, balance);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087901"></A>      if(ps.executeUpdate() != 1) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087902"></A>         System.out.println(&quot;registration create failed&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087903"></A>         throw new CreateException (&quot;JDBC did not create any row&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087904"></A>       }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087905"></A>       RegistrationPK primaryKey = new RegistrationPK();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087906"></A>       primaryKey.theuser = theuser;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087907"></A>       return primaryKey;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087908"></A>     } catch (CreateException ce) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087909"></A>       throw ce;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087910"></A>     } catch (SQLException sqe) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087911"></A>       throw new CreateException (sqe.getMessage());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087912"></A>     } finally {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087913"></A>      try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087914"></A>         ps.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087915"></A>         con.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087916"></A>       } catch (Exception ignore) {}</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087917"></A>     }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087918"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087919"></A>    public void ejbPostCreate(String theuser, String password, </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087920"></A>      String emailaddress, String creditcard) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087921"></A>      throws CreateException, RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087922"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087923"></A>    public void setEntityContext(javax.ejb.EntityContext ctx)                                 throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087924"></A>      this.ctx = ctx;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087925"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087926"></A>    public void unsetEntityContext() throws RemoteException {   </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087927"></A>      ctx = null;  </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087928"></A>    } </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087929"></A>    public void ejbRemove() throws RemoteException, RemoveException { }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087930"></A>    public void ejbActivate() throws RemoteException { }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087931"></A>    public void ejbPassivate() throws RemoteException { }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087932"></A>    public void ejbLoad() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087933"></A>      System.out.println(&quot;registration load&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087934"></A>      try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087935"></A>        refresh((RegistrationPK) ctx.getPrimaryKey());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087936"></A>      } catch (FinderException fe) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087937"></A>        throw new RemoteException (fe.getMessage());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087938"></A>      }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087939"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087940"></A>    public void ejbStore() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087941"></A>      System.out.println(&quot;registration store&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087942"></A>      Connection con = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087943"></A>      PreparedStatement ps = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087944"></A>      try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087945"></A>        con = getConnection();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087946"></A>        ps = con.prepareStatement(&quot;update registration set password = ?, </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087947"></A>           emailaddress = ?, creditcard = ?, balance = ? where theuser = ?&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087948"></A>        ps.setString(1, password);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087949"></A>        ps.setString(2, emailaddress);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087950"></A>        ps.setString(3, creditcard);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087951"></A>        ps.setDouble(4, balance);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087952"></A>        ps.setString(5, theuser);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087953"></A>        int i = ps.executeUpdate();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087954"></A>        if(i == 0) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087955"></A>          throw new RemoteException (&quot;ejbStore: Registration (&quot; + theuser</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087956"></A>                                     + &quot;) not updated&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087957"></A>        }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087958"></A>      } catch (RemoteException re) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087959"></A>        throw re;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087960"></A>      } catch (SQLException sqe) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087961"></A>        throw new RemoteException (sqe.getMessage());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087962"></A>      } finally {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087963"></A>        try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087964"></A>          ps.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087965"></A>        con.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087966"></A>      } catch (Exception ignore) {}</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087967"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087968"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087969"></A>  public RegistrationPK ejbFindByPrimaryKey(RegistrationPK pk)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087970"></A>                                   throws FinderException, RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087971"></A>    if((pk == null) || (pk.theuser == null)) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087972"></A>      throw new FinderException (&quot;primary key cannot be null&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087973"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087974"></A>    refresh(pk);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087975"></A>    return pk;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087976"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087977"></A>  private void refresh(RegistrationPK pk)                        throws FinderException, RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087978"></A>    if(pk == null) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087979"></A>      throw new RemoteException (&quot;primary key cannot be null&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087980"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087981"></A>    Connection con = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087982"></A>    PreparedStatement ps = null;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087983"></A>    try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087984"></A>      con=getConnection();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087985"></A>      ps=con.prepareStatement(&quot;select password, emailaddress, creditcard,                             balance from registration where theuser = ?&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087986"></A>      ps.setString(1, pk.theuser);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087987"></A>      ps.executeQuery();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087988"></A>      ResultSet rs = ps.getResultSet();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087989"></A>      if(rs.next()) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087990"></A>         theuser = pk.theuser;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087991"></A>         password = rs.getString(1);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087992"></A>         emailaddress = rs.getString(2);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087993"></A>         creditcard = rs.getString(3);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087994"></A>         balance = rs.getDouble(4);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087995"></A>       } else {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087996"></A>         throw new FinderException (&quot;Refresh: Registration (&quot;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087997"></A>                                    + pk.theuser + &quot;) not found&quot;);</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087998"></A>       }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087999"></A>     } catch (SQLException sqe) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088000"></A>       throw new RemoteException (sqe.getMessage());</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088001"></A>     } finally {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088002"></A>       try {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088003"></A>         ps.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088004"></A>         con.close();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088005"></A>       } catch (Exception ignore) {}</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088006"></A>     }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088007"></A>   }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088008"></A>}</PRE><PRE CLASS="CODE"><A NAME="pgfId-1088009"></A>static {    try {      new pool.JDCConnectionDriver(&quot;COM.cloudscape.core.JDBCDriver&quot;,                                    &quot;none&quot;, &quot;none&quot;);    } catch(Exception e){}  }  public Connection getConnection() throws SQLException{    return DriverManager.getConnection(jdbc:jdc:jdcpool&quot;);  }}</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1088011"></A><A NAME="73271"></A>Performance Features and Tools</H4><P CLASS="Body"><A NAME="pgfId-1088012"></A>The new Java virtual machines have features to increase performance, and you can use a number of tools to increase application performance or reduce the size of generated class files. Such features and tools improve the performance of your application with little or no change required to your application. </P><DIV><H5 CLASS="B"><A NAME="pgfId-1088013"></A>Java Virtual Machine Features</H5><P CLASS="Body"><A NAME="pgfId-1088015"></A><A NAME="marker-1088014"></A>The Java 2 Platform release has introduced many performance improvements over previous releases, including faster memory allocation, reduction of class sizes, improved garbage collection, streamlined monitors and a built-in <A NAME="marker-1088016"></A>Just-In-Time compiler (JIT) as standard.</P><P CLASS="Body"><A NAME="pgfId-1088017"></A>When using the new Java 2 virtual machine straight out of the box, you will see an improvement; however, by understanding how the speed-ups work, you can tune your application to squeeze out every last bit of performance. </P><P CLASS="Body"><A NAME="pgfId-1088022"></A><EM CLASS="Bold">Method Inlining. </EM><A NAME="61835"></A><A NAME="marker-1088019"></A><A NAME="marker-1088020"></A><A NAME="marker-1088021"></A>The Java 2 release of the Java virtual machine automatically inlines simple methods at run time. In an unoptimized Java virtual machine, every time a new method is called, a new stack frame is created. The creation of a new stack frame requires additional resources as well as some remapping of the stack. The end result is that creating new stack frames incurs a small overhead. </P><P CLASS="Body"><A NAME="pgfId-1088023"></A>Method inlining increases performance by reducing the number of method calls your program makes. The Java virtual machine inlining code inlines methods that return constants or only access internal fields. To take advantage of method inlining you can do one of two things: You can either make a method look attractive to the virtual machine to inline, or manually inline a method if it does not break your object model. Manual inlining in this context means moving the code from a method into the method that is calling it. Automatic virtual machine inlining is illustrated in this next example: </P><PRE CLASS="CODE"><A NAME="pgfId-1088026"></A><A NAME="marker-1088024"></A><A NAME="marker-1088025"></A>public class InlineMe {  int counter=0;  public void method1() {    for(int i=0;i&lt;1000;i++) {      addCount();      System.out.println(&quot;counter=&quot;+counter);  }  public int addCount() {    counter=counter+1;    return counter;  }  public static void main(String args[]) {    InlineMe im=new InlineMe();    im.method1();  }}</PRE><P CLASS="Body"><A NAME="pgfId-1088033"></A><A NAME="marker-1088027"></A><A NAME="marker-1088028"></A><A NAME="marker-1088029"></A><A NAME="marker-1088030"></A><A NAME="marker-1088031"></A>In its current state the <EM CLASS="CODE">addCount</EM><A NAME="marker-1088032"></A> method does not look very attractive to the inline detector in the virtual machine because the <EM CLASS="CODE">addCount</EM> method returns a value. To find out if this method is inlined, run the compiled example with profiling enabled: </P><PRE CLASS="CODE"><A NAME="pgfId-1088034"></A>java -Xrunhprof:cpu=times InlineMe</PRE><P CLASS="Body"><A NAME="pgfId-1088035"></A>This generates a <EM CLASS="CODE">java.hprof.txt</EM> output file. The top ten methods 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -