I'm new to Java and also to Postgres.
I have a little project about restaurant and i have a struk(eng: bill) table like this:

and I have a method to inserting information into that table like this:
public int insertBill(int id_karyawan, String tanggal, String waktu, int total) {
String SQL = "INSERT INTO struk(kode, id_karyawan, tanggal, waktu, total) VALUES (?,?,?,?,?)";
int id = 0;
try(Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS)) {
pstmt.setInt(1, 1);
pstmt.setInt(2, id_karyawan);
pstmt.setString(3, tanggal);
pstmt.setString(4, waktu);
pstmt.setInt(5, total);
int affectedRows = pstmt.executeUpdate();
if(affectedRows > 0) {
try(ResultSet rs = pstmt.getGeneratedKeys()) {
if(rs.next()) {
id = rs.getInt(1);
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return id;
}
later, that method will called with this:
int billID = app.insertBill(1, "2017-09-24", "08:00:00", 150000);
The problem is, i dont have any idea about the Date and the time, what should i pass as a parameter? what kind of variable so that the query work well? I've searched about it got some clue to use string. I use string on a query method for now. Any advice?
Since you have said you are new to java, I assume that you would be using Java 8 and The PostgreSQL JDBC driver provides native support for the Java 8 Date and Time API using JDBC 4.2.
Following datatypes for Date/Time are supported by PostgreSQL :
DATE
TIME [ WITHOUT TIMEZONE ]
TIMESTAMP [ WITHOUT TIMEZONE ]
TIMESTAMP WITH TIMEZONE
You can use the LocalDate, LocalTime, LocalDateTime or OffsetDateTime (OffsetDateTime is an immutable representation of a date-time with an offset eg:the value "20th July 2017 at 10:45.24.123456789 +04:00" can be stored in an OffsetDateTime). These classes comes in the java.time package, which was introduced in Java 8, as a new Date-Time API.(It covers several cons of old date-time API like design issues, thread safety, time zone handling issues etc.) However, ZonedDateTime, Instant and OffsetTime / TIME [ WITHOUT TIMEZONE ] are currently not supported in PostgreSQl (as in JDBC 4.2). Now comming to programming part based on you code, you can do it like this.
public int insertBill(int id_karyawan, String tanggal, String waktu, int total) {
LocalDate localDate = LocalDate.now(); //or whatever date you want to use, for passing parameter as string you could use
/*public static LocalDate parse(CharSequence text,
DateTimeFormatter formatter)*/
// LocalDate ld = LocalDate.of(2017, Month.JULY, 20);
LocalTime lt = LocalTime.of(8, 0, 0);
String SQL = "INSERT INTO struk(kode, id_karyawan, tanggal, waktu, total) VALUES (?,?,?,?,?)";
int id = 0;
try(Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS)) {
pstmt.setInt(1, 1);
pstmt.setInt(2, id_karyawan);
pstmt.setObject(3, localDate);
pstmt.setObject(4, lt);
pstmt.setInt(5, total);
pstmt.executeUpdate();
pstmt.close();
Hope this helped you. In case you are using Java versions below 8, you could use joda time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With