Java Reference
In-Depth Information
116
}
117
catch ( Exception e )
118
{
119
System .out.println ( "Exception creating UserStocks table: "
120
+ e.getMessage ()) ;
121
}
122
123
// Create UserStocks table primary key index
124
try
125
{
126
System .out.println ( "Creating UserStocks table primary key index..." ) ;
127
stmt.executeUpdate ( "CREATE UNIQUE INDEX PK_UserStocks "
128
+ "ON UserStocks (userID, symbol) "
129
+ "WITH PRIMARY DISALLOW NULL" ) ;
130
}
131
catch ( Exception e )
132
{
133
System .out.println ( "Exception creating UserStocks index: "
134
+ e.getMessage ()) ;
135
}
136
137
138
// Create one administrative user with password as initial data
139
String userID = "admin01" ;
140
String firstName = "Default" ;
141
String lastName = "Admin" ;
142
String initialPswd = "admin01" ;
143
Password pswd = new Password ( initialPswd ) ;
144
boolean admin = true ;
145
146
PreparedStatement pStmt =
147
con.prepareStatement ( "INSERT INTO Users VALUES (?,?,?,?,?)" ) ;
148
try
149
{
150
pStmt.setString ( 1, userID ) ;
151
pStmt.setString ( 2, lastName ) ;
152
pStmt.setString ( 3, firstName ) ;
153
pStmt.setBytes ( 4, serializeObj ( pswd )) ;
154
pStmt.setBoolean ( 5, admin ) ;
155
pStmt.executeUpdate () ;
156
}
157
catch ( Exception e )
158
{
159
System .out.println ( "Exception inserting user: "
160
+ e.getMessage ()) ;
161
}
162
163
pStmt.close () ;
164
165
// Read and display all User data in the database.
166
ResultSet rs = stmt.executeQuery ( "SELECT * FROM Users" ) ;
167
168
System .out.println ( "Database created.\n" ) ;
169
System .out.println ( "Displaying data from database...\n" ) ;
170
System .out.println ( "Users table contains:" ) ;
171
172
Password pswdFromDB;
173
byte [] buf = null ;
174
175
while ( rs.next ())
176
{
177
System .out.println ( "Logon ID
= "
178
+ rs.getString ( "userID" )) ;
179
System .out.println ( "First name
= "
180
+ rs.getString ( "firstName" )) ;
181
System .out.println ( "Last name
= " +rs.getString ( "lastName" )) ;
182
System .out.println ( "Administrative
= " +rs.getBoolean ( "admin" )) ;
183
System .out.println ( "Initial password = " +initialPswd ) ;
184
FIGURE 11-17
(continued)
Search WWH ::




Custom Search