整数类型
小数类型
(M, D)
来表示,其中M
称为精度,表示总共的位数;D
称为标度,表示小数的位数。
日期和时间类型
==字符串类型==
create database 数据库名称
create database if not exists 数据库名称
create database 数据库名称 character set 字符集名
show databases
show create database 数据库名称
alter database 数据库名称 character set 字符集名称
dorp database 数据库名称
drop database if exists 数据库名称
select database()
use 数据层名称
create table 表名 like 被复制的表名
show tables
desc 表名
alter table 表名 rename to 新的表名
alter table 表名 character set 字符集名称
alter table 表名 add 列名 数据类型
alter table 表名 change 列名 新列别 新数据类型
alter table 表名 drop 列名
drop table if exists 表名
insert into 表名(列名1,列名2..) values(值1,值2...)
delete from 表名 [where 条件]
update 表名 set 列名1=值1,列名2=值2,... [where 条件]
distinct
like '%字符%'
order by 排序字段1 排序方式1
create user '用户名'@'主机名' identified by '密码'
drop user '用户名'@'主机名'
set password for '用户名'@'主机名' =password('新密码')
use mysql
select * from user
show grants for '用户名'@'主机名'
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名'
grant all on *.* to '用户名'@'主机名'
revoke 权限列表 on 数据库.表名 from '用户名'@'主机名'
net stop mysql
mysqld --skip-grant-table
use mysql
update user set password = password('你的新密码') where user = 'root';
使用 SHOW CREATE TABLE <数据库表名>语句来查看表中的约束
alter table 表名 modify 字段 类型 not null
alter table 表名 modify 字段类型
alter table 表名 drop index 字段
alter table 表名 modify 字段 类型 unique
alter table 表名 drop primary key
alter table 表名 add primary key(字段)
alter table 表名 modify 字段 类型
alter table 表名 modify 字段 类型 auto_increment
alter table 表名 drop foreign key 外键名称
alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称)
存在问题:
url=jdbc:mysql:///数据库名
user=root
password=root
driver=com.mysql.jdbc.Driver
public class JdbcUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
//使用静态代码块,随着类的加载而执行,而且只执行一次
static {
try {
//创建Properties集合类
Properties pro = new Properties();
//获取src路径下的文件的方式--->ClassLoader 类加载器
ClassLoader loader = JdbcUtils.class.getClassLoader();
URL res = loader.getResource("jdbc.properties");
String path = res.getPath();
pro.load(new FileReader(path));
//pro.load(new FileReader("src/jdbc.properties"));
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
//释放资源
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
步骤: