Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/RAPublic

The library provides mysql (CRUD), socket server, logging.

License

NotificationsYou must be signed in to change notification settings

RayTW/RA

Repository files navigation

Maven CentralBuild Statuscodecov

The library provides mysql (CRUD), socket server, logging.

Download

You can download a jar from GitHub'sreleases page.

Or use Gradle:

repositories {  mavenCentral()}dependencies {  implementation'io.github.raytw:ra:0.7.0'}

How to use BigQuery JDBC?

Setp 1

Download the BigQueryJDBC 4.2-compatible file.

Setp 2

Import the library(GoogleBigQueryJDBC42.jar) to the java build path.

Setp 3

dependencies {  implementation'io.github.raytw:ra:0.7.0'  implementation'com.google.cloud:google-cloud-bigquery:2.16.1'}

How to use Cloud Spanner JDBC?

dependencies {  implementation'io.github.raytw:ra:0.7.0'  implementation'com.google.cloud:google-cloud-spanner-jdbc:2.7.9'}

API Document

Example

Connection to MySQL database

Once connection (OnceConnection)

MysqlParameters.Builderbuilder =MysqlParameters.newBuilder()            .setHost("127.0.0.1")            .setName("test")            .setPort(3306)            .setUser("ray")            .setPassword("raypwd");try (DatabaseConnectionconnection =newOnceConnection(builder.build())) {// auto close connection.RecordCursorrecord =connection.createStatementExecutor().executeQuery("SELECT * FROM `test_table`");record          .stream()          .forEach(row -> {System.out.println("name = " +row.getInt("id") +row.getString("name"));              });    }catch (Exceptione) {e.printStackTrace();    }

Keep-alive connection and connection pool. (DatabaseConnections)

MysqlParameters.Builderbuilder =MysqlParameters.newBuilder()            .setHost("127.0.0.1")            .setName("test")            .setPort(3306)            .setUser("ray")            .setPassword("raypwd");DatabaseConnectionspool =newDatabaseConnections();intconnectionSize =5;pool.connectOriginalConnection(builder.build(),connectionSize);for (inti =0;i <connectionSize;i++) {RecordCursorrecord =pool.next().executeQuery("SELECT * FROM `test_table` LIMIT 1");longid =record.fieldLong("id");Stringname =record.field("name");System.out.println("id = " +id +", name" +name);    }

Connection to H2 database(in-memory mode)

Once connection (OnceConnection)

try (OnceConnectionconnection =newOnceConnection(newH2Parameters.Builder()                .inMemory()                .setName("databaseName")                .setProperties("DATABASE_TO_UPPER","false")                .setProperties("MODE","MYSQL")                .build())) {connection.connect();StatementExecutorexecutor =connection.createStatementExecutor();StringcreateTableSql ="CREATE TABLE `test_table` ("              +"  `id` bigint auto_increment,"              +"  `col_int` int(10) UNSIGNED NOT NULL,"              +"  `col_double` DOUBLE UNSIGNED DEFAULT NULL,"              +"  `col_boolean` BOOLEAN DEFAULT NULL ,"              +"  `col_tinyint` tinyint(1) NOT NULL ,"              +"  `col_enum` enum('default','enum1','enum2') DEFAULT NULL ,"              +"  `col_decimal` decimal(20,3) DEFAULT 0.000 ,"              +"  `col_varchar` varchar(50) NOT NULL ,"              +"  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),"              +"  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() "              +"ON UPDATE current_timestamp()"              +");";executor.executeUpdate(createTableSql);Stringsql ="INSERT INTO test_table SET col_int=1"              +",col_double=1.01"              +",col_boolean=true"              +",col_tinyint=5"              +",col_enum='enum1'"              +",col_decimal=1.1111"              +",col_varchar='col_varchar'"              +",created_at=NOW();";executor.executeUpdate(sql);RecordCursorrecord =executor.executeQuery("SELECT * FROM `test_table`");record          .stream()          .forEach(row -> {System.out.println("col_int=" +row.getInt("col_int"));              });executor.executeUpdate("DROP TABLE test_table");    }

The database uses the prepare statement.

try (OnceConnectionconnection =newOnceConnection(newH2Parameters.Builder()                .inMemory()                .setName("databaseName")                .setProperties("DATABASE_TO_UPPER","false")                .setProperties("MODE","MYSQL")                .build())) {connection.connect();StatementExecutorexecutor =connection.createStatementExecutor();StringcreateTableSql ="CREATE TABLE `test_table` ("              +"  `id` bigint auto_increment,"              +"  `age` int(10) UNSIGNED NOT NULL,"              +"  `name` VARCHAR(100) DEFAULT NULL"              +");";executor.executeUpdate(createTableSql);Stringsql ="INSERT INTO test_table SET age=?" +",name=?;";executor.prepareExecuteUpdate(Prepared.newBuilder(sql)              .set(1,ParameterValue.int64(18))              .set(2,ParameterValue.string("ray"))              .build());executor.prepareExecuteUpdate(Prepared.newBuilder(sql)              .set(1,ParameterValue.int64(22))              .set(2,ParameterValue.string("name test"))              .build());RecordCursorrecord =executor.prepareExecuteQuery(Prepared.newBuilder("SELECT * FROM `test_table` WHERE name = ? LIMIT 1;")                  .set(1,ParameterValue.string("ray"))                  .build());System.out.println("name=" +record.field("name") +", age=" +record.fieldInt("age"));executor.executeUpdate("DROP TABLE test_table");    }

Author

Ray Li - @raytw on GitHub, Kevin - @tsaibiido on GitHub

License

MIT. See theLICENSE file for details.

About

The library provides mysql (CRUD), socket server, logging.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp