Compare commits
No commits in common. "master" and "useful" have entirely different histories.
|
|
@ -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>testkubernetes</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,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,90 @@
|
|||
<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>com.lqk</groupId>
|
||||
<artifactId>testkubernetes</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>KubernetesAPI</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>3.0.0-milestone1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-hc</artifactId>
|
||||
<!-- 2.7.8 or 3.0.0-milestone1 -->
|
||||
<version>3.1.11</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-api -->
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>kubernetes-api</artifactId>
|
||||
<version>2.2.211</version>
|
||||
</dependency>
|
||||
<!-- 2.2.211https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.12</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.json/json -->
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20160212</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>1.1.0.Final</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/io.fabric8/fabric8-utils -->
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>fabric8-utils</artifactId>
|
||||
<version>2.2.211</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/io.fabric8/openshift-client -->
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>openshift-client</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/io.fabric8/openshift-client -->
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>openshift-client</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/dnsjava/dnsjava -->
|
||||
<dependency>
|
||||
<groupId>dnsjava</groupId>
|
||||
<artifactId>dnsjava</artifactId>
|
||||
<version>2.1.8</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.7.4</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>2.4.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.fusesource.jansi/jansi -->
|
||||
<dependency>
|
||||
<groupId>org.fusesource.jansi</groupId>
|
||||
<artifactId>jansi</artifactId>
|
||||
<version>1.11</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
package com.lqk.testk8s;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
//import io.fabric8.kubernetes.api.KubernetesFactory;
|
||||
import io.fabric8.kubernetes.api.KubernetesHelper;
|
||||
import io.fabric8.kubernetes.api.model.*;
|
||||
import io.fabric8.kubernetes.api.builder.*;
|
||||
//import io.fabric8.kubernetes.api.model.Config;
|
||||
import io.fabric8.kubernetes.client.Config;
|
||||
import io.fabric8.kubernetes.api.model.*;
|
||||
import io.fabric8.kubernetes.client.ConfigBuilder;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
|
||||
import org.apache.cxf.jaxrs.*;
|
||||
//import io.fabric8.kubernetes.api.KubernetesFactory;
|
||||
public class Main {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Main.class);
|
||||
public static void main(String args[])
|
||||
{
|
||||
|
||||
Main k8s = new Main("106.75.78.183","8080");
|
||||
KubernetesClient client = k8s.getConnection();
|
||||
Map<String, Pod> pods = KubernetesHelper.getPodMap(client, "default");
|
||||
for (Entry<String, Pod> entry : pods.entrySet()) {
|
||||
Pod pod = entry.getValue();
|
||||
Map<String, String> lables = KubernetesHelper.getLabels(pod);
|
||||
for (Entry<String, String> entry2 : lables.entrySet())
|
||||
System.out.println(entry2.getKey());
|
||||
System.out.println("next");
|
||||
System.out.println(KubernetesHelper.getHost(pod));
|
||||
}
|
||||
//KubernetesHelper.getHost(pod)
|
||||
System.out.println(client.getConfiguration().getApiVersion());
|
||||
// for(Pod pod:client.getNamespace())
|
||||
// System.out.println(pod.getNamespace());
|
||||
|
||||
}
|
||||
|
||||
private String ip;
|
||||
private String port;
|
||||
|
||||
public Main(String ip, String port) {
|
||||
this.ip= ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public KubernetesClient getConnection() {
|
||||
final String URI = "http://" + ip+ ":" + port;
|
||||
String master = "https://"+ip+ ":" + port + "/";
|
||||
Config config = new ConfigBuilder().withMasterUrl(URI).build();
|
||||
final KubernetesClient kubernetes = new DefaultKubernetesClient(config);
|
||||
return kubernetes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in New Issue