java特性合集

更新至jdbc、jdbc Template、多线程

jdbc

student.sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`sn` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学号',
`sex` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓别: 男, 女',
`source` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生来源: 老学员推荐,知乎,社招',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (2, '张小二', '1002', '男', '老学员推荐');
INSERT INTO `student` VALUES (3, '张小三', '1003', '男', '知乎');
INSERT INTO `student` VALUES (4, '张小四', '1004', '女', '社招');
INSERT INTO `student` VALUES (5, '张小五', '1005', '女', '社招');
INSERT INTO `student` VALUES (7, '靓仔一号·', '14250', '男', '靓仔学院');

SET FOREIGN_KEY_CHECKS = 1;

main.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main;

import java.sql.*;

public class main {
public static void main(String[] args) {
//导入jar包
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
//加载驱动
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String password = "123456";
Class.forName(driver);
//连接数据库
conn = DriverManager.getConnection(url,user,password);
//创建sql命令发射器
stmt = conn.createStatement();
//使用sql命令发送sql语句并得到结果
String sql = "SELECT * FROM student";
rs = stmt.executeQuery(sql);
System.out.println("编号\t姓名\t学号\t性别\t生源");
while(rs.next()) {
//获取各列数据
int id = rs.getInt(1);
String sname = rs.getString(2);
String sn = rs.getString(3);
String sex = rs.getString(4);
String wfrom = rs.getString(5);
System.out.println(id + "\t" + sname + "\t" + sn + "\t" + sex + "\t" + wfrom);
}
}catch (SQLException | ClassNotFoundException e){
e.printStackTrace();
}finally {
//关闭资源
try{
if(stmt!=null){
stmt.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (conn!=null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
}
}

反射

Relect.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.reflect;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflectTest {
public static void main(String[] args) throws Exception {
//加载配置文件
Properties pro = new Properties();
ClassLoader classLoader = ReflectTest.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("pro.properties");
pro.load(is);
//获取文件信息
String className = pro.getProperty("className");
String methodName = pro.getProperty("methodName");
//加载方法到内存
Class cls = Class.forName(className);
//获取对象
Object obj = cls.newInstance();
//获取方法
Method method = cls.getMethod(methodName);
//执行方法
method.invoke(obj);
}
}

pro.properties

1
2
className=com.domain.Student
methodName=sleep

Student.class

1
2
3
4
5
6
7
package com.domain;

public class Student {
public void sleep(){
System.out.println("sleep...");
}
}

多线程

Box.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.main;

/**
* @author 北落燕门
*/
public class Box {
private int milk;
private Boolean state = false;
public synchronized void put(int milk){
//如果有牛奶
if (state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有牛奶就放置牛奶
this.milk = milk;
System.out.println("送货工在盒子里放置了第"+milk+"瓶奶");
state = true;
notifyAll();
}
public synchronized void get(){
//如果没有牛奶,等待生产
if (!state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("顾客拿走了第"+milk+"瓶奶");
state=false;
notifyAll();
}
}

BoxDemo.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.main;

public class BoxDemo {
public static void main(String[] args) {
Box b = new Box();
Producter p = new Producter(b);
Customer c = new Customer(b);

Thread th1 = new Thread(p);
Thread th2 = new Thread(c);

th1.start();
th2.start();
}
}

Customer.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.main;

public class Customer implements Runnable {
private Box b;
public Customer(Box b) {
this.b = b;
}


@Override
public void run() {
while (true){
b.get();
}
}
}

Producter.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.main;

public class Producter implements Runnable {
private Box b;

public Producter(Box b) {
this.b = b;
}

@Override
public void run() {
for (int i=1;i<5;i++){
b.put(i);
}
}
}

JDBC Template

Druid.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.main;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mysql.jdbc.PreparedStatement;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
* @author 北落燕门
*/
public class Druid {
private static DataSource ds;

static {
try{
Properties pro = new Properties();
InputStream is = Druid.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
ds = DruidDataSourceFactory.createDataSource(pro);
}catch (Exception e){
e.printStackTrace();
}
}
//获取连接池
public static DataSource getDs(){
return ds;
}
//获取数据库连接
public static Connection getConn() throws SQLException {
return ds.getConnection();
}
public static void getClose(ResultSet rs,PreparedStatement pstmt,Connection conn){
try{
if (rs!=null){
rs.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (pstmt!=null){
pstmt.close();
}
}catch (SQLException e){
e.printStackTrace();
}
try{
if (conn!=null){
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
public static void getClose(PreparedStatement pstmt,Connection conn){
getClose(null,pstmt,conn);
}
}

druid.properties

1
2
3
4
5
6
7
8
9
10
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?UseUnicode=True&characterEncoding=UTF-8
username=user
password=12345

initialSize=5

maxActive=10

maxWait=3000

jdbcTemplateTest.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.main;

import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
* @author 北落燕门
*/
public class JdbcTemplateTest {
JdbcTemplate template = new JdbcTemplate(Druid.getDs());
// 1. 修改1号数据
@Test
public void test1(){
String sql = "UPDATE student SET sex ='女' WHERE id=? ";
int count = template.update(sql, "7");
System.out.println(count);
}
// 2. 添加一条记录
@Test
public void test2(){
String sql = "INSERT INTO student VALUES(?,?,?,?,?)";
int update = template.update(sql, "250", "测试用例", "男", "2019-12-20", "13");
System.out.println(update);
}
// 3. 删除刚才添加的记录
@Test
public void test3(){
String sql = "DELETE FROM student WHERE sno = ?";
int update = template.update(sql, 250);
System.out.println(update);

}
// 4. 查询sno为101的记录,将其封装为Map集合
@Test
public void test4(){
String sql = "SELECT * FROM student WHERE sno=?";
Map<String, Object> stringObjectMap = template.queryForMap(sql, 101);
System.out.println(stringObjectMap);
}
// 5. 查询所有记录,将其封装为List
@Test
public void test5(){
String sql = "SELECT * FROM student";
List<Map<String, Object>> maps = template.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
// 6. 查询所有记录,将其封装为Emp对象的List集合
@Test
public void test6(){
String sql = "SELECT * FROM student";
List<student> query = template.query(sql, new BeanPropertyRowMapper<student>(student.class));
for (student student : query) {
System.out.println(student);
}

}
// 7. 查询总记录数
@Test
public void test7(){
String sql = "SELECT COUNT(*) FROM student";
Long tmp = template.queryForObject(sql, Long.class);
System.out.println(tmp);
}
}

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2021 John Doe
  • Visitors: | Views:

请我喝瓶快乐水吧~