From 9c4c6ba0de5e868f65b3bc12cc7d4cefaf5777bc Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Tue, 13 Mar 2018 15:20:02 +0800 Subject: [PATCH] =?UTF-8?q?:bookmark:=20java=20=E6=93=8D=E4=BD=9C=20mysql?= =?UTF-8?q?=20=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/javadb/mysql/pom.xml | 79 +++++++++++++++++++ .../io/github/dunwu/javadb/MysqlDemoTest.java | 76 ++++++++++++++++++ .../mysql/src/test/resources/logback.xml | 45 +++++++++++ codes/javadb/pom.xml | 1 + 4 files changed, 201 insertions(+) create mode 100644 codes/javadb/mysql/pom.xml create mode 100644 codes/javadb/mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java create mode 100644 codes/javadb/mysql/src/test/resources/logback.xml diff --git a/codes/javadb/mysql/pom.xml b/codes/javadb/mysql/pom.xml new file mode 100644 index 0000000..c3729bf --- /dev/null +++ b/codes/javadb/mysql/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + + + + + javadb-mysql + jar + + + + + io.github.dunwu + javadb + 1.0.0 + + + + + mysql + mysql-connector-java + 5.1.45 + + + org.apache.commons + commons-pool2 + 2.5.0 + + + + + + ch.qos.logback + logback-classic + + + + + org.springframework + spring-context-support + + + org.springframework + spring-test + test + + + + + + + + + + ${project.artifactId} + + + true + src/main/resources + + logback.xml + + + + + + + + + + ${project.artifactId} + Java 工具使用示例 + + + + diff --git a/codes/javadb/mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java b/codes/javadb/mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java new file mode 100644 index 0000000..20022da --- /dev/null +++ b/codes/javadb/mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java @@ -0,0 +1,76 @@ +package io.github.dunwu.javadb; + +import java.sql.Connection; +import java.sql.Date; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Mysql 测试例 + * + * @author Zhang Peng + * @see https://dev.mysql.com/doc/connector-j/5.1/en/ + */ +public class MysqlDemoTest { + + private static Logger logger = LoggerFactory.getLogger(MysqlDemoTest.class); + + private static final String DB_HOST = "localhost"; + private static final String DB_PORT = "3306"; + private static final String DB_SCHEMA = "sakila"; + private static final String DB_USER = "root"; + private static final String DB_PASSWORD = "root"; + private static Statement statement; + private static Connection connection; + + @BeforeClass + public static void beforeClass() { + try { + final String DB_URL = String.format("jdbc:mysql://%s:%s/%s", DB_HOST, DB_PORT, DB_SCHEMA); + connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); + // connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?" + "user=root&password=root"); + statement = connection.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void afterClass() { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Test + public void testString() { + final String sql = "select * from actor limit 10"; + try { + ResultSet rs = statement.executeQuery(sql); + // 展开结果集数据库 + while (rs.next()) { + // 通过字段检索 + int id = rs.getInt("actor_id"); + String firstName = rs.getString("first_name"); + String lastName = rs.getString("last_name"); + Date lastUpdate = rs.getDate("last_update"); + // 输出数据 + logger.debug("actor_id: {}, first_name: {}, last_name: {}, last_update: {}", id, firstName, lastName, + lastUpdate.toLocalDate()); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/codes/javadb/mysql/src/test/resources/logback.xml b/codes/javadb/mysql/src/test/resources/logback.xml new file mode 100644 index 0000000..143ac56 --- /dev/null +++ b/codes/javadb/mysql/src/test/resources/logback.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n + + + + + + + + ${user.dir}/logs/${FILE_NAME}-all.%d{yyyy-MM-dd}.log + 30 + + + + + 30MB + + + + %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n + + + + + + + + + + + + + + + + diff --git a/codes/javadb/pom.xml b/codes/javadb/pom.xml index 42a9019..e682895 100644 --- a/codes/javadb/pom.xml +++ b/codes/javadb/pom.xml @@ -16,6 +16,7 @@ redis + mysql