目录
Java特训营-第六周-笔试部分
1、MySQL中,预设的、拥有最高权限超级用户的用户名为( D )[单选题]
2、SQL语言集数据查询、数据操纵、数据定义和数据控制功能于一体,其中,CREATE、DROP、ALTER语句是实现哪种功能( )[单选题( C )
3、Mysql中表student_info(id,name,birth,sex),字段类型都是varchar,插入:1018 , 赵六 , 2003-08-02 , 男;SQL正确的是( C )
4、delete from employee语句的作用是( )[单选题](B )
5、条件“IN(20,30,40)”表示( )[单选题]( C )
6、使用SELECT语句随机地从表中挑出指定数量的行,可以使用的方法是( D )[单选题]
8、要求删除商品表中价格大于3000的商品,下列SQL语句正确的是( A )
11、下面关于PreparedStatement的说法错误的 [单选题]( C )
12、如果为预编译SQL的问号赋值,那么正确的选项是哪一个?[单选题] ( B )
13、以下有关于SQL注入漏洞说法错误的是[单选题]( B )
14、使用Connection的哪个方法可以建立一个PreparedStatement接口【单选题】( B )
16、在Jdbc中使用事务,想要回滚事务事务的方法是什么?( C )
17、在成绩表中查询绩点在3.7-4.0之间,并且挂科数为0的学生的所有信息。下列SQL语句不正确的是( D )
18、对于数据库的查询操作,需要释放掉的数据库连接对象有哪些( BCD )
19、关于ResultSet结果集的next()方法,说法正确的是( AC )。
1、说说preparedStatement和Statement的区别。
3、executeQuery(),executeUpdate()的区别是什么?
A. select student_id, sum(course_id) from course_relation;
B、select student_id, sum(course_id) from lcourse_relation group by student_id;
C、select student_id, count(course_id) from course_relation;
D、select student_id, count(course_id) from course_relation group by student_id;
A、
| select t2.*,c1 as `最高分` from (select stu_id,sum(score) as c1 from score_table group by stu_id order by c1 desc limit 1) t1 join student_table t2 on t1.stu_id = t2.id ; |
B、
| select t2.*,c1 as `最高分` from ( select stu_id,max(score) as c1 from score_table group by stu_id order by c1 desc limit 1 ) t1 join student_table t2 on t1.stu_id = t2.id ; |
C、
| select t2.*,c1 as `最高分` from ( select stu_id,max(sum(score)) as c1 from score_table group by stu_id order by c1 desc limit 1 ) t1 join student_table t2 on t1.stu_id = t2.id ; |
D、
| select t2.*,c1 as `最高分` from ( select stu_id,max(sum(score)) as c1 from score_table group by stu_id ) t1 join student_table t2 on t1.stu_id = t2.id ; |
1、当某字段要使用AUTO_INCREMENT的属性时,该字段必须是____int_________类型的数据。
2、补全语句:select vend_id,count(*) from products where prod_price>=10 group by vend_id _having___ count(*)>=2;
3、计算字段的累加和的函数是:____sum___
4、现有一销售表,表名是sale,它的结构如下:
| id int (标识号) codno char(7) (商品编码) codname varchar(30) (商品名称) spec varchar(20) (商品规格) price numeric(10,2) (价格) sellnum int (销售数量) deptno char(3) (售出分店编码) selldate datetime (销售时间) |
要求:写出查询销售时间段在2002-2-15日到2002-4-29之间,分店编码是01的所有记录。
___________:SELECT * FROM sale WHERE selldate >= '2002-2-15' AND selldate <='2002-4-29' AND deptno = '01'_____________________________________________________________________________
Statement 静态传参 拼接语句 不防SQL注入
PreparedStatement 动态传参(?),可防SQL注入
事务是一组不可分割的数据操作单元(里面可以有一条sql或多条SQL)
1、原子性 A。
* 一个事务,要么全部完成,要么全部不完成。
* 2、一致性 C。
* 在事务开始之前和事务结束之后,数据库的完整性没有被破坏。
* 3、隔离性 I
* 数据库允许多个事务同时对数据进行处理。每个事务之间是相互隔离。
* 4、持久性 D
* 事务结束以后,对数据的增删改是永久性的。
executeUpdate 用来执行修改,插入,删除操作,返回结果是int
executeQuery只能进行查询,返回结果是结果集(ResultSet)对象,
主键约束,外键约束,检查约束,唯一约束,默认约束
- CREATE TABLE account(
- id number PRIMARY KEY,
- `no` VARCHAR(30) UNIQUE,
- `password` VARCHAR(30),
- `name` VARCHAR(30),
- money number
- );
- public class CreateBankAccount {
- public Integer add(String id,Double money) {
- Connection conn = null;
- try {
- conn = JDBCUtil.getConnection();
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- int i = 0;
-
- String sql = "insert into account (id,money) values (?,?)";
- PreparedStatement preparedStatement = null;
- try {
- preparedStatement = conn.prepareStatement(sql);
- preparedStatement.setString(1,id);
- preparedStatement.setDouble(2,money);
-
- i = preparedStatement.executeUpdate();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } finally {
- JDBCUtil.close(conn,preparedStatement);
- }
- return i;
- }
- }
-
-
-
- package Util;
-
- import java.io.IOException;
- import java.sql.*;
- import java.util.Objects;
- import java.util.Properties;
-
- public class JDBCUtil {
-
- public static void close(Connection conn, Statement stmt, ResultSet rs) {
-
- if(Objects.nonNull(stmt)) {
- try {
- stmt.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if(Objects.nonNull(conn)){
- try {
- conn.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if(Objects.nonNull(rs)){
- try {
- rs.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- /**
- * 关闭链接的方法
- */
- public static void close(Connection conn, Statement stmt) {
-
- if(Objects.nonNull(stmt)) {
- try {
- stmt.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if(Objects.nonNull(conn)){
- try {
- conn.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- /**
- * 获取JDBC连接的方法
- * @return
- */
- public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException {
-
- Connection conn = null;
- Properties properties = new Properties();
-
- properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
- // 去jdbc.properties中取出属性名为mysql.url对应的值
- String url = properties.getProperty("mysql.url");
- String driverName = properties.getProperty("mysql.driverName");
- String username = properties.getProperty("mysql.username");
- String password = properties.getProperty("mysql.password");
-
- Class.forName(driverName);
- return DriverManager.getConnection(url, username, password)
- public class BankAccountTransfer {
- private final Connection conn;
- Account account = new Account();
- {
- try {
- conn = JDBCUtil.getConnection();
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public Integer transform(String out, String in, Double money) {
- // 取款之前要先查询
- ResultSet rs = null;
- PreparedStatement preparedStatement = null;
- PreparedStatement preparedStatement2 = null;
- double b = 0;
-
- String sql = "select money from account where id = ?";
-
- try {
- preparedStatement = conn.prepareStatement(sql);
- preparedStatement.setString(1, out);
- rs = preparedStatement.executeQuery();
- while (rs.next()) {
- b = rs.getDouble("money");
- }
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入收款的账号:");
- String s1 = sc.next();
- System.out.println("请输入收款的姓名:");
- String s2 = sc.next();
- System.out.println("请输入转账的金额:");
- String s3 = sc.next();
- if (b >= money && account.getId().equals(s1)) {
- // 余额够
- // 执行修改
- conn.setAutoCommit(false);
- sql = "update account set money = money - ? where id = ?";
- preparedStatement = conn.prepareStatement(sql);
- preparedStatement.setDouble(1, money);
- preparedStatement.setString((int) 2, out);
- int i = preparedStatement.executeUpdate();
-
- sql = "update account set money = money + ? where id = ?";
- preparedStatement2 = conn.prepareStatement(sql);
- preparedStatement2.setDouble(1, money);
- preparedStatement2.setString((int) 2, in);
- i = preparedStatement2.executeUpdate();
-
- conn.commit();
-
- return i;
-
- } else {
- // 余额不够
- throw new RuntimeException("余额不足,转账失败");
- }
-
- } catch (SQLException e) {
- try {
- conn.rollback();
- } catch (SQLException ex) {
- throw new RuntimeException(ex);
- }
- throw new RuntimeException(e);
- } finally {
- JDBCUtil.close(conn, preparedStatement, rs);
- JDBCUtil.close(null, preparedStatement2);
- }
- }
- }