您的位置:首页 > 数据库

正则表达式与SQL笔记

2016-07-05 08:16 239 查看
一:正则表达式 (regex)

正则表达式描述了匹配的模式。

1.常用匹配模式:

\ 匹配反斜线

\d 匹配数字 \D 匹配非数字

\w 匹配字母、数字、下划线

^ 匹配行的开始

$ 匹配行的结尾

. 匹配除了”.”字符本身之外的所有字符

\n 匹配换行

[0-9] 匹配某个数字

[a-z] 匹配小写字母

2.常用匹配数量

? 匹配0次或1次

+ 匹配1次或1次以上(至少1次)

* 匹配任意次

{n} 匹配n次

{n,} 匹配至少n次

3.匹配逻辑

| 匹配“或”逻辑

& 匹配“与”逻辑

Java常用的正则工具类:

Pattern类

正则表达式的编译表示形式。

public static Pattern compile(String regex) 将给定的正则表达式编译到模式中。

public Matcher matcher(CharSequence input) 创建匹配器

Matcher类 (匹配器)

public boolean matches() 执行匹配,如果匹配成功,返回true.

public String group()返回已经匹配的子字符串。

boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列。

正则表达式的步骤:

(1)、String str=”1234a56789”;

Pattern pattern = Pattern.compile(“\d+”);// 将正则表达式编译到Pattern中

(2)、Matcher matcher = pattern.matcher(str);// 将编译好的Pattern对象创建匹配器

(3)、boolean matches = matcher.matches();// 执行匹配

String类支持正则的方法:

1.public String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。

2.public boolean matches(String regex) 告知此字符串是否匹配给定的正则表达式。

3.public String replaceAll(String regex,String replacement)

使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串。

二:SQLite数据库管理系统

SQLite是一种小型的、轻量级、零配置的关系型数据库,常嵌入于便携式设备(eg:手机);

sqlite3 数据库文件名 打开或创建数据库文件

SQLite的“点命令”:

.database 显示数据库文件

.table 查看表

.schema 表名称 查看某表的结构(建表语句)

.exit 退出SQLite环境

.mode column 开启列模式

.header on 显示表头

常用SQL语句:

1.建表语句

create table 表名称(

主键字段 数据类型 primary key [autoincrement],

字段2 数据类型 约束 (eg: not null),



);

2.插入数据

insert into 表名称(字段1,字段2….)values(值1,值2,…);

3.查询数据

select 字段1,字段2,… from 表名称 [where 字段名 操作符 字段值 and xxx] [order by 字段名]

(注:模糊查询 where name like ? 后面的setString(1,”%”+name+”%” 就可以了))

4.修改数据

update 表名称 set 字段名1=值1,字段名2=值2… [where 字段名 操作符 字段值 and xxx]

5.删除数据

delete from 表名称 [where 字段名 操作符 字段值 and xxx]

6.删除表

drop table 表名称;

7.增加列

alter table 表名称 add column 字段名 数据类型 约束;

三:JDBC(JavaDatabase Connection)

1.DriverManager 驱动管理类

public static Connection getConnection(String url) 试图建立到给定数据库URL的连接

2.Connection 数据库连接类

PreparedStatement prepareStatement(String sql)

3.PreparedStatement 表示预编译的SQL语句的对象。

setString(int parameterIndex,String x)

setInt(int parameterIndex,int x)

setXXX(xxx)

int executeUpdate() 执行增、删、改的操作。

ResultSet executeQuery() 执行查询,返回结果集

4.ResultSet 查询结果集

boolean next()

将光标下移,如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false

String getString(String columnLabel)

Class.forName(“org.sqlite.JDBC”);// 加载数据库驱动程序

connection=DriverManager.getConnection(“jdbc:sqlite:/g:/1000phone/test/mydb.db”);// 获取数据库的连接

String insertsql=”insert into student (name,age,score)values(“xiao”,25,85.5)”;

ps=connection.prepareStatement(insertsql);// 获取数据库操作对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: