Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public class Main {
  2. public static final String URL = "jdbc:h2:tcp://localhost/~/demo_jdbc";
  3. public static final String USER = "demo_user";
  4. public static final String PASSWORD = "demo";
  5.  
  6. public static void main(String... args) {
  7. Main main = new Main();
  8. main.insert(10, "山田 たろう");
  9. //...
  10. }
  11.  
  12. public void insert(int id, String name) {
  13. try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);) {
  14. String sql = "INSERT INTO TBL_ES0206 (ID, DATA, REG_DAY, UPD_DAY) VALUES (?, ?, SYSDATE, SYSDATE)";
  15. try (PreparedStatement pstmt = conn.prepareStatement(sql);) {
  16. pstmt.setInt(1, id);
  17. pstmt.setString(2, name);
  18. conn.commit();
  19. } catch (SQLException ex) {
  20. ex.printStackTrace();
  21. if (conn!= null) {
  22. conn.rollback();
  23. }
  24. }
  25. } catch (SQLException ex) {
  26. ex.printStackTrace();
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement