📄 simplemobileapp.java
字号:
ps.setString(2, "Grand Ave.");
ps.setInt(3, 1956);
ps.executeUpdate();
System.out.println("Updated 1956 Webster to 180 Grand");
// update another row...
ps.setInt(1, 300);
ps.setString(2, "Lakeshore Ave.");
ps.setInt(3, 180);
ps.execute();
System.out.println("Updated 180 Grand to 300 Lakeshore");
// Select the rows and verify some of the results...
rs = s.executeQuery("SELECT num, addr FROM streetaddr ORDER BY num");
// Verification: Number of rows and sorted contents of the num column
boolean correctResults = true;
if (!rs.next())
{
System.err.println("No rows in table! (ResultSet was empty)");
correctResults = false;
} else {
int num;
int rows = 0;
do {
rows++;
num = rs.getInt(1);
if ((rows == 1) && (num != 300)) {
System.err.println("Wrong first row returned! "
+ "Expected num = 300, but got " + num);
correctResults = false;
} else if ((rows == 2) && (num != 1910)) {
System.err.println("Wrong second row returned! "
+ "Expected num = 1910, but got " + num);
correctResults = false;
}
} while (rs.next());
if (rows !=2) {
System.err.println("Wrong number of rows in ResultSet "
+ "(streetaddr table): " + rows);
correctResults = false;
}
}
if (correctResults) {
System.out.println("Verified the rows");
} else {
System.out.println("Verification failed: Wrong results!");
}
/* This demo automatically drops the table. This way the demo can
* be run the same way multiple times. If you want the data to
* stay in the database, comment out the following Statement
* execution and recompile the class.
*/
s.execute("drop table streetaddr");
System.out.println("Dropped table streetaddr");
// shut down the database
/* In embedded mode, an application should shut down the database.
* If the application fails to shut down the database explicitly,
* the Derby does not perform a checkpoint when the JVM shuts down,
* which means that the next connection will be slower because
* Derby has to perform recovery operations.
* Explicitly shutting down the database using the appropriate
* data source property is recommended.
* This style of shutdown will always throw an SQLException, but in
* this case the exception is (usually) not an indication that
* something went wrong.
*/
try {
ds.setShutdownDatabase("shutdown");
ds.getConnection();
} catch (SQLException se) {
if (!( (se.getErrorCode() == 45000)
&& ("08006".equals(se.getSQLState()) ))) {
// if the error code or SQLState is different, we have an
// unexpected exception (shutdown failed)
printSQLException(se);
se.printStackTrace();
} else {
System.out.println(dbName + " shut down successfully");
}
}
} catch (SQLException e) {
printSQLException(e);
} finally {
// release all open resources to avoid unnecessary memory usage
// ResultSet
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
// Statement
try {
if (s != null) {
s.close();
s = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
//PreparedStatement
try {
if (ps != null) {
ps.close();
ps = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
//Connection
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException sqle) {
printSQLException(sqle);
}
}
}
/**
* Prints information about an SQLException to System.err.
* Use this information to debug the problem.
*
* @param e Some SQLException which info should be printed
*/
public static void printSQLException(SQLException e) {
do {
System.err.println("\n----- SQLException caught: -----");
System.err.println(" SQLState: " + e.getSQLState());
System.err.println(" Error Code: " + e.getErrorCode());
System.err.println(" Message: " + e.getMessage());
//e.printStackTrace(System.err); // enable and recompile to get more info
System.err.println();
e = e.getNextException();
} while (e != null);
}
/**
* Checks if this Java Virtual Machine includes support for the JDBC optional
* package for CDC platforms (JSR-169), or better (JDBC 3.0 or newer), by
* checking the availability of classes or interfaces introduced in or
* removed from specific versions of JDBC-related specifications.
*
* @return true if the required JDBC support level is detected, false
* otherwise.
*/
public static boolean vmSupportsJSR169() {
if (haveClass("java.sql.Savepoint")) {
/* New in JDBC 3.0, and is also included in JSR-169.
* JSR-169 is a subset of JDBC 3 which does not include the Driver
* interface.
* See http://wiki.apache.org/db-derby/VersionInfo for details.
*/
return true;
} else {
return false;
}
}
/**
* Checks if this JVM is able to load a specific class. May for instance
* be used for determining the level of JDBC support available.
* @param className Fully qualified name of class to attempt to load.
* @return true if the class can be loaded, false otherwise.
*/
private static boolean haveClass(String className) {
try {
Class.forName(className);
return true;
} catch (Exception e) {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -