🔖 java 操作 mysql 实例

pull/1/head
Zhang Peng 2018-03-13 15:20:02 +08:00
parent e38cc241ed
commit 9c4c6ba0de
4 changed files with 201 additions and 0 deletions

View File

@ -0,0 +1,79 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- [Part 1] BASIC SETTINGS BEGIN -->
<!-- MAVEN COORDINATE BEGIN -->
<artifactId>javadb-mysql</artifactId>
<packaging>jar</packaging>
<!-- MAVEN COORDINATE END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<parent>
<groupId>io.github.dunwu</groupId>
<artifactId>javadb</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<!-- db begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- db end -->
<!-- log start -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- RELATIONSHIP SETTINGS END -->
<!-- [Part 1] BASIC SETTINGS END -->
<!-- [Part 2] BUILD SETTINGS BEGIN -->
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
<!--<targetPath>/abc</targetPath>-->
</resource>
</resources>
</build>
<!-- [Part 2] BUILD SETTINGS END -->
<!-- [Part 3] PROJECT INFO BEGIN -->
<name>${project.artifactId}</name>
<description>Java 工具使用示例</description>
<!-- [Part 3] PROJECT INFO END -->
</project>

View File

@ -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();
}
}
}

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- logback中一共有5种有效级别分别是TRACE、DEBUG、INFO、WARN、ERROR优先级依次从低到高 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="FILE_NAME" value="javadb"/>
<!-- 将记录日志打印到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n</pattern>
</encoder>
</appender>
<!-- RollingFileAppender begin -->
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 根据时间来制定滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${user.dir}/logs/${FILE_NAME}-all.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<!-- 根据文件大小来制定滚动策略 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>30MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n</pattern>
</encoder>
</appender>
<!-- RollingFileAppender end -->
<!-- logger begin -->
<!-- 本项目的日志记录,分级打印 -->
<logger name="io.github.dunwu" level="TRACE">
<appender-ref ref="ALL"/>
</logger>
<root level="TRACE">
<appender-ref ref="STDOUT"/>
</root>
<!-- logger end -->
</configuration>

View File

@ -16,6 +16,7 @@
<!-- RELATIONSHIP SETTINGS BEGIN -->
<modules>
<module>redis</module>
<module>mysql</module>
</modules>
<!-- RELATIONSHIP SETTINGS END -->