GithubHelp home page GithubHelp logo

db-util's Introduction

io.github.svaponi:db-utils

Provides easy-to-use features to access DB without JPA integration.

How to

Import dependency.

<dependency>
    <groupId>io.github.svaponi</groupId>
    <artifactId>db-utils</artifactId>
    <version>1.0</version>
</dependency>

To build a DbQuery object you just need a javax.sql.DataSource object to pass as argument to the unique constructor.

Here an example with Spring.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;

@Configuration
public class SpringConfig {

    @Bean
    public DbQuery dbQuery(final DataSource dataSource) {
        return new DbQuery(dataSource);
    }

    @Bean
    public DriverManagerDataSource driverManagerDataSource(
            @Value("${jdbc.driverClassName}") final String driverClassName,
            @Value("${jdbc.url}") final String url,
            @Value("${jdbc.user}") final String username,
            @Value("${jdbc.password}") final String password
    ) {
        final DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(driverClassName);
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setUsername(username);
        driverManagerDataSource.setPassword(password);
        return driverManagerDataSource;
    }
}

Then you can start accessing you DB with standard SQL.

public class Foo {

    public void foo() {
        Timestamp C0 = new Timestamp();
        String C1 = "foo";
        int C2 = 1;
        long C3 = Long.MAX_VALUE;
        double C4 = 0.23;
        
        // Create a new table
        dbq.update("CREATE TABLE test01 (" +
        "C0 timestamp, " +
        "C1 varchar, " +
        "C2 integer, " +
        "C3 bigint, " +
        "C4 double)");
        
        // Insert data into table
        dbq.update("INSERT INTO test01(C0, C1, C2, C3, C4) VALUES (?, ?, ?, ?, ?)", C0, C1, C2, C3, C4);

        // Select data from table
        DbResult result = dbq.select("SELECT C0, C1, C2, C3, C4 FROM test01");
        
        // Delete data from table
        int n = dbq.update("DELETE FROM test01");
        
        Assert.assertEquals(1, n);
    }
}

And you could go on with all SQL that comes into your mind. You can also inject results into a entity object, like Test01.

public class Foo {

    public List<test01> getList() {
        return dbq.getEntityList("SELECT * FROM test01 ORDER BY C0", Test01.class);
    }
}

See all this in action in DbQueryTest

db-util's People

Contributors

svaponi avatar

Watchers

James Cloos avatar  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.