日期:2014-05-16 浏览次数:20408 次
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class WwdEmbedded { static String dbName = "HIAPK_DOWNLOAD"; static String APK_DOWNLOAD_RECORDS = "HIAPK_DOWNLOAD_RECORDS"; static String driver = "org.apache.derby.jdbc.EmbeddedDriver"; static String connectionURL = "jdbc:derby:" + dbName + ";create=false"; public static void main(String[] args) { try { /* * 载入derby驱动.嵌入式驱动使用这个方式启动derby数据库引擎. 检查初始化动作,检查CLASSPATH路径设置问题 */ Class.forName(driver); System.out.println(driver + " loaded. "); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); System.out .println("\n >>> Please check your CLASSPATH variable <<<\n"); } Insert insert = new Insert(); Query query = new Query(); new Thread(insert, "写入线程").start(); new Thread(query, "查询线程").start(); try { Thread.currentThread(); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } // ## DATABASE SHUTDOWN SECTION ## /*** * In embedded mode, an application should shut down Derby. Shutdown * throws the XJ015 exception to confirm success. ***/ if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) { try { DriverManager.getConnection("jdbc:derby:;shutdown=true"); System.out.println("Database shut down normally"); } catch (SQLException se) { if (se.getSQLState().equals("XJ015")) { System.out.println("Database did not shut down normally"); } } } } static class Insert implements Runnable { @Override public void run() { Connection conn = null; // apk 下载记录表 String createSql = "create table " + APK_DOWNLOAD_RECORDS + "(" + "ID int generated always as identity primary key,AID int,SOFTCODE int," + " RECORD varchar(1024)" + ")"; String[] createIndexSql = new String[] { "CREATE INDEX IDX_ADR_AID ON " + APK_DOWNLOAD_RECORDS + "(AID)", "CREATE INDEX IDX_ADR_SOFTCODE ON " + APK_DOWNLOAD_RECORDS + "(SOFTCODE)" }; // Beginning of JDBC code sections // ## LOAD DRIVER SECTION ## // Beginning of Primary DB access section // ## BOOT DATABASE SECTION ## try { // Create (if needed) and connect to the database conn = DriverManager.getConnection(connectionURL); System.out.println("Connected to database " + dbName); /* * 新建HIAPK_DOWNLOAD表结构和索引 * System.out.println(" . . . . creating table " + * APK_DOWNLOAD_RECORDS); s.execute(createSql); * s.execute(createIndexSql[0]); s.execute(createIndexSql[1]); */ // 模拟大数据量插入 PreparedStatement psInsert = conn .prepareStatement("insert into " + APK_DOWNLOAD_RECORDS + "(AID,SOFTCODE,RECORD) values (?,?,?)"); doinsert(psInsert); conn.close(); System.out.println("Closed connection"); // Beginning of the primary catch block: uses errorPrint method } catch (Throwable e) { /* * Catch all exceptions and pass them to* the exception * reporting method */ System.out.println(" . . . Exception thrown:"); errorPrint(e); } System.out.println("Derby JDBC program ending."); } } // ## DERBY EXCEPTION REPORTING CLASSES ## /*** * Exception reporting methods with special handling of SQLExceptions ***/ static void errorPrint(Throwable e) { if (e instanceof SQLException) SQLExceptionPrint((SQLException) e); else { System.out.println("A non SQL error occured."); e.printStackTrace(); } } // END errorPrint // Iterates through a stack of SQLExceptions static void SQLExceptionPrint(SQLException sqle) { while (sqle != null) { System.out.println("\n---SQLException Caught---\n"); System.out.println("SQLState: " + (sqle).getSQLState()); Syste