-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'jdbc-advanced' into development
完成JDBC进阶课程的代码及介绍.
- Loading branch information
Showing
8 changed files
with
389 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
/* | ||
Navicat Premium Data Transfer | ||
Source Server : local | ||
Source Server Type : MySQL | ||
Source Server Version : 50635 | ||
Source Host : localhost:3306 | ||
Source Schema : cloud_study | ||
Target Server Type : MySQL | ||
Target Server Version : 50635 | ||
File Encoding : 65001 | ||
Date: 15/08/2017 21:07:48 | ||
*/ | ||
|
||
SET NAMES utf8mb4; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- ---------------------------- | ||
-- Table structure for user | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `user`; | ||
CREATE TABLE `user` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`userName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, | ||
PRIMARY KEY (`id`) USING BTREE | ||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; | ||
|
||
-- ---------------------------- | ||
-- Records of user | ||
-- ---------------------------- | ||
INSERT INTO `user` VALUES (1, 'ZhangSan'); | ||
INSERT INTO `user` VALUES (2, 'LiSi'); | ||
INSERT INTO `user` VALUES (3, 'ZhaoWu'); | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; | ||
/* | ||
Navicat Premium Data Transfer | ||
Source Server : local | ||
Source Server Type : MySQL | ||
Source Server Version : 50635 | ||
Source Host : localhost:3306 | ||
Source Schema : cloud_study | ||
Target Server Type : MySQL | ||
Target Server Version : 50635 | ||
File Encoding : 65001 | ||
Date: 15/08/2017 21:07:48 | ||
*/ | ||
|
||
SET NAMES utf8mb4; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- ---------------------------- | ||
-- Table structure for user | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `user`; | ||
CREATE TABLE `user` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`userName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, | ||
PRIMARY KEY (`id`) USING BTREE | ||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; | ||
|
||
-- ---------------------------- | ||
-- Records of user | ||
-- ---------------------------- | ||
INSERT INTO `user` VALUES (1, 'ZhangSan'); | ||
INSERT INTO `user` VALUES (2, 'LiSi'); | ||
INSERT INTO `user` VALUES (3, 'ZhaoWu'); | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Navicat Premium Data Transfer | ||
Source Server : local | ||
Source Server Type : MySQL | ||
Source Server Version : 50635 | ||
Source Host : localhost:3306 | ||
Source Schema : cloud_study | ||
Target Server Type : MySQL | ||
Target Server Version : 50635 | ||
File Encoding : 65001 | ||
Date: 20/08/2017 22:01:32 | ||
*/ | ||
|
||
SET NAMES utf8mb4; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- ---------------------------- | ||
-- Table structure for user | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `user`; | ||
CREATE TABLE `user` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`userName` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, | ||
`corp` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, | ||
PRIMARY KEY (`id`) USING BTREE | ||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; | ||
|
||
-- ---------------------------- | ||
-- Records of user | ||
-- ---------------------------- | ||
INSERT INTO `user` VALUES (1, 'ZhangSan', 'Netease'); | ||
|
||
-- ---------------------------- | ||
-- Table structure for user_note | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `user_note`; | ||
CREATE TABLE `user_note` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`blog` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, | ||
PRIMARY KEY (`id`) USING BTREE | ||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; | ||
|
||
-- ---------------------------- | ||
-- Records of user_note | ||
-- ---------------------------- | ||
INSERT INTO `user_note` VALUES (1, 'testread'); | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/micro/profession/jdbc/practice/BatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.micro.profession.jdbc.practice; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class BatchTest { | ||
|
||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | ||
static String DB_URL = "jdbc:mysql://localhost/cloud_study"; | ||
static final String USER = "root"; | ||
static final String PASSWORD = "root"; | ||
|
||
public static void insertUsers(Set<String> users) | ||
throws ClassNotFoundException { | ||
Connection conn = null; | ||
Statement stmt = null; | ||
|
||
//1. 加载数据库驱动 | ||
Class.forName(JDBC_DRIVER); | ||
//2. 建立数据库连接 | ||
try { | ||
// 获得数据库连接 | ||
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); | ||
//3. 执行SQL语句 | ||
stmt = conn.createStatement(); | ||
for(String user : users) { | ||
stmt.addBatch("insert into user(userName) values ('" + user | ||
+ "')"); | ||
} | ||
stmt.executeBatch(); | ||
stmt.clearBatch(); | ||
} catch (SQLException e) { | ||
// 异常处理 | ||
e.printStackTrace(); | ||
} finally { | ||
//5. 清理环境 | ||
try { | ||
if(conn != null) | ||
conn.close(); | ||
if(stmt != null) | ||
stmt.close(); | ||
} catch (SQLException e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws ClassNotFoundException { | ||
Set<String> users = new HashSet<String>(); | ||
users.add("GuoYi"); | ||
users.add("ZhangSi"); | ||
users.add("LiSan"); | ||
insertUsers(users); | ||
} | ||
|
||
} |
112 changes: 56 additions & 56 deletions
112
src/main/java/com/micro/profession/jdbc/practice/HelloJDBC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
package com.micro.profession.jdbc.practice; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class HelloJDBC { | ||
|
||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | ||
static String DB_URL = "jdbc:mysql://localhost/cloud_study"; | ||
static final String USER = "root"; | ||
static final String PASSWORD = "root"; | ||
|
||
public static void helloword() throws ClassNotFoundException { | ||
Connection conn = null; | ||
Statement stmt = null; | ||
ResultSet rs = null; | ||
|
||
//1. 加载数据库驱动 | ||
Class.forName(JDBC_DRIVER); | ||
//2. 建立数据库连接 | ||
try { | ||
// 获得数据库连接 | ||
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); | ||
//3. 执行SQL语句 | ||
stmt = conn.createStatement(); | ||
rs = stmt.executeQuery("select userName from user"); | ||
//4. 获取执行结果 | ||
while(rs.next()) { | ||
System.out.println("hello " + rs.getString("userName")); | ||
} | ||
} catch (SQLException e) { | ||
// 异常处理 | ||
e.printStackTrace(); | ||
} finally { | ||
//5. 清理环境 | ||
try { | ||
if(conn != null) | ||
conn.close(); | ||
if(stmt != null) | ||
stmt.close(); | ||
if(rs != null) | ||
rs.close(); | ||
} catch (SQLException e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws ClassNotFoundException { | ||
helloword(); | ||
} | ||
|
||
} | ||
package com.micro.profession.jdbc.practice; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class HelloJDBC { | ||
|
||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | ||
static String DB_URL = "jdbc:mysql://localhost/cloud_study"; | ||
static final String USER = "root"; | ||
static final String PASSWORD = "root"; | ||
|
||
public static void helloword() throws ClassNotFoundException { | ||
Connection conn = null; | ||
Statement stmt = null; | ||
ResultSet rs = null; | ||
|
||
//1. 加载数据库驱动 | ||
Class.forName(JDBC_DRIVER); | ||
//2. 建立数据库连接 | ||
try { | ||
// 获得数据库连接 | ||
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); | ||
//3. 执行SQL语句 | ||
stmt = conn.createStatement(); | ||
rs = stmt.executeQuery("select userName from user"); | ||
//4. 获取执行结果 | ||
while(rs.next()) { | ||
System.out.println("hello " + rs.getString("userName")); | ||
} | ||
} catch (SQLException e) { | ||
// 异常处理 | ||
e.printStackTrace(); | ||
} finally { | ||
//5. 清理环境 | ||
try { | ||
if(conn != null) | ||
conn.close(); | ||
if(stmt != null) | ||
stmt.close(); | ||
if(rs != null) | ||
rs.close(); | ||
} catch (SQLException e) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws ClassNotFoundException { | ||
helloword(); | ||
} | ||
|
||
} |
Oops, something went wrong.