Java Reference
In-Depth Information
ps.setInt(1, 12345);
rs = ps.executeQuery( );
If there are multiple parameters, you address them by number; for example, if there were a
second parameter of type double , its value would be set by:
ps.setDouble(2, 12345);
Example 18-6 is the JDBC version of the User accessor, UserDBJDBC . It uses prepared state-
ments for inserting new users, changing passwords, and setting the last login date.
Example 18-6. UserDBJDBC.java
public
public class
class UserDBJDBC
UserDBJDBC extends
extends UserDB {
protected
protected PreparedStatement setPasswordStatement ;
protected
protected PreparedStatement addUserStmt ;
protected
protected PreparedStatement setLastLoginStmt ;
protected
protected PreparedStatement deleteUserStmt ;
/** insert the dozen or so fields into the user database */
final
final static
static String SQL_INSERT_USER =
"insert into users " +
" values (?,?,?,?,?,?,?,?,?,?,?,?,?)" ;
/** Default constructor */
protected
protected UserDBJDBC () throws
throws NamingException , SQLException , IOException {
super
super ();
System . out . println ( "UserDBJDBC.<init> starting..." );
System . out . println ( "Loading Driver Class" );
try
try {
Class . forName ( "org.hsqldb.jdbcDriver" );
} catch
catch ( ClassNotFoundException ex ) {
System . out . println ( "FAILED: " + ex . toString ());
throw
throw new
new IllegalStateException ( ex . toString ());
}
Connection conn = DriverManager . getConnection (
"jdbc:hsqldb:/home/ian/src/jabadot/WEB-INF/jabadot" ,
"jabadmin" , "fredonia" );
Statement stmt = conn . createStatement ();
Search WWH ::




Custom Search