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());
System.out.println("Severity: " + (sqle).getErrorCode());
System.out.println("Message: " + (sqle).getMessage());
// sqle.printStackTrace();
sqle = sqle.getNextException();
}
} // END SQLExceptionPrint
private static void doinsert(PreparedStatement psInsert)
throws SQLException {
long currtime = 0;
Apk_Download_Record table = new Apk_Download_Record(1, 2, 3, "TEST");
for (int i = 0; i < 1000; i++) {
currtime = System.currentTimeMillis();
psInsert.setInt(1, i);
psInsert.setInt(2, table.getSoftcode());
psInsert.setString(3, table.getRecord());
psInsert.executeUpdate();
System.out.println("newline:" + i + " "
+ (System.currentTimeMillis() - currtime));
}
psInsert.close();
}
static class Query implements Runnable {
@Override
public void run() {
int index = 0;
Connection conn = null;
Statement s = null;
try {
// Create (if needed) and connect to the database
System.out.println("Connected to database " + dbName);
conn = DriverManager.getConnection(connectionURL);
s = conn.createStatement();
ResultSet rs = s.executeQuery("select count(id) from "
+ APK_DOWNLOAD_RECORDS);
while (rs.next()) {
index++;
System.out.println(rs.getString(index) + "行");
}
} catch (Throwable e) {
/*
* Catch all exceptions and pass them to* the exception
* reporting method
*/
System.out.println(" . . . Exception thrown:");
errorPrint(e);
} finally {
try {
s.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
嵌入式DERBY数据库试用
最新推荐文章于 2024-05-15 01:11:36 发布