GithubHelp home page GithubHelp logo

db_utils's Introduction

db_utils

Really simple helper for JDBC.

Maven dependency

<dependency>
  <groupId>io.github.joseerodrigues.utils</groupId>
  <artifactId>db_utils</artifactId>
  <version>1.0.5</version>
</dependency>
DataSource ds = ...;
DBUtil dbUtil = new DBUtil(ds);

Or

Connection conn  = ...;
DBUtil dbUtil = new DBUtil(conn);

Or

DBUtil dbUtil = new DBUtil(new SQLConnectionFactory() {
    @Override
    public Connection getConnection() throws SQLException {
        return makeConnectionSomehow();
    }
});

NOTE When passing a java.sql.Connection to the constructor instead of a javax.sql.DataSource, the connection will not be closed by DBUtil and should be closed by the client. This is to easily allow for multiple instances of DBUtil sharing the same Connection, such as when useConnection is called.

Then

dbUtil.iterate(...);
dbUtil.selectAll(...);
dbUtil.selectOne(...);
dbUtil.hasResults(...);

dbUtil.insert(...);
dbUtil.update(...);
dbUtil.delete(...);

dbUtil.useConnection(...);

Sample Usage

  • iterate
dbUtil.iterate("SELECT * FROM TEST.TABLE_NAME WHERE COL_NAME = ?", 
    Mappers.mapMapper(), new SimpleResultSetIterator<ResultSetMap>() {
    
    @Override
    public boolean iterate(ResultSetMap item) {

        System.out.println(item);
        return true; // false to stop iterating
    }
}, "colValue");
  • selectOne
int colIndex = 1;
Integer count = dbUtil.selectOne("SELECT COUNT(1) FROM TEST.TABLE_NAME", Mappers.intMapper(colIndex));
System.out.println("Count  = " + count);
  • hasResults
boolean hasResults = dbUtil.hasResults("SELECT * FROM TEST.TABLE_NAME WHERE COL_NAME = ?", "colValue");
System.out.println("hasResults = " + hasResults);
  • selectAll

Considering the following table TEST_TABLE

id shortinfo quantity
1 This is a short info 30
2 another one 20
3 something something 15
4 another something 10

And the following java Entity

@DBTable("TEST_TABLE")
public class Product {
    @DBColumn(value = "shortinfo", trim = true)
    private String description = null;

    @DBColumn
    private int quantity = null;

    @DBColumn
    private long id = null;

    /* getters and setters*/    
}

The code for selectAll becomes:

List<Product> products = dbUtil.selectAll("SELECT * FROM TEST_TABLE", 
                                Mappers.beanMapper(Product.class));

And to select only those items with quantity >= 20:

List<Product> products = dbUtil.selectAll("SELECT * FROM TEST_TABLE WHERE quantity >= ?", 
                                Mappers.beanMapper(Product.class), 20);

Note selectAll fetches eagerly. Be careful when using it to retrieve very large amounts of data.

  • insideTransaction and rollback
dbUtil.insideTransaction(transactionDBUtil ->
{
    DAO<Product> productDAO = DAOBuilder.build(transactionDBUtil, Product.class);

    List<Server> allProducts = productDAO.selectAll();
    assertNotNull(allProducts);
    assertTrue(allProducts.size() > 0);

    for (Server s : allProducts) {
        assertTrue(productDAO.delete(s));
    }

    // all deleted
    assertEquals(0, productDAO.count());

    transactionDBUtil.rollback();

    // all restored
    assertEquals(allProducts.size(), productDAO.count());
    return true;
});

db_utils's People

Contributors

joseerodrigues avatar dependabot[bot] avatar

Stargazers

 avatar Andrew NS Yeow avatar

Watchers

 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.