io.github.laplacedemon:async-mysql-client

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

MySQL Data Databases CLI User Interface
GroupId

GroupId

io.github.laplacedemon
ArtifactId

ArtifactId

async-mysql-client
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/LaplaceDemon/async-mysql-client

Download async-mysql-client

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.laplacedemon/async-mysql-client/ -->
<dependency>
    <groupId>io.github.laplacedemon</groupId>
    <artifactId>async-mysql-client</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.laplacedemon/async-mysql-client/
implementation 'io.github.laplacedemon:async-mysql-client:0.1.0'
// https://jarcasting.com/artifacts/io.github.laplacedemon/async-mysql-client/
implementation ("io.github.laplacedemon:async-mysql-client:0.1.0")
'io.github.laplacedemon:async-mysql-client:jar:0.1.0'
<dependency org="io.github.laplacedemon" name="async-mysql-client" rev="0.1.0">
  <artifact name="async-mysql-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.laplacedemon', module='async-mysql-client', version='0.1.0')
)
libraryDependencies += "io.github.laplacedemon" % "async-mysql-client" % "0.1.0"
[io.github.laplacedemon/async-mysql-client "0.1.0"]

Dependencies

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
mysql : mysql-connector-java jar 8.0.13
org.apache.httpcomponents : httpasyncclient jar 4.1.4

Project Modules

There are no modules declared in this project.

async-mysql-client

Asynchronous mysql client.

Get it from maven:

<dependency>
  <groupId>io.github.laplacedemon</groupId>
  <artifactId>async-mysql-client</artifactId>
  <version>0.1.0</version>
</dependency>

Base

Create a asyncMySQL :

AsyncMySQL asyncMySQL = AsyncMySQL.create();

Create a config :

Config config = asyncMySQL.makeConfig("192.168.56.101", 3306, "root", "123456");

Connection

Get useful MySQL connections asynchronously :

asyncMySQL.connect(config, (Connection con) -> {
    System.out.println("Connection completed!");
});

asyncMySQL.start();

Asynchronous execution of sql statement

Query sql statement :

final String sql = "select 1+1,1+2,2+3,3+5";

asyncMySQL.connect(config, (Connection con) -> {
    System.out.println("Connection completed!");
    AsyncPreparedStatement asyncPS = con.prepareStatement(sql);
    con.executeQuery(asyncPS, (ResultSet resultset)->{
        System.out.println("查询完成!");
        printResultSet(resultset);
    });
});

asyncMySQL.start();

Written sql statement :

final String sql = "INSERT INTO `testdb`.`t_student` (`name`, `age`) VALUES ('xiaoming04', '4')";

asyncMySQL.connect(config, (Connection con) -> {
    System.out.println("Connection completed!");
    con.executeUpdate(sql, (long count, long id)->{
        printResultSet(count, id);
    });
});

asyncMySQL.start();

Connection Pool

final String sql = "select 1+1,1+2,2+3,3+5";

final ConnectionPool cp = asyncMySQL.createPool(config, 10);

cp.get(con -> {
    System.out.println("Number of idle connections:" 
                        + cp.getFreeConnectionCount());
    con.executeQuery(sql, r -> {
        printResultSet(r);
    });
});
System.out.println("Number of idle connections:" + cp.getFreeConnectionCount());

asyncMySQL.start();
asyncMySQL.createPool(config, 10, (ConnectionPool cp)->{
    cp.get(con -> {
        System.out.println("Number of idle connections:"
                            + cp.getFreeConnectionCount());
        con.executeQuery(sql, r -> {
            printResultSet(r);
        });
    });
    
    System.out.println("Number of idle connections:" + cp.getFreeConnectionCount());
});

asyncMySQL.start();

Use transaction

asyncMySQL.connect(config, (Connection con) -> {
    con.beginTxn(() -> {
        con.executeUpdate("INSERT INTO `testdb`.`t_student` (`name`, `age`) VALUES ('xiaohong01', 12)", (long count0, long id0)->{
            System.out.println("Completion of transaction 1...");

            con.executeUpdate("INSERT INTO `testdb`.`t_student` (`name`, `age`) VALUES ('xiaohong02', 13)",(long count1, long id1) -> {
                System.out.println("Completion of transaction 2...");
                
                con.endTxn(()->{
                    System.out.println("End of transaction");
                });
            });
        });
    });
});

asyncMySQL.start();

Versions

Version
0.1.0