📄 bmp3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 3: Bean-Managed
Persistence and the JDBC platform</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)proj.src 1.3 05/11/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 3 Continued: Bean-Managed Persistence and the
JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT> Platform</FONT></H2>
<FONT SIZE="-1">[<A HREF="bmp.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="bmp4.html">NEXT>></A>]</FONT></DIV>
<P>
There might be times when you want to override container-managed
persistence and implement entity or session Bean methods to use the SQL
commands you provide. This type of Bean-managed persistence can be useful
if you need to improve performance or map data in multiple Beans to one
row in a database table.
<P>
This section shows you how to convert the
<A HREF="./Code/registration/RegistrationBean.java">RegistrationBean.java</A>
class to access the database with the JDBC<FONT SIZE="-2"><SUP>TM</SUP></FONT> <CODE>PreparedStatement</CODE> class.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#connect">Connect to Database</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#create">Create Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#load">Load Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#refresh">Refresh Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#store">Store Method</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#find">Find Method</A></FONT>
</UL>
<HR>
<A NAME="connect"></A>
<H3>Connect to Database</H3>
This version of the
<A HREF="./Code/registration/JDBC/RegistrationBean.java">RegistrationBean.java</A>
class establishes a connection to the database by instantiating a static
<CODE>Driver</CODE> class and providing the <CODE>getConnection</CODE> method.
<P>
The <CODE>getConnection</CODE> method queries the static
<CODE>DriverManager</CODE> class for a registered database driver
that matches the Uniform Resource Locator (URL) . In this case, the URL is
<CODE>weblogic.jdbc.jts.Driver</CODE>.
</FONT>
<PRE>
//Create static instance of database driver
static {
new weblogic.jdbc.jts.Driver();
}
//Get registered driver from static instance
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(
"jdbc:weblogic:jts:ejbPool");
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="create"></A>
<H3>Create Method</H3>
The <CODE>ejbCreate</CODE> method assigns
values to data member variables, gets a connection to
the database, and creates an instance of the
<CODE>java.sql.PreparedStatement</CODE> class to execute the
SQL statement for writing the data to the <CODE>registration</CODE>
table in the database.
<P>
A <CODE>PreparedStatement</CODE> object is created from a SQL
statement which is sent to the database and precompiled before
any data is sent. You call the appropriate <CODE>setXXX</CODE> statements
on the <CODE>PreparedStatement</CODE> object to send the data.
Keeping the <CODE>PreparedStatement</CODE> and
<CODE>Connection</CODE> objects has private instance
variables greatly reduces overhead because the SQL statement does
not have to be compiled everytime data is sent.
<P>
The last thing the <CODE>ejbCreate</CODE> method
does is create a primary key class with the
user Id, and return it to the container.
</FONT>
<PRE>
public RegistrationPK ejbCreate(String theuser,
String password,
String emailaddress,
String creditcard)
throws CreateException, RemoteException {
this.theuser=theuser;
this.password=password;
this.emailaddress=emailaddress;
this.creditcard=creditcard;
this.balance=0;
try {
con=getConnection();
ps=con.prepareStatement("insert into registration (
theuser, password,
emailaddress, creditcard,
balance) values (
?, ?, ?, ?, ?)");
ps.setString(1, theuser);
ps.setString(2, password);
ps.setString(3, emailaddress);
ps.setString(4, creditcard);
ps.setDouble(5, balance);
if (ps.executeUpdate() != 1) {
throw new CreateException (
"JDBC did not create a row");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -