Created
July 19, 2013 10:08
-
-
Save ihercowitz/6038123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class SqliteSample { | |
Connection conn = null; | |
boolean inserting = false; | |
public SqliteSample() { | |
} | |
public void openDB() throws ClassNotFoundException { | |
Class.forName("org.sqlite.JDBC"); | |
try { | |
conn = DriverManager.getConnection("jdbc:sqlite:sample.db"); | |
conn.setAutoCommit(false); | |
} catch (SQLException e) { } | |
} | |
public boolean executeStatement(String stat) { | |
boolean executed = false; | |
try { | |
Statement stmt = conn.createStatement(); | |
stmt.setQueryTimeout(30); | |
stmt.executeUpdate(stat); | |
} catch (SQLException e) { | |
} | |
return executed; | |
} | |
public void executePS(int totalRows, String txtInserted) { | |
String query = "INSERT INTO USERS VALUES (?, ?)"; | |
int i = totalRows; | |
try { | |
PreparedStatement ps = conn.prepareStatement(query); | |
ps.setInt(1, i); | |
ps.setString(2, txtInserted+" "+i); | |
ps.executeUpdate(); | |
} catch (SQLException e) { e.printStackTrace(); } | |
} | |
public void commit() { | |
try { | |
conn.commit(); | |
} catch (SQLException e) {} | |
} | |
public void lastRecord() { | |
String query = "SELECT * from USERS ORDER BY ID DESC LIMIT 1"; | |
try { | |
Statement stmt = conn.createStatement(); | |
ResultSet rs = stmt.executeQuery(query); | |
while (rs.next()) { | |
System.out.println("Last Record:"); | |
System.out.println("\t ID: "+rs.getInt("id")); | |
System.out.println("\t NAME: "+rs.getString("name")); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public boolean isInserting() { | |
return inserting; | |
} | |
public void setInserting(boolean status) { | |
inserting = status; | |
} | |
public static void main(String[] args) throws ClassNotFoundException { | |
final int total = args.length > 0? Integer.parseInt(args[0]): 1000; | |
final String txtInserted = args.length > 1? args[1]: "TEST"; | |
final SqliteSample tblUsers = new SqliteSample(); | |
tblUsers.openDB(); | |
tblUsers.executeStatement("CREATE TABLE IF NOT EXISTS USERS (ID INT, NAME TEXT)"); | |
Runnable insert = new Runnable() { | |
public void run() { | |
int i = 0; | |
tblUsers.setInserting(true); | |
while (i < total ) { | |
tblUsers.executePS(i, txtInserted); | |
tblUsers.commit(); | |
i++; | |
} | |
} | |
}; | |
Runnable insert2 = new Runnable() { | |
public void run() { | |
int i = 0; | |
tblUsers.setInserting(true); | |
while (i < total ) { | |
tblUsers.executePS(i, "THREAD2"); | |
tblUsers.commit(); | |
i++; | |
} | |
} | |
}; | |
Runnable insert3 = new Runnable() { | |
public void run() { | |
int i = 0; | |
tblUsers.setInserting(true); | |
while (i < total ) { | |
tblUsers.executePS(i, "THREAD3"); | |
tblUsers.commit(); | |
i++; | |
} | |
tblUsers.setInserting(false); | |
} | |
}; | |
Runnable readLast = new Runnable() { | |
public void run() { | |
while (tblUsers.isInserting()) { | |
tblUsers.lastRecord(); | |
} | |
} | |
}; | |
Thread thrInsert = new Thread(insert); | |
Thread thrInsert2 = new Thread(insert2); | |
Thread thrInsert3 = new Thread(insert3); | |
Thread thrRead = new Thread(readLast); | |
thrInsert.start(); | |
thrInsert2.start(); | |
thrInsert3.start(); | |
thrRead.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment