📄 guestdatabean.java
字号:
// GuestDataBean.java
// Class GuestDataBean makes a database connection and supports
// inserting and retrieving data from the database.
package com.deitel.advjhtp1.jsp.beans;
// Java core packages
import java.io.*;
import java.sql.*;
import java.util.*;
public class GuestDataBean {
private Connection connection;
private PreparedStatement addRecord, getRecords;
// construct TitlesBean object
public GuestDataBean() throws Exception
{
// load the Cloudscape driver
Class.forName( "COM.cloudscape.core.RmiJdbcDriver" );
// connect to the database
connection = DriverManager.getConnection(
"jdbc:rmi:jdbc:cloudscape:guestbook" );
getRecords =
connection.prepareStatement(
"SELECT firstName, lastName, email FROM guests"
);
addRecord =
connection.prepareStatement(
"INSERT INTO guests ( " +
"firstName, lastName, email ) " +
"VALUES ( ?, ?, ? )"
);
}
// return an ArrayList of GuestBeans
public List getGuestList() throws SQLException
{
List guestList = new ArrayList();
// obtain list of titles
ResultSet results = getRecords.executeQuery();
// get row data
while ( results.next() ) {
GuestBean guest = new GuestBean();
guest.setFirstName( results.getString( 1 ) );
guest.setLastName( results.getString( 2 ) );
guest.setEmail( results.getString( 3 ) );
guestList.add( guest );
}
return guestList;
}
// insert a guest in guestbook database
public void addGuest( GuestBean guest ) throws SQLException
{
addRecord.setString( 1, guest.getFirstName() );
addRecord.setString( 2, guest.getLastName() );
addRecord.setString( 3, guest.getEmail() );
addRecord.executeUpdate();
}
// close statements and terminate database connection
protected void finalize()
{
// attempt to close database connection
try {
getRecords.close();
addRecord.close();
connection.close();
}
// process SQLException on close operation
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
}
}
/***************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have *
* used their best efforts in preparing the book. These *
* efforts include the development, research, and testing of *
* the theories and programs to determine their effectiveness. *
* The authors and publisher make no warranty of any kind, *
* expressed or implied, with regard to these programs or to *
* the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for *
* incidental or consequential damages in connection with, or *
* arising out of, the furnishing, performance, or use of *
* these programs. *
***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -