Compare commits
No commits in common. "master" and "dockerAPI" have entirely different histories.
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>test</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/test/java=UTF-8
|
||||||
|
encoding/<project>=UTF-8
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.5
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
use bookstore;
|
|
||||||
create table book(
|
|
||||||
id int(11) not NULL,
|
|
||||||
title VARCHAR(255),
|
|
||||||
author VARCHAR(255),
|
|
||||||
publisher VARCHAR(255),
|
|
||||||
publishYear int(11)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
Insert into book (id,title,author,publisher,publishYear) values
|
|
||||||
(1002,'平凡的世界', '路遥','新世界出版社',2011),
|
|
||||||
(1026,'活着', '余华','东方出版社',2016),
|
|
||||||
(1204,'围城', '钱钟书','人名文学出版社',1991),
|
|
||||||
(1208,'围城', '钱钟书','文艺出版社',2012);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
use bookstore;
|
|
||||||
create table book(
|
|
||||||
id int(11) not NULL,
|
|
||||||
title VARCHAR(255),
|
|
||||||
author VARCHAR(255),
|
|
||||||
publisher VARCHAR(255),
|
|
||||||
publishYear int(11)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
Insert into book (id,title,author,publisher,publishYear) values
|
|
||||||
(1002,'平凡的世界', '路遥','新世界出版社',2011),
|
|
||||||
(1026,'活着', '余华','东方出版社',2016),
|
|
||||||
(1204,'围城', '钱钟书','人名文学出版社',1991),
|
|
||||||
(1208,'围城', '钱钟书','文艺出版社',2012),
|
|
||||||
(1004,'平凡的世界', '路遥','文艺出版社',2012),
|
|
||||||
(1010,'人生', '路遥','文艺出版社',2005),
|
|
||||||
(1024,'活着', '余华','作家出版社',2012);
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
use bookstore;
|
|
||||||
create table book(
|
|
||||||
id int(11) not NULL,
|
|
||||||
title VARCHAR(255),
|
|
||||||
author VARCHAR(255),
|
|
||||||
publisher VARCHAR(255),
|
|
||||||
publishYear int(11)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
Insert into book (id,title,author,publisher,publishYear) values
|
|
||||||
(1002,'平凡的世界', '路遥','新世界出版社',2011),
|
|
||||||
(1204,'围城', '钱钟书','人名文学出版社',1991),
|
|
||||||
(1208,'围城', '钱钟书','文艺出版社',2012),
|
|
||||||
(1004,'平凡的世界', '路遥','文艺出版社',2012),
|
|
||||||
(1010,'人生', '路遥','文艺出版社',2005);
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
use bookstore;
|
|
||||||
create table book(
|
|
||||||
id int(11) not NULL,
|
|
||||||
title VARCHAR(255),
|
|
||||||
author VARCHAR(255),
|
|
||||||
publisher VARCHAR(255),
|
|
||||||
publishYear int(11)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
Insert into book (id,title,author,publisher,publishYear) values
|
|
||||||
(1002,'平凡的世界', '路遥','新世界出版社',2011),
|
|
||||||
(1204,'围城', '钱钟书','人名文学出版社',1991),
|
|
||||||
(1208,'围城', '钱钟书','文艺出版社',2012),
|
|
||||||
(1004,'平凡的世界', '路遥','文艺出版社',2012),
|
|
||||||
(1010,'人生', '路遥','文艺出版社',2005);
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
drop database if exists bookstore;
|
|
||||||
create database bookstore;
|
|
||||||
use bookstore;
|
|
||||||
create table step7(
|
|
||||||
id int(11) not NULL,
|
|
||||||
title VARCHAR(255),
|
|
||||||
author VARCHAR(255),
|
|
||||||
publisher VARCHAR(255),
|
|
||||||
publishYear int(11)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
Insert into step7 (id,title,author,publisher,publishYear) values
|
|
||||||
(1002,'平凡的世界', null,'新世界出版社',null),
|
|
||||||
(1208,null, '钱钟书',null,null);
|
|
||||||
create table transformedBook(
|
|
||||||
id int(11),
|
|
||||||
column_name VARCHAR(255),
|
|
||||||
value VARCHAR(255)
|
|
||||||
)DEFAULT CHARSET=utf8;
|
|
||||||
BIN
lib/ant.jar
BIN
lib/ant.jar
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,46 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>test</groupId>
|
||||||
|
<artifactId>test</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>test</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-client -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.fabric8</groupId>
|
||||||
|
<artifactId>kubernetes-client</artifactId>
|
||||||
|
<version>2.6.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.spotify/docker-client -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spotify</groupId>
|
||||||
|
<artifactId>docker-client</artifactId>
|
||||||
|
<version>8.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.docker-java</groupId>
|
||||||
|
<artifactId>docker-java</artifactId>
|
||||||
|
<version>3.0.13</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.docker.api;
|
||||||
|
/**
|
||||||
|
* 获取系统CPU使用率,以监控整个集群的资源使用状况
|
||||||
|
* @author kevin
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CpuStats {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.docker.api;
|
||||||
|
/**
|
||||||
|
* 提供docker的各种接口供资源监控使用
|
||||||
|
* @author kevin
|
||||||
|
*/
|
||||||
|
public class DockerClient {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.docker.api;
|
||||||
|
/**
|
||||||
|
* 获取整个集群的健康状态,如平均编译运行时间、剩余内存及CPU
|
||||||
|
* @author kevin
|
||||||
|
*/
|
||||||
|
public class HealthStats {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.docker.api;
|
||||||
|
/**
|
||||||
|
* 提供集群的各种信息如节点数量和地址,供资源监控使用
|
||||||
|
* @author kevin
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class KubernetesClient {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.docker.api;
|
||||||
|
/**
|
||||||
|
* 获取整个集群的内存使用状态
|
||||||
|
* @author kevin
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MemoryStats {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,178 @@
|
||||||
|
package test.test;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.glassfish.jersey.internal.util.collection.StringIgnoreCaseKeyComparator;
|
||||||
|
|
||||||
|
import ch.qos.logback.core.net.server.Client;
|
||||||
|
import io.fabric8.kubernetes.api.model.Container;
|
||||||
|
import io.fabric8.kubernetes.api.model.ContainerBuilder;
|
||||||
|
import io.fabric8.kubernetes.api.model.KubernetesResource;
|
||||||
|
import io.fabric8.kubernetes.api.model.Namespace;
|
||||||
|
import io.fabric8.kubernetes.api.model.Node;
|
||||||
|
import io.fabric8.kubernetes.api.model.NodeAddress;
|
||||||
|
import io.fabric8.kubernetes.api.model.ObjectMeta;
|
||||||
|
import io.fabric8.kubernetes.api.model.Pod;
|
||||||
|
import io.fabric8.kubernetes.api.model.PodBuilder;
|
||||||
|
import io.fabric8.kubernetes.api.model.PodSpec;
|
||||||
|
import io.fabric8.kubernetes.client.Config;
|
||||||
|
import io.fabric8.kubernetes.client.ConfigBuilder;
|
||||||
|
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
|
||||||
|
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||||
|
import io.fabric8.kubernetes.client.KubernetesClientException;
|
||||||
|
import io.fabric8.kubernetes.client.Watcher;
|
||||||
|
import io.fabric8.kubernetes.client.utils.Utils;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hello world!
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
Config config = new ConfigBuilder().
|
||||||
|
withMasterUrl("106.75.96.108:6443").
|
||||||
|
withNamespace("default").
|
||||||
|
withTrustCerts(true).
|
||||||
|
// withOauthToken("c7dd73a7c86992fb").
|
||||||
|
withUsername("testk8s").
|
||||||
|
withPassword("testk8s").
|
||||||
|
build();
|
||||||
|
// Pod pod = new PodBuilder().withKind("ReplictioController").withSpec(spec);
|
||||||
|
|
||||||
|
// ObjectMeta objectMeta;
|
||||||
|
PodSpec podSpec;
|
||||||
|
Container container = new ContainerBuilder().w
|
||||||
|
|
||||||
|
KubernetesClient client = new DefaultKubernetesClient(config);
|
||||||
|
|
||||||
|
client.pods();
|
||||||
|
//client.pods()
|
||||||
|
client.pods().list().getItems().get(0).getSpec();
|
||||||
|
//NodeAddress
|
||||||
|
List<NodeAddress> address = client.nodes().list().getItems().get(0).getStatus().getAddresses();
|
||||||
|
System.out.println(address.get(0).getAddress());
|
||||||
|
client.resource("pod").get();
|
||||||
|
|
||||||
|
|
||||||
|
// KubernetesResource<HasMetadata> kubernetesResource =
|
||||||
|
|
||||||
|
// List<Namespace> nameSpaceList =client.namespaces().list().getItems();
|
||||||
|
|
||||||
|
// List<Pod> pods = client.pods().list().getItems();
|
||||||
|
|
||||||
|
// testDeletePod(client, "docker-evaluate-1341-r8zz5");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// //假设我可以通过Pod查看容器的名字
|
||||||
|
// Container container = client.pods().withName("").get().getSpec().getContainers().get(0);
|
||||||
|
// //那么也就可以使用docker stat --nostream 查看容器的状态
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// container.getResources();
|
||||||
|
// container.get
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// watch(client,"docker-evaluate-1335-8gmsj");
|
||||||
|
//
|
||||||
|
// testDeletePod(client,"docker-evaluate-1335-8gmsj");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// System.out.println(pods.get(0).getMetadata().getName());
|
||||||
|
|
||||||
|
// System.out.println(nameSpaceList.size());
|
||||||
|
|
||||||
|
// testCreateNamespace(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void testCreateNamespace(KubernetesClient kubernetesClient){
|
||||||
|
|
||||||
|
Namespace ns = new Namespace();
|
||||||
|
|
||||||
|
ns.setApiVersion("v1");
|
||||||
|
ns.setKind("Namespace");
|
||||||
|
|
||||||
|
ObjectMeta om = new ObjectMeta();
|
||||||
|
om.setName("ns-fabric8");
|
||||||
|
|
||||||
|
ns.setMetadata(om);
|
||||||
|
|
||||||
|
kubernetesClient.namespaces().create(ns);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// public static void testCreatePod(KubernetesClient kubernetesClient){
|
||||||
|
//
|
||||||
|
// Pod pod = new Pod();
|
||||||
|
//
|
||||||
|
//// pod.setApiVersion("v1");
|
||||||
|
//// pod.setKind("ReplicationController");
|
||||||
|
//
|
||||||
|
// //设置pod的meta数据
|
||||||
|
// ObjectMeta om = new ObjectMeta();
|
||||||
|
// om.setName("webssh-master-wrmtest");
|
||||||
|
// Map<String,String> map = new HashMap<String,String>();
|
||||||
|
//
|
||||||
|
// pod.setMetadata(om);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// PodSpec spec =new PodSpec();
|
||||||
|
// spec.setNodeSelector(map);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// List<Container> containerList = new ArrayList<Container>();
|
||||||
|
// Container container = new Container();
|
||||||
|
// container.setName("webssh");
|
||||||
|
// container.setImage("dind-ssh:v1.0");
|
||||||
|
//
|
||||||
|
// SecurityContext securityContext = new SecurityContext();
|
||||||
|
// securityContext.setPrivileged(true);
|
||||||
|
// container.setSecurityContext(securityContext);
|
||||||
|
//
|
||||||
|
// spec.setContainers(containerList);
|
||||||
|
//
|
||||||
|
// pod.setSpec(spec);
|
||||||
|
//
|
||||||
|
// kubernetesClient.pods().create(pod);
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
public static void testDeletePod(KubernetesClient kubernetesClient,String podName){
|
||||||
|
|
||||||
|
kubernetesClient.pods().withName(podName).delete();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void watch(KubernetesClient client,String podName) {
|
||||||
|
client.pods().withName(podName).watch(new Watcher<Pod>() {
|
||||||
|
|
||||||
|
public void eventReceived(Action action, Pod resource) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.out.println(action+":"+resource);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onClose(KubernetesClientException cause) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.out.println("closed"+cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
package test.test;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
import com.spotify.docker.client.DefaultDockerClient;
|
||||||
|
import com.spotify.docker.client.DockerClient;
|
||||||
|
import com.spotify.docker.client.DockerClient.ListContainersParam;
|
||||||
|
import com.spotify.docker.client.auth.FixedRegistryAuthSupplier;
|
||||||
|
import com.spotify.docker.client.exceptions.DockerException;
|
||||||
|
import com.spotify.docker.client.messages.Container;
|
||||||
|
import com.spotify.docker.client.messages.ContainerState;
|
||||||
|
import com.spotify.docker.client.messages.ContainerStats;
|
||||||
|
import com.spotify.docker.client.messages.CpuStats;
|
||||||
|
import com.spotify.docker.client.messages.MemoryStats;
|
||||||
|
import com.spotify.docker.client.messages.RegistryAuth;
|
||||||
|
|
||||||
|
import io.netty.util.internal.SystemPropertyUtil;
|
||||||
|
import sun.audio.*;
|
||||||
|
|
||||||
|
public class DockerApiTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws DockerException, InterruptedException{
|
||||||
|
// RegistryAuth registr = RegistryAuth.builder().serverAddress("http://106.75.95.215:4243/v1")
|
||||||
|
// .username("educoder")
|
||||||
|
// .password("educoder")
|
||||||
|
// .email("lqk_nudt@163.com")
|
||||||
|
// .build();
|
||||||
|
|
||||||
|
//172.17.0.1:2376
|
||||||
|
//10-9-186-111
|
||||||
|
final DockerClient docker = DefaultDockerClient.builder()
|
||||||
|
.uri(URI.create("http://106.75.96.108:4243"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ListContainersParam param = new ListContainersParam("namespace", "default");
|
||||||
|
Container container = docker.listContainers(param).get(0);
|
||||||
|
String containerId = container.id();
|
||||||
|
MemoryStats memoryStats = docker.stats(containerId).memoryStats();
|
||||||
|
CpuStats cpuStats = docker.stats(containerId).cpuStats();
|
||||||
|
|
||||||
|
Long systemUsage = cpuStats.systemCpuUsage();
|
||||||
|
Long total = cpuStats.cpuUsage().totalUsage();
|
||||||
|
System.out.println("containerId: " + containerId);
|
||||||
|
System.out.println("systemUsage:" + systemUsage);
|
||||||
|
System.out.println("total:" + total);
|
||||||
|
// Long perCpu = cpuStats.cpuUsage().percpuUsage();
|
||||||
|
// System.out.println("CpuStats:" + cpuStats.cpuUsage() );
|
||||||
|
// System.out.println(memoryStats.usage().intValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
package test.test;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.github.dockerjava.api.DockerClient;
|
||||||
|
import com.github.dockerjava.api.async.ResultCallback;
|
||||||
|
import com.github.dockerjava.api.model.Identifier;
|
||||||
|
import com.github.dockerjava.api.model.PushResponseItem;
|
||||||
|
import com.github.dockerjava.api.model.Repository;
|
||||||
|
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||||
|
import com.github.dockerjava.core.DockerClientBuilder;
|
||||||
|
import com.github.dockerjava.core.DockerClientConfig;
|
||||||
|
|
||||||
|
public class DockerJava {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
DockerJava dockerJava = new DockerJava();
|
||||||
|
DockerClient dockerClient = dockerJava.getDockerClient("tcp://106.75.96.108:4243", "http://106.75.95.215:5000/v2/");
|
||||||
|
// System.out.println(dockerJava.getImageID(dockerClient, "gcc", "latest"));
|
||||||
|
dockerJava.pushToRegistry(dockerClient, "busybox", "latest");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public DockerClient getDockerClient(String host, String RegistryUrl)
|
||||||
|
{
|
||||||
|
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||||
|
.withDockerHost(host)
|
||||||
|
.withDockerTlsVerify(false)
|
||||||
|
.withApiVersion("1.23")
|
||||||
|
.withRegistryUrl(RegistryUrl)
|
||||||
|
.build();
|
||||||
|
DockerClient docker = DockerClientBuilder.getInstance(config).build();
|
||||||
|
return docker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getImageID(DockerClient docker, String imageName, String tag)
|
||||||
|
{
|
||||||
|
return docker.inspectImageCmd(imageName + ":" + tag).exec().getId();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushToRegistry(DockerClient docker, String imageName, String imageTag)
|
||||||
|
{
|
||||||
|
PushResponseItem responseItem = new PushResponseItem();
|
||||||
|
|
||||||
|
// Repository repository = new Repository(imageName);
|
||||||
|
// Identifier identifier = new Identifier(repository, imageTag);
|
||||||
|
docker.pushImageCmd(imageName + ":" + imageTag).exec(resultCallback);
|
||||||
|
|
||||||
|
//docker.pushImageCmd(imageName + ":" + imageTag).exec(resultCallback)
|
||||||
|
}
|
||||||
|
ResultCallback<PushResponseItem> resultCallback = new ResultCallback<PushResponseItem>() {
|
||||||
|
public void close() throws IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.exit(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStart(Closeable closeable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.exit(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onError(Throwable throwable) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.exit(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onComplete() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.out.println("已经完成");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onNext(PushResponseItem object) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
System.out.println(object.getProgressDetail());
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,83 +0,0 @@
|
||||||
package step1;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
public class MySQLWithJDBC {
|
|
||||||
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "create database " + databaseName;
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//创建数据库实例
|
|
||||||
public void dropDatabase(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "drop database if exists " + databaseName;
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取数据库中已经存在的数据库实例
|
|
||||||
public ResultSet getExistDB(Connection connection, String databaseName) throws SQLException
|
|
||||||
{
|
|
||||||
ResultSet resultSet = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME=\"" + databaseName + "\"";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
resultSet = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return resultSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
package step1;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class MySQLWithJDBCTest {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws SQLException {
|
|
||||||
|
|
||||||
MySQLWithJDBC mysql = new MySQLWithJDBC();
|
|
||||||
|
|
||||||
String databaseName = "";
|
|
||||||
|
|
||||||
//从命令行接收输入获得数据库的名字
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
databaseName = sc.next();
|
|
||||||
|
|
||||||
//建立数据库连接
|
|
||||||
Connection connection = mysql.getConnection(mysql.JDBC_DRIVER, mysql.DB_URL, mysql.USER, mysql.PASS);
|
|
||||||
mysql.dropDatabase(connection,databaseName);
|
|
||||||
|
|
||||||
//执行创建数据库的命令
|
|
||||||
mysql.createDatabaseInstance(connection, databaseName);
|
|
||||||
|
|
||||||
//查询数据库验证是否成功创建
|
|
||||||
ResultSet result = mysql.getExistDB(connection, databaseName);
|
|
||||||
|
|
||||||
//获取查询结果
|
|
||||||
while(result.next())
|
|
||||||
{
|
|
||||||
String dbName = result.getString("SCHEMA_NAME");
|
|
||||||
System.out.print("databaseName:" + dbName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,108 +0,0 @@
|
||||||
package step2;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CreateTable {
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//在指定数据库实例中创建有tableName命名的表
|
|
||||||
public void createTable(Connection connection, String tableName,String[][] tableInfo){
|
|
||||||
//请在此处实现创建表的操作
|
|
||||||
String sqlScript = "create table " + tableName + "(\n";
|
|
||||||
Statement stmt = null;
|
|
||||||
for(int k=0;k<tableInfo.length;k++)
|
|
||||||
{
|
|
||||||
if(tableInfo[k][0].equals(""))
|
|
||||||
break;
|
|
||||||
System.out.println(tableInfo[k][0] + tableInfo[k][1]);
|
|
||||||
sqlScript = sqlScript + tableInfo[k][0] + " " + tableInfo[k][1] + ",\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlScript = sqlScript.substring(0, sqlScript.length()-2);
|
|
||||||
sqlScript = sqlScript + ")";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResultSet getExistTable(Connection conn,String databaseName,String tableName) {
|
|
||||||
ResultSet resultSet = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
/*select TABLE_NAME from INFORMATION_SCHEMA.TABLES whereTABLE_SCHEMA='dbname' and TABLE_NAME='tablename' ;*/
|
|
||||||
String sqlScript = "select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=\'" +databaseName +
|
|
||||||
"\' and TABLE_NAME=\'" + tableName + "\'";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) conn.createStatement();
|
|
||||||
resultSet = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return resultSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> loadSql(String sqlFile) throws Exception {
|
|
||||||
List<String> sqlList = new ArrayList<String>();
|
|
||||||
try {
|
|
||||||
InputStream sqlFileIn = new FileInputStream(sqlFile);
|
|
||||||
StringBuffer sqlSb = new StringBuffer();
|
|
||||||
byte[] buff = new byte[1024];
|
|
||||||
int byteRead = 0;
|
|
||||||
while ((byteRead = sqlFileIn.read(buff)) != -1) {
|
|
||||||
sqlSb.append(new String(buff, 0, byteRead));
|
|
||||||
} // Windows 下换行是 \r\n, Linux 下是 \n
|
|
||||||
|
|
||||||
String[] sqlArr = sqlSb.toString().split("(;\\s*\\r\\n)(;\\s*\\n)");
|
|
||||||
for (int i = 0; i < sqlArr.length; i++) {
|
|
||||||
String sql = sqlArr[i].replaceAll("--.*", "").trim();
|
|
||||||
|
|
||||||
if (!sql.equals("")) {
|
|
||||||
sqlList.add(sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sqlList;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new Exception(ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,102 +0,0 @@
|
||||||
package step2;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.apache.tools.ant.Project;
|
|
||||||
import org.apache.tools.ant.taskdefs.SQLExec;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class CreateTableTest {
|
|
||||||
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String tableName =sc.nextLine().trim();
|
|
||||||
int fieldNum = Integer.parseInt(sc.nextLine().trim());
|
|
||||||
String[][]TableInfo = initTableInfo(fieldNum);
|
|
||||||
|
|
||||||
int arrayIndex=0;
|
|
||||||
String fieldName="";
|
|
||||||
String prop="";
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
String input = sc.nextLine();
|
|
||||||
if(input.equals(""))
|
|
||||||
break;
|
|
||||||
|
|
||||||
String[]info = input.split(" ");
|
|
||||||
if(info.length != 2)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//字段名
|
|
||||||
fieldName = info[0];
|
|
||||||
prop = info[1];
|
|
||||||
|
|
||||||
TableInfo[arrayIndex][0] = fieldName;
|
|
||||||
TableInfo[arrayIndex][1] = prop;
|
|
||||||
arrayIndex = arrayIndex + 1;
|
|
||||||
if(arrayIndex > fieldNum-1)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
CreateTable createTable = new CreateTable();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 执行初始化脚本(F:\test.sql)
|
|
||||||
* 执行初始化脚连接数据库时不需要指定数据库
|
|
||||||
*/
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge2.sql";
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(createTable.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(createTable.USER);
|
|
||||||
sqlExec.setPassword(createTable.PASS);
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//将连接定位到bookStore数据库
|
|
||||||
Connection connection = createTable.getConnection(createTable.JDBC_DRIVER, createTable.DB_URL, createTable.USER, createTable.PASS);
|
|
||||||
createTable.createTable(connection, tableName,TableInfo);
|
|
||||||
|
|
||||||
//再次查询数据库,检查是否成功创建指定的表
|
|
||||||
ResultSet result = createTable.getExistTable(connection,"bookStore",tableName);
|
|
||||||
|
|
||||||
while(result.next())
|
|
||||||
{
|
|
||||||
String table = result.getString("TABLE_NAME");
|
|
||||||
System.out.print("table:"+table);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String[][] initTableInfo(int rowNum)
|
|
||||||
{
|
|
||||||
String[][] tmp = new String[rowNum][2];
|
|
||||||
for(int i=0;i<rowNum;i++)
|
|
||||||
{
|
|
||||||
for(int j=0;j<2;j++)
|
|
||||||
tmp[i][j] = "";
|
|
||||||
}
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,115 +0,0 @@
|
||||||
package step3;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class InsertTable {
|
|
||||||
|
|
||||||
public void InsertRecord(Connection connection, int id, String title, String author, String publisher, int year){
|
|
||||||
//在此添加插入数据的操作
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "Insert into book(id,title,author,publisher,publishYear) values" + "(" + id +"," +
|
|
||||||
"\'" + title+ "\',\'" + author + "\',\'" + publisher + "\',\'" + year + "\')";
|
|
||||||
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResultSet queryDB(Connection connection,String tableName) {
|
|
||||||
ResultSet result = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "select * from " + tableName + " order by id desc";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
List<String> loadSql(String sqlFile) throws Exception {
|
|
||||||
List sqlList = new ArrayList();
|
|
||||||
try {
|
|
||||||
InputStream sqlFileIn = new FileInputStream(sqlFile);
|
|
||||||
StringBuffer sqlSb = new StringBuffer();
|
|
||||||
byte[] buff = new byte[1024];
|
|
||||||
int byteRead = 0;
|
|
||||||
while ((byteRead = sqlFileIn.read(buff)) != -1) {
|
|
||||||
sqlSb.append(new String(buff, 0, byteRead));
|
|
||||||
} // Windows 下换行是 \r\n, Linux 下是 \n
|
|
||||||
|
|
||||||
String[] sqlArr = sqlSb.toString().split("(;\\s*\\r\\n)(;\\s*\\n)");
|
|
||||||
for (int i = 0; i < sqlArr.length; i++) {
|
|
||||||
String sql = sqlArr[i].replaceAll("--.*", "").trim();
|
|
||||||
|
|
||||||
if (!sql.equals("")) {
|
|
||||||
sqlList.add(sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sqlList;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new Exception(ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,77 +0,0 @@
|
||||||
package step3;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.awt.print.Printable;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.io.*;
|
|
||||||
import org.apache.tools.ant.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.condition.Equals;
|
|
||||||
import org.apache.tools.ant.types.*;
|
|
||||||
public class InsertTableTest {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
String tableName = "book";
|
|
||||||
|
|
||||||
InsertTable insertTable = new InsertTable();
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge3.sql";
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(insertTable.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(insertTable.USER);
|
|
||||||
sqlExec.setPassword(insertTable.PASS);
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Connection connection = insertTable.getConnection(insertTable.JDBC_DRIVER, insertTable.DB_URL, insertTable.USER, insertTable.PASS);
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
String input = sc.nextLine();
|
|
||||||
if(input.equals(""))
|
|
||||||
break;
|
|
||||||
|
|
||||||
String[]commands = input.split(" ");
|
|
||||||
if(commands.length ==0)
|
|
||||||
break;
|
|
||||||
int id = Integer.parseInt(commands[0]);
|
|
||||||
String title = commands[1];
|
|
||||||
String author = commands[2];
|
|
||||||
String publisher = commands[3];
|
|
||||||
int year = Integer.parseInt(commands[4]);
|
|
||||||
insertTable.InsertRecord(connection, id,title,author,publisher,year);
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultSet result = insertTable.queryDB(connection, tableName);
|
|
||||||
while (result.next()) {
|
|
||||||
String id = result.getString("id");
|
|
||||||
String title= result.getString("title");
|
|
||||||
String author = result.getString("author");
|
|
||||||
String publisher = result.getString("publisher");
|
|
||||||
String publishYear = result.getString("publishYear");
|
|
||||||
|
|
||||||
System.out.println(id + " " + title+ " " + author + " "
|
|
||||||
+ publisher + " " + publishYear);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,88 +0,0 @@
|
||||||
package step4;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class DeleteRecord {
|
|
||||||
public int deleteRecordByAuthor(Connection connection, String tableName,String author)
|
|
||||||
{
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
int updatedNum = 0;
|
|
||||||
//请在此处添加数据库删除记录的实现代码
|
|
||||||
String sqlScript = "delete from " + tableName + " where author = \'" + author + "\'";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
updatedNum = stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return updatedNum;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ResultSet queryDB(Connection connection,String tableName) {
|
|
||||||
ResultSet result = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "select * from " + tableName + " order by id desc";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
package step4;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.io.*;
|
|
||||||
import org.apache.tools.ant.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.*;
|
|
||||||
import org.apache.tools.ant.types.*;
|
|
||||||
public class DeleteRecordTest {
|
|
||||||
public static void main(String[] args) throws SQLException {
|
|
||||||
|
|
||||||
String tableName ="book";
|
|
||||||
|
|
||||||
String authorName = "";
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
authorName = sc.nextLine();
|
|
||||||
|
|
||||||
DeleteRecord deleteTable = new DeleteRecord();
|
|
||||||
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(deleteTable.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(deleteTable.USER);
|
|
||||||
sqlExec.setPassword(deleteTable.PASS);
|
|
||||||
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge4.sql";
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection connection = deleteTable.getConnection(deleteTable.JDBC_DRIVER, deleteTable.DB_URL, deleteTable.USER, deleteTable.PASS);
|
|
||||||
|
|
||||||
int updatedNum = deleteTable.deleteRecordByAuthor(connection,tableName,authorName);
|
|
||||||
|
|
||||||
//updatedNum=0说明删除的行数为0,删除失败
|
|
||||||
if(updatedNum == 0)
|
|
||||||
{
|
|
||||||
System.out.println(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultSet result = deleteTable.queryDB(connection, tableName);
|
|
||||||
while (result.next()) {
|
|
||||||
String id = result.getString("id");
|
|
||||||
String title= result.getString("title");
|
|
||||||
String author = result.getString("author");
|
|
||||||
String publisher = result.getString("publisher");
|
|
||||||
String publishYear = result.getString("publishYear");
|
|
||||||
|
|
||||||
System.out.println(id + " " + title+ " " + author + " "
|
|
||||||
+ publisher + " " + publishYear);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
package step5;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class QueryRecord {
|
|
||||||
|
|
||||||
public ResultSet queryPublisherByBookName(Connection connection, String tableName,String bookName)
|
|
||||||
{
|
|
||||||
//请在此处查询表的操作
|
|
||||||
Statement stmt = null;
|
|
||||||
ResultSet result = null;
|
|
||||||
String sqlScript = "select distinct(publisher) from " + tableName + " where title = '" + bookName + "\'";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
package step5;
|
|
||||||
import java.io.File;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.apache.tools.ant.Project;
|
|
||||||
import org.apache.tools.ant.taskdefs.SQLExec;
|
|
||||||
public class QueryRecordTest {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws SQLException {
|
|
||||||
String tableName = "book";
|
|
||||||
String bookName = "";
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
String input = scanner.nextLine();
|
|
||||||
String[] queryInfo = input.split(" ");
|
|
||||||
if(queryInfo.length != 1)
|
|
||||||
{
|
|
||||||
System.out.println("参数输入错误");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
bookName = queryInfo[0];
|
|
||||||
QueryRecord queryRecord = new QueryRecord();
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge4.sql";
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(queryRecord.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(queryRecord.USER);
|
|
||||||
sqlExec.setPassword(queryRecord.PASS);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection connection = queryRecord.getConnection(queryRecord.JDBC_DRIVER, queryRecord.DB_URL, queryRecord.USER, queryRecord.PASS);
|
|
||||||
ResultSet result = queryRecord.queryPublisherByBookName(connection, tableName,bookName);
|
|
||||||
while(result.next())
|
|
||||||
{
|
|
||||||
String id = result.getString("publisher");
|
|
||||||
System.out.println("publisher:" + id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,86 +0,0 @@
|
||||||
package step6;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.io.*;
|
|
||||||
import org.apache.tools.ant.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.*;
|
|
||||||
import org.apache.tools.ant.types.*;
|
|
||||||
public class UpdateRecord {
|
|
||||||
|
|
||||||
public int updatePublisherByTitle(Connection connection,String tableName, String title, String publisher){
|
|
||||||
//请在此处实现数据更新功能
|
|
||||||
Statement stmt = null;
|
|
||||||
int updatedNum=0;
|
|
||||||
String sqlScript = "update " + tableName + " set publisher=\'" +publisher + "\' where title=\'" + title + "'";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
updatedNum = stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return updatedNum;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResultSet queryDB(Connection connection,String tableName) {
|
|
||||||
ResultSet result = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "select * from " + tableName + " order by id desc";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,69 +0,0 @@
|
||||||
package step6;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.io.*;
|
|
||||||
import org.apache.tools.ant.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.*;
|
|
||||||
import org.apache.tools.ant.types.*;
|
|
||||||
public class UpdateRecordTest {
|
|
||||||
public static void main(String args[]) throws SQLException {
|
|
||||||
|
|
||||||
String tableName = "book";
|
|
||||||
|
|
||||||
UpdateRecord updateRecord = new UpdateRecord();
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge6.sql";
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(updateRecord.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(updateRecord.USER);
|
|
||||||
sqlExec.setPassword(updateRecord.PASS);
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection connection = updateRecord.getConnection(updateRecord.JDBC_DRIVER, updateRecord.DB_URL, updateRecord.USER, updateRecord.PASS);
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
String input = scanner.nextLine();
|
|
||||||
if(input.equals(""))
|
|
||||||
break;
|
|
||||||
|
|
||||||
String[]commands = input.split(" ");
|
|
||||||
if(commands.length ==0 || commands.length != 2)
|
|
||||||
break;
|
|
||||||
|
|
||||||
String title = commands[0];
|
|
||||||
String publisher = commands[1];
|
|
||||||
int updatedNum = updateRecord.updatePublisherByTitle(connection, tableName,title,publisher);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ResultSet result = updateRecord.queryDB(connection, tableName);
|
|
||||||
while (result.next()) {
|
|
||||||
String id = result.getString("id");
|
|
||||||
String title= result.getString("title");
|
|
||||||
String author = result.getString("author");
|
|
||||||
String publisher = result.getString("publisher");
|
|
||||||
String publishYear = result.getString("publishYear");
|
|
||||||
|
|
||||||
System.out.println(id + " " + title+ " " + author + " "
|
|
||||||
+ publisher + " " + publishYear);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,125 +0,0 @@
|
||||||
package step7;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
import javax.naming.spi.DirStateFactory.Result;
|
|
||||||
|
|
||||||
public class TransferTable {
|
|
||||||
|
|
||||||
public void transferTable(Connection connection, String tableName) throws SQLException{
|
|
||||||
//请在此将转换表结构的函数补充完整。
|
|
||||||
ResultSet result = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
String sqlScript = "select * from " + tableName + " order by id desc";
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
String sqlScrript = "insert into transformedBook (id,column_name,value) values (";
|
|
||||||
while (result.next()) {
|
|
||||||
String sql = "";
|
|
||||||
String id = result.getString("id");
|
|
||||||
String title= result.getString("title");
|
|
||||||
if(title != null)
|
|
||||||
{
|
|
||||||
sql = sqlScript + id + ",'" + "title" + "','" + title + "');";
|
|
||||||
InsertRecord(connection, sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
String author = result.getString("author");
|
|
||||||
if(author != null)
|
|
||||||
{
|
|
||||||
sql = sqlScript + id + ",'" + "author" + "','" + author + "');";
|
|
||||||
InsertRecord(connection, sql);
|
|
||||||
}
|
|
||||||
String publisher = result.getString("publisher");
|
|
||||||
if(publisher != null)
|
|
||||||
{
|
|
||||||
sql = sqlScript + id + ",'" + "publisher" + "','" + publisher + "');";
|
|
||||||
InsertRecord(connection, sql);
|
|
||||||
}
|
|
||||||
String publishYear = result.getString("publishYear");
|
|
||||||
if(publishYear != null)
|
|
||||||
{
|
|
||||||
sql = sqlScript + id + ",'" + "publishYear" + "','" + publishYear + "');";
|
|
||||||
InsertRecord(connection, sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void InsertRecord(Connection connection,String sqlScript){
|
|
||||||
Statement stmt = null;
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResultSet queryDB(Connection connection, String tableName){
|
|
||||||
|
|
||||||
//请在此处查询表的操作
|
|
||||||
Statement stmt = null;
|
|
||||||
ResultSet result = null;
|
|
||||||
String sqlScript = "select * from " + tableName;
|
|
||||||
try {
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
result = stmt.executeQuery(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
// 设定JDBC驱动以及本地数据库的URL
|
|
||||||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
||||||
static final String DB_URL = "jdbc:mysql:///bookstore?useUnicode=true&characterEncoding=utf-8";
|
|
||||||
|
|
||||||
// 指定数据库的用户名和密码
|
|
||||||
static final String USER = "root";
|
|
||||||
static final String PASS = "123123";
|
|
||||||
|
|
||||||
// 建立与指定数据库的连接,并返回该连接
|
|
||||||
public Connection getConnection(String jdbc_driver, String db_url, String db_user, String db_passwd) {
|
|
||||||
Connection connection = null;
|
|
||||||
|
|
||||||
//注册JDBC驱动
|
|
||||||
try {
|
|
||||||
Class.forName(jdbc_driver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建于指定数据库的连接
|
|
||||||
try {
|
|
||||||
connection = (Connection) DriverManager.getConnection(db_url, db_user, db_passwd);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
//创建数据库实例
|
|
||||||
public void createDatabaseInstance(Connection connection, String databaseName){
|
|
||||||
|
|
||||||
Statement stmt = null;
|
|
||||||
|
|
||||||
//请在此处补全创建数据库实例的SQL语句
|
|
||||||
String sqlScript = "";
|
|
||||||
try {
|
|
||||||
stmt = (Statement) connection.createStatement();
|
|
||||||
stmt.executeUpdate(sqlScript);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
||||||
package step7;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.io.*;
|
|
||||||
import org.apache.tools.ant.*;
|
|
||||||
import org.apache.tools.ant.taskdefs.*;
|
|
||||||
import org.apache.tools.ant.types.*;
|
|
||||||
|
|
||||||
public class TransferTableTest {
|
|
||||||
public static void main(String args[]) throws SQLException {
|
|
||||||
TransferTable transferTable = new TransferTable();
|
|
||||||
|
|
||||||
//原始的表名
|
|
||||||
String tableName = "book";
|
|
||||||
//转换后的表名
|
|
||||||
String transferTableName = "transformedBook";
|
|
||||||
|
|
||||||
//获取初始化的sql脚本
|
|
||||||
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge7.sql";
|
|
||||||
SQLExec sqlExec = new SQLExec();
|
|
||||||
//设置数据库参数
|
|
||||||
sqlExec.setDriver(transferTable.JDBC_DRIVER);
|
|
||||||
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
||||||
sqlExec.setUserid(transferTable.USER);
|
|
||||||
sqlExec.setPassword(transferTable.PASS);
|
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//要执行的脚本
|
|
||||||
sqlExec.setSrc(new File(sqlFile));
|
|
||||||
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
||||||
sqlExec.execute();
|
|
||||||
}catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Connection connection = transferTable.getConnection(transferTable.JDBC_DRIVER, transferTable.DB_URL, transferTable.USER, transferTable.PASS);
|
|
||||||
|
|
||||||
transferTable.transferTable(connection, tableName);
|
|
||||||
|
|
||||||
//查询数据库中转换后的表,输出其内容以判断操作转换操作是否正确
|
|
||||||
ResultSet resultSet = transferTable.queryDB(connection, transferTableName);
|
|
||||||
|
|
||||||
while(resultSet.next())
|
|
||||||
{
|
|
||||||
String id = resultSet.getString("id");
|
|
||||||
String columnName = resultSet.getString("columnName");
|
|
||||||
String value = resultSet.getString("value");
|
|
||||||
//注意这里每行后有换行,即最后一行后有换行。
|
|
||||||
System.out.println(id + " " + columnName + " " + value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package test.test;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
Manifest-Version: 1.0
|
||||||
|
Built-By: kevin
|
||||||
|
Build-Jdk: 1.8.0_101
|
||||||
|
Created-By: Maven Integration for Eclipse
|
||||||
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
#Generated by Maven Integration for Eclipse
|
||||||
|
#Wed Sep 20 07:09:35 CST 2017
|
||||||
|
version=0.0.1-SNAPSHOT
|
||||||
|
groupId=test
|
||||||
|
m2e.projectName=test
|
||||||
|
m2e.projectLocation=C\:\\software\\Code\\dockerAPI\\tmp
|
||||||
|
artifactId=test
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>test</groupId>
|
||||||
|
<artifactId>test</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>test</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-client -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.fabric8</groupId>
|
||||||
|
<artifactId>kubernetes-client</artifactId>
|
||||||
|
<version>2.6.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.spotify/docker-client -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spotify</groupId>
|
||||||
|
<artifactId>docker-client</artifactId>
|
||||||
|
<version>8.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.docker-java</groupId>
|
||||||
|
<artifactId>docker-java</artifactId>
|
||||||
|
<version>3.0.13</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue