| |
"MM.MySQL and the MySQL Database System"
Vol. 5, Issue 12, p. 76
Listing 1
// Assume conn is a java.sql.Connection to a MySQL database
// that has already been created using DriverManager.getCon-
// nection()
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO myTable VALUES ('abc', '123')");
// Get the value for the AUTO_INCREMENT primary key
long autoKey = ((org.gjt.mm.mysql.Statement)stmt).getLastIn- sertID();
// Get the (potentially) large update count (in this case // should be "1"
long bigUpdateCount = ((org.gjt.mm.mysql.Statement)stmt).get LongUpdateCount();
Listing 2
// Assume conn is a java.sql.Connection to a MySQL database
// that has already been created using DriverManager.getCon-
// nection(), and that we have a java.security.cert.Certifi-
// cate named "cert" given to us by some client that we want // to store in the "serObject" table in our MySQL database
PreparedStatement pstmt =
conn.prepareStatement("INSERT INTO serObject values (?, ?)");
pstmt.setString(1, "username");
pstmt.setObject(2, cert);
// By calling executeUpdate here, a row will be inserted,
// where the user column will be set to "username" and the
// cert column will have a serialized version of
// the certificate
pstmt.executeUpdate();
pstmt.close();
// Okay, now let's retrieve the certificate
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select cert from serObject where user='username'");
while (rs.next()) {
// If you're using MM.MySQL 2.0.2 or earlier, retrieve the
// certificate using the utility class
java.security.cert.Certificate aSerializedCert =
(java.security.cert.Certificate)org.gjt.mm.mysql.Util.readOb- ject(rs, 1);
// Or, if you're using MM.MySQL 2.0.3 or newer, use this
// format:
java.security.cert.Certifcate aSerializedCert =
(java.security.cert.Certificate)rs.readObject(1);
// Now, do something with the certificate
}
rs.close();
stmt.close();
|
|