链接:Mac安装mysql:https://www.jianshu.com/p/07a9826898c0
登陆:终端--> mysql -u 用户名 -p 回车输入密码
输入?号:表示MySql有哪些命令可拱我们使用
创建数据库: CREATE DATAVASE 数据库名;
删除数据库:DROP DATABASE 数据库名;
使用数据库:USE 数据库名;
创建表: CREATE TABLE mytab(
id INT AUTO_INCREMENT PRIMARY KEY, //自动增长,主键
name VARCHAR(30) NOT NULL, //非空
password VARCHAR(32) NOT NULL, //非空
age INT NOT NULL //非空
) ;
删除表:DROP TABLE 表名;
查看表结构:DESC 表名;
显示所有数据库:SHOW DATABASES;
显示所有表:SHOW TABLES;
SQL是一门结构化查询语言,SQL是关系型数据库管理系统的标准语言,在现在的开发中基本都支持标准的SQL语法.
增加
insert into 表名(列名) values(值 );
insert into user(name,password) values(‘Fhvk’,‘LCW’);
删除
delete from 表名 where 列名=值;
//表示删除name为LCW的记录
delete from user where name=‘LCW’;
//表示删除所有记录
delete from user;
查询
简单查询:
select * from 表名; //查询所有记录
select 列名 from 表名; //查询所有记录的指定列
select * from 表名 where 列名=值; //表示查询该列名为该值的记录
模糊查询
//查询所有记录中name列带’m’的记录,或password列带’m’的记录
select * from user where name like ‘%m%’ OR passwrod like ‘%m%’;
分页查询
//查询0开始的5条记录
select * from user limit 0,5;
修改
update 表名 set 列名=值,列名=值… where 列名=值;
update user set nam=‘FHVK’ where id=1;
package com.lichaowu.jdbc;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class Demo05_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
//定义mysql的数据库驱动程序
//public static final String DBDRIEVER = "org.gjt.mm.mysql.Driver";
//定义数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
//定义数据库用户名
public static final String DBUSER = "root";
//定义数据库密码
public static final String DBPASS = "123456";
//所有异常抛了
public static void main(String[] agrs) throws Exception{
String name = "陈清香"; //定义姓名
String password = "1234"; //定义密码
int age = 19; //定义年龄
String sex = "女"; //定义性别
//定义生日
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("1998-03-21");
java.sql.Date birthday = new java.sql.Date(d.getTime());
//定义sql语句
String sql = "insert into user(name,password,age,sex,birthday) values(?,?,?,?,?)";
//数据库的连接
Connection conn = (Connection)DriverManager.getConnection(DBURL,DBUSER,DBPASS);
//实例化PreparedStatement对象
PreparedStatement pstmt = (PreparedStatement)conn.prepareStatement(sql);
pstmt.setString(1, name); //从1开始
pstmt.setString(2, password);
pstmt.setInt(3, age);
pstmt.setString(4, sex);
pstmt.setDate(5, birthday);
int index = pstmt.executeUpdate();
System.out.println(index + "条记录受影响!");
pstmt.close();
conn.close();
}
}
package com.lichaowu.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.Date;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Demo06_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//定义数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL";
//定义数据库用户名
public static final String DBUSER = "root";
//定义数据库密码
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
//定义模糊查询关键字
String keyWord = "F";
String sql = "select id,name,password,age,sex,birthday from user where password like ?";
//连接数据库
Connection conn = (Connection)DriverManager.getConnection(DBURL, DBUSER, DBPASS);
//操作数据库
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, "%" + keyWord + "%");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
Date d = rs.getDate(6);
System.out.println(id + ", " + name + ", " + password + ", " + age + ", " + sex + ", " + d);
}
rs.close();
ps.close();
conn.close();
}
}
package com.lichaowu.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Date;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Demo07_JDbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//定义数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
//定义数据库用户名
public static final String DBUSER = "root";
//定义数据库密码
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
//连接数据库
Connection conn = (Connection)DriverManager.getConnection(DBURL, DBUSER, DBPASS);
//操作数据库
PreparedStatement ps = (PreparedStatement)conn.prepareStatement("select id,name,password,age,sex,birthday from user");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
Date d = rs.getDate(6);
System.out.println(id + ", " + name + ", " + password + ", " + age + ", " + sex + ", " + d);
}
}
}
cretae table userclob(
id int auto_increment primary key,
name varchar(30) not null,
note longtext --存储大文本数据最大为4G
);
public class Demo08_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
e.printStackTrace();
}
}
//数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
//数据库用户名
public static final String DBUSER = "root";
//数据库密码
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
//定义名称
String name = "李超武";
//定义备注
File f = new File("Text.text");
FileInputStream fis = new FileInputStream(f);
//连接数据库
Connection conn = (Connection)DriverManager.getConnection(DBURL,DBUSER,DBPASS);
//Sql语句
String sql = "insert into userclob(name,note) values(?,?)";
//操作数据库
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
//设置
ps.setString(1, name);
ps.setAsciiStream(2, fis, (int)f.length());
int index = ps.executeUpdate();
System.out.println(index + "条插入成功!");
ps.close();
conn.close();
}
}
public class Demo09_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
e.printStackTrace();
}
}
//数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
//数据库用户名
public static final String DBUSER = "root";
//数据库密码
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
int id = 1;
String sql = "select id,name,note from userclob where id = ?";
//连接数据库
Connection conn = (Connection)DriverManager.getConnection(DBURL,DBUSER,DBPASS);
//操作数据库
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
int iud = rs.getInt(1);
String naem = rs.getString(2);
InputStream is = rs.getAsciiStream(3);
StringBuffer note = new StringBuffer();
Scanner snote = new Scanner(is);
snote.useDelimiter("\r\n");
while(snote.hasNextLine()) {
note.append(snote.next()).append("\n");
}
System.out.println("内容: " + note);
is.close();
}
rs.close();
ps.close();
conn.close();
}
}
create table userblob(
id int auto_increment primary key,
name varchar(30) not null,
photo longblob. -- 存放二进制文件如图片,最大为4G
);
package com.lichaowu.jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Demo11_JDbc {
//加载驱动
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
e.printStackTrace();
}
}
//数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
//数据库用户名
public static final String DBUSER = "root";
//数据库密码
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
String name = "李超武";
String sql = "insert into userbolb(name,photo) values(?,?)";
Connection conn = (Connection)DriverManager.getConnection(DBURL,DBUSER,DBPASS);
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setString(1, name);
File f = new File("BLOB.jpg");
FileInputStream fis = new FileInputStream(f);
ps.setBinaryStream(2, fis, (int)f.length());
ps.executeLargeUpdate();
ps.close();
conn.close();
}
}
public class Demo12_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
e.printStackTrace();
}
}
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
public static final String DBUSER = "root";
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
int id = 1;
String sql = "select name,photo from userbolb";
Connection conn = (Connection)DriverManager.getConnection(DBURL,DBUSER,DBPASS);
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String name = rs.getString(1);
System.out.println("姓名: " + name);
InputStream is = rs.getBinaryStream(2);
File f = new File("QQ.jpg");
FileOutputStream fos = new FileOutputStream(f);
byte[] arr = new byte[1024];
int len = 0;
while((len = is.read(arr)) != -1) {
fos.write(arr, 0, len);
}
is.close();
fos.close();
}
rs.close();
ps.close();
conn.close();
}
}
package com.lichaowu.jdbc;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Blob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Demo13_Jdbc {
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
e.printStackTrace();
}
}
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn?useSSL=true";
public static final String DBUSER = "root";
public static final String DBPASS = "123456";
public static void main(String[] agrs) throws Exception{
int id = 1;
String sql = "select name,photo from userbolb where id = ?";
Connection conn = (Connection)DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String name = rs.getString(1);
Blob b = (Blob)rs.getBlob(2);
File f = new File("QQ1.jpg");
FileOutputStream fos = new FileOutputStream(f);
fos.write(b.getBytes(1, (int)b.length()));
fos.close();
}
rs.close();
ps.close();
conn.close();
}
}
以上的操作不经常使用不如直接通过PreparedStatement
1、取出掉自动提交,每次执行数据库更新的时候实际上发出SQL命令之后就已经提交上去了
2、开始事务
3、进行一系列操作
4、如果操作全部合格,则提交事务
5、如果发现一个地方有问题,则可以进行回滚。
6、或者设置一个SAVEPOINT保存事务的提交点
1、取消掉Connection中设置的自动提交方式:conn.setAutoCommit(false);
2、如果批处理操作成功,则执行提交事务;conn.commit();
3、如果操作失败,则肯定会引发异常,在异常处理中让事务回滚。conn.rollback();
如果需要可以设置回滚位置,Savepoint sp = conn.setSavepoint();
在正常情况下,可以通过SAVEPOINT保存事务的操作点,因为默认情况下所有的回滚,就是将全部的操作取消掉,而通过Savepoint可以设置回滚的位置。
一个session的操作(每一个连接到数据库上的用户都称为一个session)
操作1
操作2
Savepoint 记录点
操作3
操作4
操作5
rollback 记录点
下面用代实现
总结:1、事务的基本概念:如何在数据库中处理事务。
2、JDBC操作事务的步骤:取消自动提交,执行多条sql语句,如果没有异常,则提交事务,如果有异常,则回滚操作
略。。。。。