一、JDBC概述
1. 概述:
JDBC是一种可用于执行SQL语句的JAVA API,是链接数据库和JAVA应用程序的纽带
2. 主要任务:
JDBC技术主要是完成以下几个任务:
①与数据库建立一个链接
②向数据库发送SQL语句
③处理从数据库返回的结果
需要注意的是,JDBC并不能直接访问数据库,必须以来数据库厂提供的JDBC驱动程序。
3.驱动程序类型:
JDBC的总体结构由4个组件——应用程序、驱动程序管理器、驱动程序和数据源组成。JDBC驱动基本上分为一下4种:
①JDBC-ODBC桥
②本地API一部分用Java编写的驱动程序
③JDBC网络驱动
④本地协议驱动
4.JDBC常用的类和接口:
Connection接口:
Connection接口代表与特定的数据库建立连接
Statement接口:
Statement接口用于在已经建立连接的基础上向数据库发送SQL语句。在JDBC中有三种Statement对象:Statement、PreparedStatement、CallableStatement。
Statement对象用于执行不带参数的简单的SQL语句;PreparedStatement对象用于执行动态的SQL语句;CallableStatement对象用于执行对数据库的存储过程的调用。
PreparedStatement接口:
PreparedStatement接口用来动态地执行SQL语句。
DriverManager类:
DriverManager类用来管理数据库中的所有驱动程序。
ResultSet接口:
ResultSet接口类似于一个临时表,用来暂时存放数据库查询操作所获得的结果集。
二、JDBC的具体实现
①加载驱动 Class.forName()加载驱动
②connection DriverManager获取connection连接
③statement
④resultset
⑤释放资源
具体案例:
package org.example;
import java.sql.*;
public class MASTER {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://192.168.153.132:3306/jdbcstudb",
"root",
"root"
);
// 增
// String sql = "insert into MASTER(name,age,gender,yearnum,did) value (?,?,?,?,?)";
// String name = "wojiagege";
// Integer age = 250;
// String gender = "unknown";
// Integer yearnum = 2500;
// Integer did = 5;
// preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setString(1,name);
// preparedStatement.setInt(2,age);
// preparedStatement.setString(3,gender);
// preparedStatement.setInt(4,yearnum);
// preparedStatement.setInt(5,did);
// int num = preparedStatement.executeUpdate();
// if (num>0){
// System.out.println("新增宠物狗成功!");
// }
// 改
// String sql = "update MASTER set name=?,age=?,gender=?,yearnum=?,did=? where pid=?;";
// String name = "chunluren";
// Integer age = 25;
// String gender = "nv";
// Integer yearnum= 2555;
// Integer did = 5;
// Integer pid = 5;
// preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setString(1,name);
// preparedStatement.setInt(2,age);
// preparedStatement.setString(3,gender);
// preparedStatement.setInt(4,yearnum);
// preparedStatement.setInt(5,did);
// preparedStatement.setInt(6,pid);
//
// int num = preparedStatement.executeUpdate();
// if (num>0){
// System.out.println("修改成功!");
// }
// 删
// String sql = "delete from MASTER where pid=?";
// preparedStatement = connection.prepareStatement(sql);
// Integer pid = 5;
// preparedStatement.setInt(1,pid);
// int num = preparedStatement.executeUpdate();
// if (num>0){
// System.out.println("删除成功!");
// }
//
// 查1
// String sql1 = "select pid,name,age,gender,yearnum,did from MASTER;";
// preparedStatement = connection.prepareStatement(sql1);
// resultSet = preparedStatement.executeQuery();
// System.out.println("主人信息");
// System.out.println("主人编号\t主人名字\t\t主人年龄\t主人性别\t经验值\t宠物id");
// while (resultSet.next()){
// System.out.print(resultSet.getInt("pid")+"\t\t");
// System.out.print(resultSet.getString("name")+"\t\t");
// System.out.print(resultSet.getInt("age")+"\t\t");
// System.out.print(resultSet.getString("gender")+"\t\t");
// System.out.print(resultSet.getInt("yearnum")+"\t\t");
// System.out.println(resultSet.getInt("did")+"\t\t");
// }
// 查2
// String sql2 = "select pid,name,age,gender,yearnum,did from MASTER where pid=?;";
// preparedStatement = connection.prepareStatement(sql2);
// preparedStatement.setInt(1,1);
// resultSet = preparedStatement.executeQuery();
// System.out.println("主人信息");
// System.out.println("主人编号\t主人名字\t\t主人年龄\t主人性别\t经验值\t宠物id");
// while (resultSet.next()){
// System.out.print(resultSet.getInt("pid")+"\t\t");
// System.out.print(resultSet.getString("name")+"\t\t");
// System.out.print(resultSet.getInt("age")+"\t\t");
// System.out.print(resultSet.getString("gender")+"\t\t");
// System.out.print(resultSet.getInt("yearnum")+"\t\t");
// System.out.println(resultSet.getInt("did")+"\t\t");
// }
// 查3
// String sql2 = "select m.pid,m.name,m.age,m.gender,m.yearnum,m.did,d.name,d.strain from MASTER m left join dog d on m.did=d.id;";
// preparedStatement = connection.prepareStatement(sql2);
// resultSet = preparedStatement.executeQuery();
// System.out.println("主人信息");
// System.out.println("主人编号\t主人名字\t\t主人年龄\t主人性别\t经验值\t宠物id");
// while (resultSet.next()){
// System.out.print(resultSet.getInt("m.pid")+"\t\t");
// System.out.print(resultSet.getString("m.name")+"\t\t");
// System.out.print(resultSet.getInt("m.age")+"\t\t");
// System.out.print(resultSet.getString("m.gender")+"\t\t");
// System.out.print(resultSet.getInt("m.yearnum")+"\t\t");
// System.out.print(resultSet.getInt("m.did")+"\t\t");
// System.out.print(resultSet.getString("d.name")+"\t\t");
// System.out.println(resultSet.getString("d.strain")+"\t\t");
// }
// String sql = "insert into vip(product,price) value(?,?);";
// String product = "meirong";
// Integer price = 499;
// preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setString(1,product);
// preparedStatement.setInt(2,price);
// int num = preparedStatement.executeUpdate();
// if (num>0){
// System.out.println("项目添加完成!");
// }
String sql = "select m.pid,m.name,d.name,d.strain,v.product,v.price from MASTER m left join dog d on m.did=d.id left join vip v on m.pid=v.pid;";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.print(resultSet.getInt("m.pid")+"\t");
System.out.print(resultSet.getString("m.name")+"\t");
System.out.print(resultSet.getString("d.name")+"\t");
System.out.print(resultSet.getString("d.strain")+"\t");
System.out.print(resultSet.getString("v.product")+"\t");
System.out.println(resultSet.getString("v.price")+"\t");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}