Compare commits
No commits in common. "master" and "KubernetesMonitor" have entirely different histories.
master
...
Kubernetes
|
|
@ -0,0 +1,36 @@
|
|||
<?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 excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<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 excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<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>KubernetesMonitor</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,6 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=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,35 @@
|
|||
<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>Educoder</groupId>
|
||||
<artifactId>KubernetesMonitor</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Kubernetes集群资源监控</name>
|
||||
<description>获取kubernetes集群的资源使用状况,</description>
|
||||
|
||||
<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>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.educoder.api;
|
||||
/**
|
||||
* 获取系统CPU使用率,以监控整个集群的资源使用状况
|
||||
* @author kevin
|
||||
*
|
||||
*/
|
||||
public class CpuStatus {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package com.educoder.api;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
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.RegistryAuth;
|
||||
/**
|
||||
* 提供docker的各种接口供资源监控使用
|
||||
* @author kevin
|
||||
*"http://106.75.96.108:4243"
|
||||
*/
|
||||
public class Docker {
|
||||
private DockerClient dockerClient;
|
||||
|
||||
|
||||
public List<Container> getAllContainers(String name,String value) throws DockerException, InterruptedException
|
||||
{
|
||||
List containerList = new ArrayList<Container>();
|
||||
ListContainersParam params = new ListContainersParam(name, value);
|
||||
containerList = dockerClient.listContainers(params);
|
||||
return containerList;
|
||||
}
|
||||
|
||||
public String getCpuUsage(String containerId) throws DockerException, InterruptedException
|
||||
{
|
||||
ContainerStats stats = this.dockerClient.stats(containerId);
|
||||
Long cpuUsage = stats.cpuStats().cpuUsage().totalUsage();
|
||||
return cpuUsage/1024 + "";
|
||||
}
|
||||
|
||||
|
||||
Docker(String URL)
|
||||
{
|
||||
this.dockerClient = DefaultDockerClient.builder()
|
||||
.uri(URI.create(URL))
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.educoder.api;
|
||||
/**
|
||||
* 获取整个集群的健康状态,如平均编译运行时间、剩余内存及CPU
|
||||
* @author kevin
|
||||
*/
|
||||
public class HealthStatus {
|
||||
|
||||
public int getCpuUsage(Docker dockerClient)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getMemoUsage()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean ableToRun(Docker dockerClient)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.educoder.api;
|
||||
/**
|
||||
* 提供集群的各种信息如节点数量和地址,供资源监控使用
|
||||
* @author kevin
|
||||
*
|
||||
*/
|
||||
public class KubernetesClient {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.educoder.api;
|
||||
/**
|
||||
* 获取整个集群的内存使用状态
|
||||
* @author kevin
|
||||
*
|
||||
*/
|
||||
public class MemoryStatus {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.educoder.api;
|
||||
|
||||
public class Utils {
|
||||
|
||||
}
|
||||
|
|
@ -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,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
|
||||
#Sat Sep 09 09:16:30 CST 2017
|
||||
version=0.0.1-SNAPSHOT
|
||||
groupId=Educoder
|
||||
m2e.projectName=KubernetesMonitor
|
||||
m2e.projectLocation=C\:\\EclipseWorkSpace\\KubernetesMonitor
|
||||
artifactId=KubernetesMonitor
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<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>Educoder</groupId>
|
||||
<artifactId>KubernetesMonitor</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Kubernetes集群资源监控</name>
|
||||
<description>获取kubernetes集群的资源使用状况,</description>
|
||||
|
||||
<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>
|
||||
</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