1)什么是JDBC
java database connect(java连接数据库)
需要jar包的支持:
- Java.sql
- javax.sql
- mysql-connector-java
2)数据库建表
关键词重复用符号飘`围起来:
3)idea连接mysql
pom.xml导入依赖
<dependencies>
<!--mysql的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
连接成功:
4)jdbc固定步骤
Statement不安全的方式:
package com.kakafa.test;
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=utf-8解决中文乱码问题
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
String username="root";
String password="123456";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//向数据库发送SQL的对象Statement:CRUD增删改查
Statement statement = connection.createStatement();
//编写sql
String sql="select * from users";
//执行sql,返回一个结果集
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("id=" + resultSet.getObject("id"));
System.out.println("name=" + resultSet.getObject("name"));
System.out.println("password=" + resultSet.getObject("password"));
System.out.println("email=" + resultSet.getObject("email"));
System.out.println("birthday="+resultSet.getObject("birthday"));
}
//关闭连接释放资源
resultSet.close();
statement.close();
connection.close();
}
}
PreparedStatement安全的方式:
package com.kakafa.test;
import java.sql.*;
public class TestJDBC02 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
String username="root";
String password="123456";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//编写sql
String sql="insert into users(id,name,password,email,birthday)values(?,?,?,?,?)";
//预编译
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,3);//给第一个占位符?赋值为1
preparedStatement.setString(2,"李四");
preparedStatement.setString(3,"333333");
preparedStatement.setString(4,"14@qq.com");
preparedStatement.setString(5,"2020-04-05");
//执行sql
int i=preparedStatement.executeUpdate();
if (i>0){
System.out.println("插入成功");
}
//关闭连接释放资源
preparedStatement.close();
connection.close();
}
}
5)事务
要么都成功,要么都失败
ACID原则:保证数据的安全
- 开启事务
- 事务提交 commit()
- 事务回滚 rollback()
- 关闭事务
package com.kakafa.test;
import java.sql.*;
public class TestJDBC02 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
String username="root";
String password="123456";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//编写sql
String sql="insert into users(id,name,password,email,birthday)values(?,?,?,?,?)";
//预编译
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,3);//给第一个占位符?赋值为1
preparedStatement.setString(2,"李四");
preparedStatement.setString(3,"333333");
preparedStatement.setString(4,"14@qq.com");
preparedStatement.setString(5,"2020-04-05");
//执行sql
int i=preparedStatement.executeUpdate();
if (i>0){
System.out.println("插入成功");
}
//关闭连接释放资源
preparedStatement.close();
connection.close();
}
}