Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql. *;
- static final String JDBC_DRIVER = "org.h2.Driver";
- static final String DB_URL = "jdbc:h2:~/myDB";
- static final String USER = "test";
- static final String PASS = "test";
- static final Connection conn = null;
- static final Statement stmt = null;
- public class DataBaseManager {
- public void insertIntoDB(String id1val, String id2val, Boolean raw_yn_val, ...,...) {
- try {
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- myStatement = "INSERT INTO PRODUCTS VALUES(?,?,?,..,...)";
- stmt = conn.prepareStatement(myStatement);
- stmt.setString(1, id1val);
- stmt.setString(2, id2val);
- stmt.setBoolean(3, raw_yn_val);
- stmt.setString(4,....);
- // Continue up to 55
- stmt.executeUpdate();
- }
- [catch&finally blocks]
- }
- }
- INSERT INTO PRODUCTS SELECT * FROM(
- select 0, 'id1' union // <--- How does this fit into Prepared Statement?
- select 1, 'id2' union
- select 2, 'raw_yn' union
- ) x where not exists(SELECT * FROM PRODUCTS); // <--- Ensures only works when empty
- try {
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- statement = conn.createStatement();
- String sql = "SELECT COUNT(*) AS n FROM PROPERTIES WHERE id1='" + id1 + "' AND id2='" + id2 + "' AND raw_yn='true'";
- rs = statement.executeQuery(sql);
- rs.next();
- if (rs.getInt("n") == 0) {
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- myStatement = "INSERT INTO PROPERTIES VALUES(?,?,?,...)";
- stmt = conn.prepareStatement(myStatement);
- stmt.setString(1, id1);
- stmt.setString(2, id2);
- stmt.setBoolean(3, raw_yn);
- stmt.executeUpdate();
- } else {
- return; // <-- Takes 5x longer to go through ???
- }
- }
- [catch & finally blocks]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement