🔖 数据库示例

pull/1/head
Zhang Peng 2018-06-15 17:33:16 +08:00
parent e2af003534
commit ed860415c9
18 changed files with 230 additions and 339 deletions

View File

@ -0,0 +1,3 @@
# javadb
> 本目录中存放在 Java 中操作各数据库的示例(未使用 ORM

View File

@ -0,0 +1,113 @@
<?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>
<groupId>io.github.dunwu</groupId>
<artifactId>javadb-mysql</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring.version>4.3.13.RELEASE</spring.version>
<logback.version>1.2.3</logback.version>
<junit.version>4.12</junit.version>
</properties>
<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 -->
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring end -->
<!-- test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- test end -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- database begin -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- database end -->
<!-- log begin -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-parent</artifactId>
<version>${logback.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- log end -->
<!-- test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- test end -->
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</resources>
</build>
</project>

View File

@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
/** /**
* Mysql * Mysql
*
* @author Zhang Peng * @author Zhang Peng
* @see https://dev.mysql.com/doc/connector-j/5.1/en/ * @see https://dev.mysql.com/doc/connector-j/5.1/en/
*/ */
@ -35,7 +34,8 @@ public class MysqlDemoTest {
try { try {
final String DB_URL = String.format("jdbc:mysql://%s:%s/%s", DB_HOST, DB_PORT, DB_SCHEMA); 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(DB_URL, DB_USER, DB_PASSWORD);
// connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?" + "user=root&password=root"); // connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?" +
// "user=root&password=root");
statement = connection.createStatement(); statement = connection.createStatement();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -0,0 +1,108 @@
<?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>
<groupId>io.github.dunwu</groupId>
<artifactId>javadb-redis-jedis</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring.version>4.3.13.RELEASE</spring.version>
<logback.version>1.2.3</logback.version>
<jedis.version>2.9.0</jedis.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- database begin -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- database end -->
<!-- log start -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- log end -->
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring end -->
<!-- test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- test end -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- database begin -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- database end -->
<!-- log begin -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-parent</artifactId>
<version>${logback.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- log end -->
<!-- test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- test end -->
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</resources>
</build>
</project>

View File

@ -3,24 +3,22 @@ package io.github.dunwu.javadb;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
/** /**
* Jedis * Jedis
* @see https://github.com/xetorthio/jedis
* @author Zhang Peng * @author Zhang Peng
* @see https://github.com/xetorthio/jedis
*/ */
public class JedisDemoTest { public class JedisDemoTest {
private static final String REDIS_HOST = "192.168.58.170"; private static final String REDIS_HOST = "192.168.28.32";
private static final int REDIS_PORT = 6379; private static final int REDIS_PORT = 6379;
private static Jedis jedis = null; private static Jedis jedis = null;
private static Logger logger = LoggerFactory.getLogger(JedisDemoTest.class); private static Logger logger = LoggerFactory.getLogger(JedisDemoTest.class);

View File

@ -16,7 +16,7 @@
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 根据时间来制定滚动策略 --> <!-- 根据时间来制定滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${user.dir}/logs/${FILE_NAME}-all.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${user.dir}/logs/${FILE_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory> <maxHistory>30</maxHistory>
</rollingPolicy> </rollingPolicy>

View File

@ -1,5 +1,5 @@
redis.name=redis-default redis.name=redis-default
redis.host=192.168.58.170 redis.host=192.168.28.32
redis.port=6379 redis.port=6379
redis.timeout=3000 redis.timeout=3000
redis.password=zp redis.password=zp

View File

@ -1,79 +0,0 @@
<?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

@ -1,110 +0,0 @@
<?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 -->
<groupId>io.github.dunwu</groupId>
<artifactId>javadb</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<!-- MAVEN COORDINATE END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<modules>
<module>redis</module>
<module>mysql</module>
</modules>
<!-- RELATIONSHIP SETTINGS END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring end -->
<!-- log start -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-parent</artifactId>
<version>${logback.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<!-- log end -->
<!-- javaee begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- javaee end -->
</dependencies>
</dependencyManagement>
<!-- RELATIONSHIP SETTINGS END -->
<!-- PROPERTIES BEGIN -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring.version>4.3.13.RELEASE</spring.version>
<logback.version>1.2.3</logback.version>
</properties>
<!-- PROPERTIES END -->
<!-- [Part 1] BASIC SETTINGS END -->
<!-- [Part 2] BUILD SETTINGS BEGIN -->
<build>
<finalName>${project.artifactId}</finalName>
</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

@ -1,74 +0,0 @@
<?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-redis</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>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.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

@ -1,68 +0,0 @@
/*
sql Mysql
*/
-- --------------------------------------------------------------
-- DDL BEGIN
-- --------------------------------------------------------------
-- NOT NULL 约束
DROP TABLE IF EXISTS person;
CREATE TABLE person (
person_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
age INT,
address VARCHAR(255)
);
-- UNIQUE 约束
DROP TABLE IF EXISTS person;
CREATE TABLE person (
person_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
age INT,
address VARCHAR(255),
UNIQUE (person_id)
);
-- PRIMARY KEY 约束
DROP TABLE IF EXISTS person;
CREATE TABLE person (
person_id INT NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT,
address VARCHAR(255)
);
-- FOREIGN KEY 约束
DROP TABLE IF EXISTS bill;
CREATE TABLE bill
(
bill_id INT NOT NULL,
bill_no INT NOT NULL,
person_id INT,
PRIMARY KEY (bill_id),
FOREIGN KEY (person_id) REFERENCES person(person_id)
);
-- CHECK 约束
DROP TABLE IF EXISTS person;
CREATE TABLE person (
person_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
age INT CHECK (age >= 18),
address VARCHAR(255)
);
-- DEFAULT 约束
DROP TABLE IF EXISTS person;
CREATE TABLE person (
person_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
age INT DEFAULT 18,
address VARCHAR(255)
);
-- --------------------------------------------------------------
-- DDL END
-- --------------------------------------------------------------