2024/10/22日工作总结
1.利用Java数据库连接池(druid)连接数据库:
点击查看代码
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;
public class test {
public static void main(String[] args) throws Exception {
//druid数据库连接池
//导入jar包
//定义配置文件druid.properties
//加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
// System.out.println(System.getProperty("user.dir"));
}
}
点击查看代码
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
initialSize=3
maxActive=10
maxWait=3000
2.学习Maven并完成Maven环境变量的配置,在idea完成Maven配置
3.数据结构设计算法实现统计树中度为1的结点:
点击查看代码
#include <iostream>
using namespace std;
// 树的节点结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 递归函数,统计度为1的节点个数
int countDegreeOneNodes(TreeNode* root) {
if (root == nullptr) {
return 0;
}
// 统计当前节点是否度为1
int count = 0;
if ((root->left == nullptr && root->right != nullptr) ||
(root->left != nullptr && root->right == nullptr)) {
count = 1;
}
// 递归统计左子树和右子树中的度为1的节点
return count + countDegreeOneNodes(root->left) + countDegreeOneNodes(root->right);
}
int main() {
// 构建树
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->right->right = new TreeNode(5);
int result = countDegreeOneNodes(root);
cout << "树中度为1的节点个数: " << result << endl;
return 0;
}