Giter VIP home page Giter VIP logo

springboot-h2-database's Introduction

SpringBoot-H2-Database:

Install h2 console:

run in local -> 192.168.0.105:8082/login.jsp?jsessionid=57ffec2d5129387e3198173cfe74194a

	CREATE TABLE CUSTOMER (id number, name varchar(20), age number, address varchar(20), 
	salary number);  

	INSERT into CUSTOMER values (1, 'Ramesh', 32, 'Ahmedabad', 2000); 

	INSERT into CUSTOMER values (2, 'Khilan', 25, 'Delhi', 1500); 

H2 in JDBC:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement;  

public class H2jdbcCreateDemo { 
   // JDBC driver name and database URL 
   static final String JDBC_DRIVER = "org.h2.Driver";   
   static final String DB_URL = "jdbc:h2:~/test";  

   //  Database credentials 
   static final String USER = "sa"; 
   static final String PASS = ""; 

   public static void main(String[] args) { 
      Connection conn = null; 
      Statement stmt = null; 
      try { 
	 // STEP 1: Register JDBC driver 
	 Class.forName(JDBC_DRIVER); 

	 //STEP 2: Open a connection 
     System.out.println("Connecting to database..."); 
     conn = DriverManager.getConnection(DB_URL,USER,PASS);  
     
     //STEP 3: Execute a query 
     System.out.println("Creating table in given database..."); 
     stmt = conn.createStatement(); 
     String sql =  "CREATE TABLE   REGISTRATION " + 
        "(id INTEGER not NULL, " + 
        " first VARCHAR(255), " +  
        " last VARCHAR(255), " +  
        " age INTEGER, " +  
        " PRIMARY KEY ( id ))";  
     stmt.executeUpdate(sql);
     System.out.println("Created table in given database..."); 
     
     // STEP 4: Clean-up environment 
     stmt.close(); 
     conn.close(); 
  } catch(SQLException se) { 
     //Handle errors for JDBC 
     se.printStackTrace(); 
  } catch(Exception e) { 
     //Handle errors for Class.forName 
     e.printStackTrace(); 
  } finally { 
     //finally block used to close resources 
     try{ 
        if(stmt!=null) stmt.close(); 
     } catch(SQLException se2) { 
     } // nothing we can do 
     try { 
        if(conn!=null) conn.close(); 
     } catch(SQLException se){ 
        se.printStackTrace(); 
     } //end finally try 
  } //end try 
  System.out.println("Goodbye!");

} }

Change login H2 url to

jdbc:h2:mem:testdb

REST request validation annotations:

@AssertFalse The annotated element must be false.

@AssertTrue The annotated element must be true.

@DecimalMax The annotated element must be a number whose value must be lower or equal to the specified maximum.

@DecimalMin The annotated element must be a number whose value must be higher or equal to the specified minimum.

@Future The annotated element must be an instant, date or time in the future.

@Max The annotated element must be a number whose value must be lower or equal to the specified maximum.

@Min The annotated element must be a number whose value must be higher or equal to the specified minimum.

@Negative The annotated element must be a strictly negative number.

@NotBlank The annotated element must not be null and must contain at least one non-whitespace character.

@NotEmpty The annotated element must not be null nor empty.

@NotNull The annotated element must not be null.

@Null The annotated element must be null.

@Pattern The annotated CharSequence must match the specified regular expression.

@Positive The annotated element must be a strictly positive number.

@Size The annotated element size must be between the specified boundaries (included).

Query:

Delete the table if it exists:

DROP TABLE IF EXISTS TEST;

Create a new table with ID and NAME columns:

CREATETABLE TEST(ID INT PRIMARY KEY,  NAME VARCHAR(255));

Add a new row:

INSERT INTO TEST VALUES(1, 'Hello');

Add another row:

INSERT INTO TEST VALUES(2, 'World');

Query the table:

SELECT * FROM TEST ORDER BY ID;

Change data in a row:

UPDATE TEST SET NAME='Hi' WHERE ID=1;

Remove a row:

DELETE FROM TEST WHERE ID=2;

logging, log4j, logback

    LOGGER.debug("This is a debug message");

LOGGER.info("This is an info message");

LOGGER.warn("This is a warn message");

LOGGER.error("This is an error message");

test H2 console

Follow us on Blog

springboot-h2-database's People

Contributors

sandysanthosh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.