一个将行插入数据库的JDBC PreparedStatement
示例。
RowInsert.java
package com.mkyong.jdbc.preparestatement.row;
import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDateTime;
public class RowInsert {
private static final String SQL_INSERT = "INSERT INTO EMPLOYEE (NAME, SALARY, CREATED_DATE) VALUES (?,?,?)";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password");
PreparedStatement preparedStatement = conn.prepareStatement(SQL_INSERT)) {
preparedStatement.setString(1, "mkyong");
preparedStatement.setBigDecimal(2, new BigDecimal(799.88));
preparedStatement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
int row = preparedStatement.executeUpdate();
// rows affected
System.out.println(row); //1
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
表定义。
CREATE TABLE EMPLOYEE
(
ID serial,
NAME varchar(100) NOT NULL,
SALARY numeric(15, 2) NOT NULL,
CREATED_DATE timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (ID)
);
PS已通过PostgreSQL 11和Java 8测试
pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
下载源代码
$ git clone https://github.com/mkyong/java-jdbc.git
参考文献
翻译自: https://mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/