课下作业补做

一、加载JDBC-数据库驱动。

    try{ Class. forName("com.mysql.jdbc.Driver");
        }
    catch(Exception e){}

二、连接数据库

    Connection con;
    String uri =
    "jdbc:mysql://192.168.100.1:3309/name?user=root&password=&useSSL=true";
    try{
        con = DriverManager.getConnection(uri);
    }
    catch(SQLException e){
        System.out.println(e);
    }

三、常规操作

向数据库发送SQL查询语句

try {Statement sql=con.createStatement();
}
catch(SQLException e) {}
处理查询结果,返回一个ResultSet对象

ResultSet rs = sql.executeQuery{"SELCET * FROM student");
关闭链接

con.close();

四、条件与排序查询

一般格式

select 字段 from 表名 where 条件
字段和固定值比较

select name from mess where name = ''
字段在某个区间范围

select * from mess where height>1.60 and height<=1.8
使用某些特殊的日期函数

select * from time_list where second(shijian)=56
用操作符like进行模式匹配

select * from mess where name like '%林%'
排序:用order by子语句对记录进行排序

selct * from mess order by height

课上内容补做

11章代码解析

Example11_1:查询student数据库中的mess表的全部记录。

Example11_2:随机查询students数据库中mess表中的两条记录,首先将光标移到最后一行,然后再获取最后一行的行号,以便获取表中的记录数目

Example11_3:查询mess表中姓张,身高大于16.5,出生年份为2000或2000年之前,月份在七月份之后的学生,并按出生日期排序。

Example11_4:向mess表中插入如下两条记录:

R11,将三,2000-10-23

R10,李武,1989-7-22

Example11_5:使用预处理语句向mess表添加并查询了姓张的记录

Example11_6:将数据库名以及SQL语句传递给Query类的对象,用表格显示所查到的记录

Example11_7:将mess表中的number字段R1001的height值减少n,并将减少的n增加到字段是R1002的height上。

Example11_8:使用Derby数据库管理系统创建了名字是students的数据库,并在数据库中建立了成绩表。

原文地址:https://www.cnblogs.com/gstgst/p/8910983.html