JDBC操作基础

JDBC过程

  1. 创建数据源

    DataSource dataSource = new MysqlDataSource();
    
  2. 建立与数据库的连接

    由于mySQL是服务器与客户端连接形式的数据库软件, 需要建立客户端与服务器的联系, 但并不是所有数据库软件都是这种形式, 为了通用性, JDBC自带的DataSource类型并没有与服务器建立连接的方法, 所以需要向下转型, 使用MysqlDataSource建立与服务器的连接

    ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/bite?characterEncoding=UTF-8&useSSL=false");
    ((MysqlDataSource)dataSource).setUser("root");
    ((MysqlDataSource)dataSource).setPassword("2222");
    Connection connection = dataSource.getConnection();
    

在这里插入图片描述

  1. 书写sql语句, 并对sql语句进行预处理

    String sql = "insert into student values('张三',12)";
    PreparedStatement statement = connection.prepareStatement(sql); //对sql语句进行预处理
    

    服务器其实可以对sql进行预处理, 在客户端预处理为了减轻服务器的压力

  2. 进行sql操作

    statement.executeUpdate(); //删除,插入,修改操作,返回值为涉及的行数
    statement.executeQuery(); //查询操作, 需要用ResultSet类型接受结果, 如何查看数据见下
    
  3. 释放资源, 先创建的后释放

    statement.close();
    connection.close();
    

查看查询到的数据

String sql = "select * from student;";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()){//检测是否还有没读的行
    System.out.println(resultSet.getInt("age"));//括号内可以是列名也可以是列数,从1开始,类型必须对应
    System.out.println(resultSet.getString("name"));
}

通过外部数据进行sql操作

在实际工程中我们往往需要使用类似命令行中scanner的办法获取用户数据, 然后进行sql的操作, 最简单的办法就是直接读取数据, 然后以拼接字符串的方式进行操作, 但是这种做法不仅让代码可读性降低, 而且危险性很大, 我们无法确保用户输入的数据是什么样的。例如下面的例子:

Scanner scan = new Scanner(System.in);
System.out.println("输入您的姓名:");
String name = scan.next();
System.out.println("输入您的年龄:");
int age = scan.nextInt();
String sql = "insert into student values('" + name + "'," + age + ");";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();

作为用户我可以这样输入: “; drop table student;”

这样输入虽然并不能真的删除student, 我只是拿来举个例子, 但是懂得如何输入的"用户"自然会成功, 这也就是所谓的sql注入

为了防止这一情况的放生, 我们使用库内的方法进行sql操作, 这样不仅增加了代码可读性, 也增加了代码的安全性。

String sql = "insert into student values(? , ?);";//问号为占位符
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,age);
statement.executeUpdate();
华为OD华为公司用于招聘软件开发工程师的一项重要考核环节。2024华为OD题库中包含了多种类型的编程题目,主要以C语言为主。以下是一些常见的题型和示例: 1. **基础算法题**: - 题目:实现一个函数,计算两个整数的最大公约数(GCD)。 - 示例代码: ```c #include <stdio.h> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a, b; printf("Enter two integers: "); scanf("%d %d", &a, &b); printf("GCD of %d and %d is %d\n", a, b, gcd(a, b)); return 0; } ``` 2. **数据结构题**: - 题目:实现一个简单的链表,并提供插入和删除操作。 - 示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void insert(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void deleteNode(struct Node** head_ref, int key) { struct Node *temp = *head_ref, *prev; if (temp != NULL && temp->data == key) { *head_ref = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { struct Node* head = NULL; insert(&head, 3); insert(&head, 2); insert(&head, 1); printf("Created Linked List: "); printList(head); deleteNode(&head, 2); printf("\nLinked List after Deletion of 2: "); printList(head); return 0; } ``` 3. **逻辑题**: - 题目:给定一个整数数组,找出其中没有出现的最小正整数。 - 示例代码: ```c #include <stdio.h> int firstMissingPositive(int* nums, int numsSize) { for (int i = 0; i < numsSize; i++) { while (nums[i] > 0 && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) { int temp = nums[nums[i] - 1]; nums[nums[i] - 1] = nums[i]; nums[i] = temp; } } for (int i = 0; i < numsSize; i++) { if (nums[i] != i + 1) { return i + 1; } } return numsSize + 1; } int main() { int nums[] = {3, 4, -1, 1}; int size = sizeof(nums)/sizeof(nums[0]); printf("The smallest missing positive integer is %d\n", firstMissingPositive(nums, size)); return 0; } ``` 这些题目涵盖了C语言编程中的基础算法、数据结构和逻辑思维等方面。准备华为OD时,建议多练习类似题型,提升编程能力和解题技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最后一只三脚兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值