Skip to content

Instantly share code, notes, and snippets.

@yuizho
Created June 14, 2016 01:22
Show Gist options
  • Save yuizho/004952a74f2562d9327234579e45c19f to your computer and use it in GitHub Desktop.
Save yuizho/004952a74f2562d9327234579e45c19f to your computer and use it in GitHub Desktop.
public class Main {
public static final String URL = "jdbc:h2:tcp://localhost/~/demo_jdbc";
public static final String USER = "demo_user";
public static final String PASSWORD = "demo";
public static void main(String... args) {
Main main = new Main();
main.insert(10, "山田 たろう");
//...
}
public void insert(int id, String name) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);) {
String sql = "INSERT INTO TBL_ES0206 (ID, DATA, REG_DAY, UPD_DAY) VALUES (?, ?, SYSDATE, SYSDATE)";
try (PreparedStatement pstmt = conn.prepareStatement(sql);) {
pstmt.setInt(1, id);
pstmt.setString(2, name);
conn.commit();
} catch (SQLException ex) {
ex.printStackTrace();
if (conn!= null) {
conn.rollback();
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
CREATE TABLE tbl_ex0206
(
ID DECIMAL(5) PRIMARY KEY,
DATA VARCHAR(20),
REG_DAY DATE(8),
UPD_DAY DATE(8)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment