1. JDBC概念
1.1 jdbc 概念
JDBC: Java DataBase Connection ,就是使用java去连接数据库,对数据库中的数据进行增删改查的操作
就是使用程序去操作数据库。
1.2 jdbc流程图
流程:
(1)定义Mysql相关信息
(2)加载DriverManager类--来生产 Connection类型的连接对象
(3)Connection类型的连接对象--生产PreparedStatement类型对象
(4)PreparedStatement类型对象 --向数据库发送sql语句指令
(5)数据库接收到sql语句,执行sql操作
(6)数据库向程序端返回一个封装原始数据的ResultSet类对象
2. MySQL的JDBC
2.1 创建项目
1. 新建项目
2. 新建目录lib
3. 导入Mysql-connector-java......jar包到lib目录下
4. 右键lib选择Add as Library...导入驱动包
5. 在弹窗选择ok
2.2 创建程序
1. 在src下新建两个包
2.2.1 ConnectionUtil类
1.在ConnectionUtil包下新建ConnectionUtil类作为工具类
(1)新建ConnectionUtil类
(2) 属性:准备Mysql相关信息
//准备Mysql相关信息
//1.mysql地址
public static final String URL="jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncodinng=UTF-8";
//2.驱动地址
public static final String DRIVER="com.mysql.jdbc.Driver";
//3.Mysql用户名
public static final String USERNAME="root";
//4.密码
public static final String PASSWORD="xxxxxxxx";
(3) 加载驱动信息到程序中--使用静态代码块,在加载程序的时候就会自动执行
//5.加载驱动信息到程序中
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
(4) 方法:取得Connection 连接对象
//取得Connection连接对象,
public static Connection getConnection() throws Exception{
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
}
(5) 方法:关闭资源
//关闭资源
public static void close(Connection conn, PreparedStatement pst) throws Exception{
if(conn!=null){
conn.close();
}
if(pst!=null){
pst.close();
}
}
(6)完整代码!!!!!!!!!!!!!!!!!!!!!!!!
package com.ConnectionUtil;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Collections;
public class ConnectionUtil {
//准备Mysql相关信息
//1.mysql地址
public static final String URL="jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncodinng=UTF-8";
//2.驱动地址
public static final String DRIVER="com.mysql.jdbc.Driver";
//3.Mysql用户名
public static final String USERNAME="root";
//4.密码
public static final String PASSWORD="zx200061";
//5.加载驱动信息到程序中
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//取得Connection连接对象,
public static Connection getConnection() throws Exception{
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
}
//取得Connection连接对象,
public static void close(Connection conn, PreparedStatement pst) throws Exception{
if(conn!=null){
conn.close();
}
if(pst!=null){
pst.close();
}
}
}
2.2.2 JDBCTest类
1.在JDBCTest下面新建JDBCTest类作为客户端
(1)新建JDBCTest类
其中有增删改查三种操作,本质相同,只是其中的SQL语句不同。
1. 插入数据
//1.插入数据
public static int addEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "insert INTO emp (empno,ename,job,sal,hiredate,mgr,comm,deptno)" + "VALUES(1001,'Bob','Cleaner',2000.0,NOW(),7788,null,40)";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
2. 修改数据
//2.修改数据
public static int updateEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "Update emp SET comm=200 WHERE empno=1001";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
3. 删除数据
//3.删除数据
public static int deleteEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "DELETE FROM emp WHERE empno=1001";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
4.main方法调用
public static void main(String[] args) throws Exception{
addEmp();
//updateEmp();
//deleteEmp();
}
完整代码!!!!!!!!!!!!!!!!!!!!!!!
package com.JDBCtest;
import com.ConnectionUtil.ConnectionUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class JDBCTest {
public static void main(String[] args) throws Exception{
//addEmp();
//updateEmp();
deleteEmp();
}
//方法:对demo数据库中emp表的操作
//1.新增数据
public static int addEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "insert INTO emp (empno,ename,job,sal,hiredate,mgr,comm,deptno)" + "VALUES(1001,'Bob','Cleaner',2000.0,NOW(),7788,null,40)";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
//2.修改数据数据
public static int updateEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "Update emp SET comm=200 WHERE empno=1001";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
//删除数据
public static int deleteEmp() throws Exception{
Connection conn = null;
PreparedStatement pst=null;
try {
//获得连接对象
conn = ConnectionUtil.getConnection();
//准备sql语句
String sql = "DELETE FROM emp WHERE empno=1001";
//取得发送sql语句的对象
pst = conn.prepareStatement(sql);
//发送sql语句指令
return pst.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭资源
conn.close();
}
return 0;
}
}
2.3 执行代码
1. 增加数据empno为1001的员工记录
执行addEmp()方法
2. 修改empno为1001的员工记录comm为200
执行updateEmp()方法
3. 删除empno为1001的员工记录
执行deleteEmp()方法