将日期插入数据库Postgres JDBC

时间:2017-07-20 06:30:30

标签: java postgresql jdbc

我是Java和Postgres的新手。

我有一个关于餐厅的小项目,我有一个像这样的strk(eng:bill)表:

enter image description here

我有一种方法可以将信息插入到该表中:

<div ng-repeat="a in list">
......
<md-input-container class="md-block select-option-input" style="margin:0px" flex-gt-sm>  
        <md-select
        name="tableNo" placeholder="Nr. masa"
        ng-model="a.tableNo" ng-change="tableEdit(a)" class="md-block" flex-gt-sm>
            <md-optgroup  label="Nr. Masa" flex > <md-option
                ng-value="tableNo" ng-repeat="tableNo in getNumber(me.settings.tableNo)"  >{{tableNo}}</md-option>
            </md-optgroup> 
        </md-select>
</md-input-container>
....
</div>

稍后,该方法将调用:

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;
}

问题是,我对日期和时间一无所知,我应该作为参数传递什么?什么样的变量使查询工作得很好?我搜索了它有一些使用字符串的线索。我现在在查询方法上使用字符串。有什么建议吗?

2 个答案:

答案 0 :(得分:5)

由于您已经说过您是java新手,我假设您将使用Java 8并且PostgreSQL JDBC驱动程序使用JDBC 4.2为Java 8 Date和Time API提供本机支持。

PostgreSQL支持以下数据类型的日期/时间:

DATE
TIME [ WITHOUT TIMEZONE ]
TIMESTAMP [ WITHOUT TIMEZONE ]
TIMESTAMP WITH TIMEZONE

您可以使用LocalDate,LocalTime,LocalDateTime或OffsetDateTime(OffsetDateTime是具有偏移量的日期时间的不可变表示,例如:值“2017年7月20日10:45.24.123456789 +04:00”可以存储在OffsetDateTime中)。这些类来自java.time包,它是在Java 8中引入的,作为一个新的Date-Time API。(它涵盖了几个旧的日期时间API,如设计问题,线程安全,时区处理问题等)。但是,PostgreSQl当前不支持ZonedDateTime,Instant和OffsetTime / TIME [WITHOUT TIMEZONE](如JDBC 4.2中所示)。现在开始根据你的代码编写部分,你可以这样做。

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();

希望这对你有所帮助。如果您使用的是低于8的Java版本,则可以使用joda time。

答案 1 :(得分:0)

希望这对您有帮助:

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.setDate(3, java.sql.Date.valueof("2017-09-24")); 
    pstmt.setTime(4, java.sql.Time.valueof("08:00:00")); 
    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;

}
相关问题