Skip to content

Commit dabbdad

Browse files
author
ent_teach
committed
添加jdbc学习和netty学习
1 parent 6862333 commit dabbdad

File tree

12 files changed

+390
-57
lines changed

12 files changed

+390
-57
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<version>1.0-SNAPSHOT</version>
1010
<properties>
1111
<mongodb.driver.version>3.2.2</mongodb.driver.version>
12+
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
1213
</properties>
1314

1415
<dependencies>
@@ -48,6 +49,12 @@
4849
<version>2.7</version>
4950
</dependency>
5051

52+
<dependency>
53+
<groupId>mysql</groupId>
54+
<artifactId>mysql-connector-java</artifactId>
55+
<version>${mysql-connector-java.version}</version>
56+
</dependency>
57+
5158

5259
<!-- easypoi -->
5360
<dependency>
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
package com.chen.api.util.jdbc;
2+
3+
import java.sql.*;
4+
5+
/**
6+
* 基本的jdbc查询数据库
7+
*
8+
* @author : chen weijie
9+
* @Date: 2018-08-06 3:15 PM
10+
*/
11+
public class JDBCTest {
12+
13+
14+
public static void main(String[] args) {
15+
testQuery();
16+
17+
}
18+
19+
20+
/**
21+
* 查询操作
22+
*/
23+
public static void testQuery() {
24+
25+
Connection connection = getConnection();
26+
27+
try {
28+
String querySql = "select * from ent_user_cache_multi_provider_rule ";
29+
Statement statement = connection.createStatement();
30+
ResultSet rs = statement.executeQuery(querySql);
31+
while (rs.next()) {
32+
System.out.println("rs:" + rs.getString("table_name"));
33+
}
34+
35+
36+
} catch (SQLException e) {
37+
e.printStackTrace();
38+
}
39+
releaseConnection(connection);
40+
}
41+
42+
/**
43+
* 测试更新操作
44+
*/
45+
public static void testUpdate() {
46+
Connection connection = getConnection();
47+
48+
String Sql = "update ent_user_cache_multi_provider_rule set salary=8000 where employee_id=100005";
49+
try {
50+
Statement stmt1 = connection.createStatement();//或者用PreparedStatement方法
51+
stmt1.executeUpdate(Sql);//执行sql语句
52+
if (stmt1 != null) {
53+
try {
54+
stmt1.close();
55+
} catch (SQLException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
} catch (SQLException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
/**
65+
* 测试事务
66+
*/
67+
public static void testTraction() {
68+
69+
/**
70+
* JDBC处理事务通过关闭连接的自动提交实现的:
71+
72+
Connection.setAutoCommit(false);
73+
提交事务:
74+
Connection.commit();
75+
回滚事务
76+
回滚部分:
77+
Connection.rollback(Savepoint);
78+
全部回滚:
79+
Connection.rollback();
80+
81+
*/
82+
Connection connection = getConnection();
83+
84+
Statement statement = null;
85+
try {
86+
connection.setAutoCommit(false);
87+
statement = connection.createStatement();
88+
89+
String sql = "update employees set salary=500 where employee_id=100001";
90+
statement.executeQuery(sql);
91+
sql = "update employees set salary=100 where employee_id=100002";
92+
statement.executeQuery(sql);
93+
connection.commit();
94+
} catch (SQLException e) {
95+
try {
96+
connection.rollback();
97+
} catch (SQLException e1) {
98+
e1.printStackTrace();
99+
}
100+
e.printStackTrace();
101+
}
102+
103+
if (statement != null) {
104+
try {
105+
statement.close();
106+
} catch (SQLException e) {
107+
e.printStackTrace();
108+
}
109+
}
110+
releaseConnection(connection);
111+
}
112+
113+
/**
114+
* 测试事务回滚
115+
*/
116+
public static void testTractionRollBack() {
117+
118+
Savepoint savepoint = null;
119+
Connection connection = getConnection();
120+
121+
Statement statement = null;
122+
try {
123+
connection.setAutoCommit(false);
124+
statement = connection.createStatement();
125+
126+
String sql = "update employees set salary=500 where employee_id=100001";
127+
savepoint = connection.setSavepoint("savepoint1");
128+
129+
statement.executeQuery(sql);
130+
sql = "update employees set salary=100 where employee_id=100002";
131+
statement.executeQuery(sql);
132+
connection.commit();
133+
} catch (SQLException e) {
134+
e.printStackTrace();
135+
try {
136+
if (savepoint == null) {
137+
// SQLException occurred in saving into Employee or Address
138+
// tables
139+
connection.rollback();
140+
System.out.println("JDBC Transaction rolled back successfully");
141+
} else {
142+
// exception occurred in inserting into Logs table
143+
// we can ignore it by rollback to the savepoint
144+
connection.rollback(savepoint);
145+
// lets commit now
146+
connection.commit();
147+
}
148+
} catch (SQLException e1) {
149+
System.out.println("SQLException in rollback" + e.getMessage());
150+
}
151+
}
152+
153+
if (statement != null) {
154+
try {
155+
statement.close();
156+
} catch (SQLException e) {
157+
e.printStackTrace();
158+
}
159+
}
160+
releaseConnection(connection);
161+
}
162+
163+
164+
/**
165+
* 获取数据库连接
166+
*
167+
* @return
168+
*/
169+
public static Connection getConnection() {
170+
171+
Connection connection = null;
172+
//1.定义驱动程序名为driver内容为com.mysql.jdbc.Driver
173+
String driver = "com.mysql.jdbc.Driver";
174+
//2.定义url;jdbc是协议;mysql是子协议:表示数据库系统管理名称;localhost:3306是你数据库来源的地址和目标端口;test是我本人建的表位置所在处,你以你的为标准。
175+
String url = "jdbc:mysql://172.16.117.226:3306/ent_portal?useUnicode=true&characterEncoding=UTF8";
176+
String user = "ent_all";
177+
String password = "ent";
178+
179+
try {
180+
Class.forName(driver);
181+
//获取数据库连接,使用java.sql里面的DriverManager的getConnectin(String url , String username ,String password )来完成
182+
connection = DriverManager.getConnection(url, user, password);
183+
184+
//8.构造一个statement对象来执行sql语句:主要有Statement,PreparedStatement,CallableStatement三种实例来实现
185+
//三种实现方法分别为:
186+
// Statement stmt = con.createStatement() ;
187+
//PreparedStatement pstmt = conn.prepareStatement(sql) ;
188+
// CallableStatement cstmt = conn.prepareCall("{CALL demoSp(? , ?)}") ;
189+
} catch (ClassNotFoundException e) {
190+
e.printStackTrace();
191+
} catch (SQLException e) {
192+
e.printStackTrace();
193+
}
194+
195+
return connection;
196+
}
197+
198+
199+
/**
200+
* 释放数据库连接
201+
*
202+
* @param conn
203+
*/
204+
public static void releaseConnection(Connection conn) {
205+
try {
206+
if (conn != null)
207+
conn.close();
208+
} catch (Exception e) {
209+
e.printStackTrace();
210+
}
211+
}
212+
213+
214+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chen.api.util.netty;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-08-07 12:30 PM
6+
*/
7+
public class BIOTest {
8+
9+
10+
public static void main(String[] args) {
11+
12+
13+
14+
15+
16+
17+
}
18+
19+
20+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.chen.api.util.thread.callable;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.concurrent.*;
7+
8+
/**
9+
* @author : chen weijie
10+
* @Date: 2018-05-31 00:32
11+
*/
12+
public class CallableTest {
13+
14+
public static void main(String[] args) throws ExecutionException, InterruptedException {
15+
16+
17+
System.out.println("程序开始执行。。。。");
18+
Date date1 = new Date();
19+
20+
ExecutorService pool = Executors.newFixedThreadPool(5);
21+
22+
List<Future> list = new ArrayList<>();
23+
24+
for (int i = 0; i < 5; i++) {
25+
Callable c = new MyCallable(i + "");
26+
Future<Object> future = pool.submit(c);
27+
list.add(future);
28+
}
29+
30+
31+
pool.shutdown();
32+
33+
for (Future f : list) {
34+
35+
System.out.println("result:" + f.get().toString());
36+
}
37+
38+
Date date2 = new Date();
39+
40+
System.out.println("程序结束用时:" + (date2.getTime() - date1.getTime()));
41+
42+
43+
}
44+
45+
46+
static class MyCallable implements Callable<Object> {
47+
48+
private String taskNum;
49+
50+
MyCallable(String taskNum) {
51+
this.taskNum = taskNum;
52+
}
53+
54+
@Override
55+
public Object call() throws Exception {
56+
System.out.println("taskNum" + taskNum + "任务启动。");
57+
Date date = new Date();
58+
Thread.sleep(1000);
59+
Date date2 = new Date();
60+
long time = date2.getTime() - date.getTime();
61+
System.out.println("任务终止:" + taskNum);
62+
return taskNum + "任务返回运行结果,当前任务用时:" + time + "ms";
63+
}
64+
}
65+
66+
67+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.chen.api.util.thread.study.chapter4.yieldTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-31 01:27
6+
*/
7+
public class MyThread extends Thread {
8+
9+
10+
@Override
11+
public void run() {
12+
long beginTIme = System.currentTimeMillis();
13+
int count = 0;
14+
15+
for (int i = 0; i < 500000000; i++) {
16+
count = count + (i + 1);
17+
Thread.yield();
18+
}
19+
20+
long endTime = System.currentTimeMillis();
21+
22+
System.out.println("用时:" + (endTime - beginTIme) + "ms");
23+
24+
}
25+
26+
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.chen.api.util.thread.study.chapter4.yieldTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-31 01:30
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
MyThread myThread =new MyThread();
12+
13+
myThread.start();
14+
15+
16+
17+
}
18+
19+
}

src/main/java/com/chen/api/util/thread/test1/SpeakHello.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class SpeakHello extends Thread {
77

88

9+
@Override
910
public void run() {
1011

1112
for (int i = 0; i < 10; i++) {
@@ -14,5 +15,4 @@ public void run() {
1415

1516
}
1617

17-
1818
}

src/main/java/com/chen/api/util/thread/test1/SpeakNiHao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
public class SpeakNiHao extends Thread {
77

8+
@Override
89
public void run() {
9-
1010
for (int i = 0; i < 20; i++) {
1111
System.out.println("你好" + i);
1212
}

0 commit comments

Comments
 (0)