java特性合集

更新至jdbc、反射

[toc]

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...");
}
}
  • 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:

请我喝瓶快乐水吧~