jdbc和数据库的应用

jdbc是Java Data Base Connectivity(java数据库连接):

  1. 是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
  2. JDBC关键字的使用:

         1.DriverManager:依据数据库的不同,管理JDBC驱动

         2.Connection:负责连接数据库并且担任传送数据库的任务

         3.Statement:由Connection产生、负责执行SQL语句

         4.ResultSet:负责保存Statement执行后所产生的查询结果

         5.PreparedStatement接口(预编译的SQL语句)提高了SQL语句的性能、代码的安全性、代码的可读性和可维护性

  3. 简单来说它有5个步骤来完成java到数据库的连接:                                                                                                                                                                                                    (1)加载驱动(Class.forName(com.mysql.jdbc.Driver))                                                                                                                                                                                          (2)搭建一条java到数据库的通道 Connection conn =DriverManager.getConnection(url, user, password);                                                                                                                      (3)通过Statement stmt = conn.createStatement(); 来向数据库增删改;                                                                                                                                                                  (4)通过 ResultSet rs = stmt.executeQuery(sql);  来查询数据库数据                                                                                                                                                                        (5)关闭资源
  4. tatement常用方法:

       ResultSet executeQuery(String sql):执行SQL查询并且获取ResultSet对象

       Int executeUpdate(String sql):可以执行插入、删除、更新等操作,返回值是执行该操作所影响的行数

       Boolean execute(String sql):可以执行任意SQL语句,然后获得一个布尔值,表示是否返回ResultSet

       Boolean next():将光标从当前位置向下移动一行

       Boolean previous():游标从当前位置向上移动一行

       Void close():关闭ResultSet对象

       Int getInt(int colIndex):以int形式获取结果集当前行指定列号值

       Int getInt(String colLabel):以int形式获取结果集当前行指定的列名值

       Float getFloat(int colIndex):以float形式获取结果集当前行指定列号值

       Float getFloat(String colLabel):以float形式获取结果集当前指定列名值

      String getString(int colIndex):以Sting形式获取当前行指定列号值

      String getString(String colLabel):以String形式获取当前行指定列名值

5. 数据库表的管理和数据的管理

 管理表:      

             选择数据库:use 数据库;

              增加: create table 表(字段名1 字段类型,字段名2 字段类型......);

              删除: drop table 表;

               修改:           

              添加字段: alter table 表 add [column] 字段名 字段类型;           

               删除字段:   alter table 表 drop [column] 字段名;

                修改字段类型: alter table 表 modify 字段名 新的字段类型;

                修改字段名称 : alter table 表 change 旧字段名 新字段名 字段类型;

                修改表名称:   alter table 表 rename [to] 新表名;

                  查询:

                 show tables  /  desc student;

  管理数据:

              增加: insert into 表(字段1,字段2,。。。) values(值1,值2.。。。。);

               删除: delete from 表 where 条件;

               修改: update 表 set 字段1=值1,字段2=值2...... where 条件;

               查询:

                5.1)所有字段: select * from 表;

                5.2)指定字段: select 字段1,字段2.... from 表;

                5.3)指定别名: select 字段1 as 别名 from 表;

                5.4 )合并列: select (字段1+字段2) from 表;

                5.5)去重: select distinct 字段 from 表;

                5.6)条件查询:

                          a)逻辑条件 :and(与)     or(或)

                              select * from 表 where 条件1 and/or 条件2

                         b)比较条件: >  <  >=  <=  =  <>   between and(在。。。之间)

                              select * from 表 where servlet>=90;

                            c)判空条件:

                                判断null: is null   /  is not null

                                判断空字符串: =''    /  <>''

                            d)模糊条件: like

                                 %:  替换任意个字符

                                 _:   替换一个字符                     

                   5.7 分页查询:limit 起始行,查询行数

                                 起始行从0开始

                    5.8 排序: order by 字段 asc/desc

                                   asc: 正序,顺序

                                  desc:反序,倒序

                    5.9 分组查询:group by 字段

                    5.10: 分组后筛选: having 条件

                                                       

                                     SQL语句的分类:          

                                               DDL: 数据定义语言

                                                                 create / drop / alter       

                                               DML:数据操作语句

                                                                 insert / delete /update / truncate

                                               DQL: 数据查询语言:

                                                 select / show                              

原文地址:https://www.cnblogs.com/guorui1995/p/6180043.html