work on db connection basic
This commit is contained in:
parent
90d9401203
commit
1142def290
7
pom.xml
7
pom.xml
@ -49,6 +49,13 @@
|
|||||||
<version>7.0.0</version>
|
<version>7.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-dbutils</groupId>
|
||||||
|
<artifactId>commons-dbutils</artifactId>
|
||||||
|
<version>1.8.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
|
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mariadb.jdbc</groupId>
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
|
|||||||
@ -1,6 +1,15 @@
|
|||||||
package dev.mduchene;
|
package dev.mduchene;
|
||||||
|
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
|
import org.apache.commons.dbutils.DbUtils;
|
||||||
|
import org.apache.commons.dbutils.QueryRunner;
|
||||||
|
import org.apache.commons.dbutils.handlers.ScalarHandler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** Hello world! */
|
/** Hello world! */
|
||||||
public class App {
|
public class App {
|
||||||
@ -12,6 +21,58 @@ public class App {
|
|||||||
.password("root")
|
.password("root")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
var app = Javalin.create(cnf -> {}).get("/", ctx -> ctx.result("Hello World")).start(3001);
|
db.init();
|
||||||
|
|
||||||
|
var app = Javalin.create(cnf -> {});
|
||||||
|
app.before(
|
||||||
|
ctx -> {
|
||||||
|
Connection connection = db.getConnection();
|
||||||
|
ctx.attribute("connection", connection);
|
||||||
|
});
|
||||||
|
app.after(
|
||||||
|
ctx -> {
|
||||||
|
Connection connection = ctx.attribute("connection");
|
||||||
|
if (connection != null) {
|
||||||
|
DbUtils.commitAndCloseQuietly(connection);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.exception(
|
||||||
|
Exception.class,
|
||||||
|
(e, ctx) -> {
|
||||||
|
LoggerFactory.getLogger(App.class).error("error", e);
|
||||||
|
|
||||||
|
Connection connection = ctx.attribute("connection");
|
||||||
|
if (connection != null) {
|
||||||
|
DbUtils.rollbackAndCloseQuietly(connection);
|
||||||
|
}
|
||||||
|
ctx.status(500);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
"/",
|
||||||
|
ctx -> {
|
||||||
|
var r = new QueryRunner();
|
||||||
|
Integer query =
|
||||||
|
r.query(
|
||||||
|
(Connection) ctx.attribute("connection"),
|
||||||
|
"SELECT 1",
|
||||||
|
resultSet -> {
|
||||||
|
System.out.println("hello world");
|
||||||
|
if (resultSet.next()) return resultSet.getInt(1);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.result(query.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
"/error",
|
||||||
|
ctx -> {
|
||||||
|
throw new RuntimeException("error");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.start(3001);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,13 +2,19 @@ package dev.mduchene;
|
|||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class Db {
|
public class Db {
|
||||||
private HikariDataSource dataSource;
|
private HikariDataSource dataSource;
|
||||||
private Db() {}
|
private Db() {}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String url;
|
private String url;
|
||||||
private String user;
|
private String user;
|
||||||
@ -54,4 +60,5 @@ public class Db {
|
|||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user