JPA Insert + Oracle Sequences example

A quick JPA + Oracle sequences example, for self reference. 1. Oracle Database Issue the following SQL script to create a table and a sequence. CREATE TABLE CUSTOMER( ID NUMBER(10) NOT NULL, NAME VARCHAR2(100) NOT NULL, EMAIL VARCHAR2(100) NOT NULL, CREATED_DATE DATE NOT NULL, CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID) ); CREATE SEQUENCE customer_seq MINVALUE 1 …

Read more

Spring Data MongoDB : Insert document

In Spring data MongoDB, you can use save(), insert() to save a or a list of objects into mongoDB database. User user = new User("…"); //save user object into "user" collection / table //class name will be used as collection name mongoOperation.save(user); //save user object into "tableA" collection mongoOperation.save(user,"tableA"); //insert user object into "user" collection …

Read more

Hibernate – Unable to insert if column named is keyword, such as DESC

Problem A table named “category” in MySQL database, contains a “DESC” keyword as column name. CREATE TABLE `category` ( `CATEGORY_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `NAME` varchar(10) NOT NULL, `DESC` varchar(255) NOT NULL, PRIMARY KEY (`CATEGORY_ID`) USING BTREE ); Hibernate XML mapping file <hibernate-mapping> <class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb"> … <property name="desc" type="string"> <column name="DESC" not-null="true" …

Read more

Oracle Stored Procedure INSERT example

Here’s an INSERT stored procedure example in Oracle database. 1. Table SQL Script DBUSER table creation script. CREATE TABLE DBUSER ( USER_ID NUMBER (5) NOT NULL, USERNAME VARCHAR2 (20) NOT NULL, CREATED_BY VARCHAR2 (20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY ( USER_ID ) ) 2. Stored Procedure A stored procedure, accept 4 IN …

Read more

Insert date value in PreparedStatement

Problem A simple table script in Oracle database. CREATE TABLE DBUSER ( USER_ID NUMBER (5) NOT NULL, USERNAME VARCHAR2 (20) NOT NULL, CREATED_BY VARCHAR2 (20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY ( USER_ID ) ) No idea how to insert current date value, e.g. “04/04/2011” into “CREATED_DATE” field, via JDBC PreparedStatement. String insertTableSQL …

Read more

Insert timestamp value in PreparedStatement

Problem A simple table script in Oracle database. CREATE TABLE DBUSER ( USER_ID NUMBER (5) NOT NULL, USERNAME VARCHAR2 (20) NOT NULL, CREATED_BY VARCHAR2 (20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY ( USER_ID ) ) No idea how to insert a timestamp value, e.g. “04/04/2011 14:45:04” into “CREATED_DATE” field, via JDBC PreparedStatement. String …

Read more

JDBC PreparedStatement – Insert a row

A JDBC PreparedStatement example to insert a row into the database. 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 = …

Read more

JDBC Statement – Insert a row

A JDBC Statement example to insert a row into the database. RowInsert.java package com.mkyong.jdbc.statement.row; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDateTime; public class RowInsert { public static void main(String[] args) { // auto close connection and statement try (Connection conn = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password"); Statement statement = conn.createStatement()) { …

Read more

How to add row in JSF dataTable

This example is enhancing previous delete dataTable row example, by adding a “add” function to add a row in dataTable. Here’s a JSF 2.0 example to show you how to add a row in dataTable. 1. Managed Bean A managed bean named “order”, self-explanatory. package com.mkyong; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import …

Read more