JDBC:是官方定义大的一套操作所有关系型数据库的规则,即接口
MySQL驱动jar包的下载方式:
地址:https://dev.mysql.com/downloads/
选择platform independent下载
JDBC使用步骤:
1.导入驱动jar包:复制到项目下的libs目录下,并且Add As Library
2.注册驱动
3.获取数据库连接对象Connection
4.定义sql
5.获取执行sql语句的对象statement
6.执行sql,接收返回结果
7.处理结果
8.释放资源
给数据表插入一条数据:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcdem2 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.定义sql
String sql = "insert into student values('zdbc','zdbc')";
//3.获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/live","root","1234");
//4.获取执行sql的对象
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql);
//6.处理结果
System.out.println(count);
if(count>0){
System.out.println("添加成功");
}
else {
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//8.释放资源
if(stmt!=null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
查询数据(查询结果对象也需要释放资源):
package test;
import java.sql.*;
public class jdbcdem3 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
ResultSet rs =null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.定义sql
String sql = "select * from student";
//3.获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///live","root","1234");
//4.获取执行sql的对象
stmt = conn.createStatement();
//5.执行sql
rs = stmt.executeQuery(sql);
//6.处理结果
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("num");
String pass = rs.getString("pass");
System.out.println(name+'-'+pass);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//8.释放资源
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
防止SQL注入:使用prepareStatement对象并且使用?代替参数
JDBC控制事务:
1.事务:一个包含多个步骤的业务操作。如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败。
2.操作:
1.开启事务
2.提交事务
3.回滚事务
3.使用connection对象来管理事务(在操作数据库前开启事务,操作后关闭事务,在try catch当中回滚事务)
数据库连接池:当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将对象归还给容器
实现方法:
1.标准接口:DataSource javax.sql包下的
方法:
获取连接:getConnection()
归还连接:Connection.close()。如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接,而是归还连接
数据库连接池Druid的使用方法:
1.导入jar包
2.定义配置文件
是properties形式的
可以叫任意名称,可以放在任意目录下
3.获取数据库连接池对象:通过工厂来获取 DruidDataSourceFactory
4.获取连接getConnection
package test;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class jdbcdem3 {
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.定义配置文件,把druid.preoperties复制到src目录下
//3.加载druid.preoperties配置文件
Properties pro = new Properties();
//获取当前项目下的src目录下的某个文件的字节流
InputStream is = jdbcdem3.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//4.获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//5.获取连接
Connection conn = ds.getConnection();
System.out.println(conn);
//6.定义SQL
// String sql = "insert into student values(?,?)";
String sql = "insert into student values('bbb','bbb')";
//7.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// //8.给问号赋值
// pstmt.setString(1,"aa");
// pstmt.setString(2,"aa");
//9.执行sql
int count = pstmt.executeUpdate(sql);
System.out.println(count);
//10.释放资源
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}