目录
JDBC是java操作提供的一套用于数据库操作的接口API,java程序员只需要面向这套接口编程计科。不同的数据库厂商,需要针对这套接口,提供不同实现。
JDBC API 是一系列接口,她同一和规范了应用程序于数据的连接,执行SQL语句,并得到返回结果等各种操作,相关类和接口在java.sql与 javax.sql 包中

JDBC程序编写步骤
- package com.tianedu.jdbc;
-
- import com.mysql.jdbc.Driver;
-
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
-
- /**
- * @author tian
- * 这是第一个JDBC程序 完成简单的操作
- */
- public class Jdbc01 {
- public static void main(String[] args) throws SQLException {
- // 前置工作在项目下创建一个文件夹 libs
- // 将 mysql.jar 拷贝到该目录下,点击 add to project 加入到项目中才可以使用
-
- //1.注册驱动
- Driver driver = new Driver(); // 创建driver 对象
- //2.得到连接
- //(1) jdbc: // 规定好的协议,通过jdbc的方式连接mysql
- //(2) localhost 主机,也可以是ip地址
- //(3) 3306 表示MySQL 监听的端口
- //(4) hsp_db03 表示连接到呢个数据库
- // (5) MySQL的连接本质就是前面学习过sql 的连接
- String url = "jdbc:mysql://localhost:3306/hsp_db03";
- // 将用户名和密码 放到Properties 对象中
- Properties properties = new Properties();
- // 说明: user 和 password 是规定好的,后面的值根据实际情况写
- properties.setProperty("user","root"); // 用户
- properties.setProperty("password","tian"); //密码
-
- Connection connect = driver.connect(url, properties);
- //3.执行sql语句
- //String sql = "insert into actor values(null,'刘德华','男','1970-11-11',110)";
- // String sql = "update actor set name = '周星驰'where id = 1";
- String sql = "delete from actor where id = 1";
- // 下面的statement 用于执行静态的sql 语句并返回其生成的结果的对象
- Statement statement = connect.createStatement();
- int rows = statement.executeUpdate(sql); // 如果是dml语句,返回的就是影响行数 如果是1 添加成功 如果是0 添加失败
- System.out.println(rows > 0 ? "成功":"失败");
- //4.关闭资源
- statement.close();
- connect.close();
- }
- }
获取数据库连接5种方式
- package com.tianedu.jdbc;
-
- import com.mysql.jdbc.Driver;
- import org.junit.Test;
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
-
- /**
- * @author tian
- * 分析java 连接MySQL的物种方式
- */
- public class JdbcConn {
- @Test
- public void connect01() throws SQLException {
- Driver driver = new Driver();
- String url = "jdbc:mysql://localhost:3306/hsp_db03";
- // 将用户名和密码放入到Properties 对象
- Properties properties = new Properties();
- properties.setProperty("user", "root");
- properties.setProperty("password", "tian");
- Connection connect = driver.connect(url, properties);
- System.out.println(connect);
- }
- //方式2
- @Test
- public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
- //使用反射加载Driver 动态加载,更加灵活,减少依赖性
- Class> aClass = Class.forName("com.mysql.jdbc.Driver");
- Driver driver = (Driver)aClass.newInstance();
- String url = "jdbc:mysql://localhost:3306/hsp_db03";
- // 将用户名和密码放入到Properties 对象
- Properties properties = new Properties();
- properties.setProperty("user", "root");
- properties.setProperty("password", "tian");
-
- Connection connect = driver.connect(url, properties);
- System.out.println("方式2=" + connect);
- }
- // 方式3 使用DriverManager 替代 Driver 进行统一管理
- @Test
- public void connect03() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
- // 使用反射 加载Driver
- Class> aClass = Class.forName("com.mysql.jdbc.Driver");
- Driver driver = (Driver) aClass.newInstance();
- //创建 url 和 user 和 password
- String url = "jdbc:mysql://localhost:3306/hsp_db03";
- String user = "root";
- String password = "tian";
- DriverManager.registerDriver(driver); //注册Driver 驱动
- Connection connection = DriverManager.getConnection(url, user, password);
- System.out.println("第三种方式" + connection);
- }
- // 方式4 使用Class.forName 自动完成注册,简化代码
- // 使用最多
- @Test
- public void connect04() throws ClassNotFoundException, SQLException {
- // 使用反射加载 Driver 类
- // 在加载Driver类时,完成注册
- /*
- 源码: 1.静态代码块,在类加载时,会执行一次
- 2.DriverManager.registerDriver(new Driver());
- 3.因此 加载和注册Driver 的工作已经完成
- static {
- try {
- DriverManager.registerDriver(new Driver());
- } catch (SQLException var1) {
- throw new RuntimeException("Can't register driver!");
- }
- }
- */
-
- Class.forName("com.mysql.jdbc.Driver") ; //如果没有这句也可以执行,建议写上更加明确
- // MySQL 驱动 5.1.6 可以无需Class.forName("com.mysql.jdbc.Driver");
- // 从 jdk1.5 以后使用Jdbc4 不在需要显示调用class.forName() 注册驱动而已自动调用驱动
- // jar包下META-INF\services\java.sql.Driver文本种的类 名称去注册
- String url = "jdbc:mysql://localhost:3306/hsp_db03";
- String user = "root";
- String password = "tian";
- Connection connection = DriverManager.getConnection(url,user,password);
- System.out.println("第四种方式" + connection);
- }
- @Test
- // 方式5,在方式4的基础上改进,增加配置文件,让信息连接MySQL更加灵活
- public void connect05() throws IOException, ClassNotFoundException, SQLException {
- //通过Properties 对象获取配置文件的信息
- Properties properties = new Properties();
- properties.load(new FileInputStream("src\\mysql.properties"));
- // 获取相关的值
- String user = properties.getProperty("user");
- String password = properties.getProperty("password");
- String driver = properties.getProperty("driver");
- String url = properties.getProperty("url");
- Class.forName(driver); //建议写上 更加明确
- Connection connection = DriverManager.getConnection(url, user, password);
- System.out.println("方式5" + connection);
- }
- }
- package com.tianedu.jdbc.resultest_;
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.*;
- import java.util.Properties;
-
- /**
- * @author tian
- *
- * 演示select 语句返回一个resultset 并取出结果
- */
- @SuppressWarnings({"all"})
- public class ResultSet_ {
- public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
- //通过Properties 对象获取配置文件
- Properties properties = new Properties();
- properties.load(new FileInputStream("src\\mysql.properties"));
- // 获取相关的值
- String user = properties.getProperty("user");
- String password = properties.getProperty("password");
- String driver = properties.getProperty("driver");
- String url = properties.getProperty("url");
- Class.forName(driver); //建议写上 更加明确
- Connection connection = DriverManager.getConnection(url, user, password);
- //得到Statement
- Statement statement = connection.createStatement();
- //组织sql 语句
- String sql = "select id,name,sex,borndate from actor";
- //执行给定的sql语句,该语句返回单个 ResultSet对象
- ResultSet resultSet = statement.executeQuery(sql);
- // 使用where 循环取出数据
- while (resultSet.next()) {
- // 让光标向后移动 如果没有更多的记录则返回false
- int id = resultSet.getInt(1);
- String name = resultSet.getString(2);// 获取该行第二列
- String sex = resultSet.getString(3);
- Date date = resultSet.getDate(4);
- System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
- // 获取该行的对第一列
- }
- // 关闭连接
- resultSet.close();
- connection.close();
- statement.close();
-
- }
- }
- package com.tianedu.jdbc.statement_;
-
- import java.io.FileInputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.Scanner;
-
- /**
- * @author tian
- * 演示statement 的注入问题
- */
- @SuppressWarnings({"all"})
- public class Statement_ {
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- //让用户输入管理员姓名和密码
- System.out.print("请输入管理员的名字:");
- String admin_name = scanner.nextLine(); //next当接受到空格或者 ' 表示结束
- System.out.print("请输入管理员的密码:");
- String admin_pwd = scanner.nextLine();
- // 通过Properties 对象获取配置文件的信息
- Properties properties = new Properties();
- properties.load(new FileInputStream("src\\mysql.properties"));
- // 获取相关的值
- String user = properties.getProperty("user");
- String password = properties.getProperty("password");
- String driver = properties.getProperty("driver");
- String url = properties.getProperty("url");
- //1.注册驱动
- Class.forName(driver);
-
- //2.得到连接
- Connection connection = DriverManager.getConnection(url,user,password);
- //3.得到Statement
- Statement statement = connection.createStatement();
- //4.组织sql
- String sql = "select name,pwd from admin where name = '"
- +admin_name+"' and pwd= '"+admin_pwd+"'" ;
- ResultSet resultSet = statement.executeQuery(sql);
- if(resultSet.next()){
- //如果查询到一条记录,则说明该管理员存在
- System.out.println("登录成功");
- } else {
- System.out.println("对不起,登录失败");
- }
- // 关闭连接
- resultSet.close();
- statement.close();
- connection.close();
- }
-
- }
预处理好处
- package com.tianedu.jdbc.preparedstatement_;
-
- import java.io.FileInputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.util.Properties;
- import java.util.Scanner;
-
- /**
- * @author tian
- *
- * 演示 preparedStatement使用
- */
- @SuppressWarnings({"all"})
- public class PreparedStatementDML_ {
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- //让用户输入管理员姓名和密码
- System.out.print("请输入添加的名字:");
- String admin_name = scanner.nextLine(); //next当接受到空格或者 ' 表示结束
- // System.out.print("请输入管理员的密码:");
- //String admin_pwd = scanner.nextLine();
- //通过Properties
- Properties properties = new Properties();
- properties.load(new FileInputStream("src\\mysql.properties"));
-
- //获取相关的值
- String user = properties.getProperty("user");
- String password = properties.getProperty("password");
- String driver = properties.getProperty("driver");
- String url = properties.getProperty("url");
-
- // 注册驱动
- Class.forName(driver);
- //得到连接
- Connection connection = DriverManager.getConnection(url,user,password);
- // 组织sql sql语句的 ? 相当于占位符
- // 添加一个记录
- //String sql = "insert into admin values(?,?)";
- //String sql = "update admin set pwd = ? where name = ?";
- String sql = "delete from admin where name = ?";
- //3.2 preparedStatement 对象实现了 preparedStatement 接口的实现类的对象
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
- //3.3 给?赋值
- preparedStatement.setString(1,admin_name);
- //preparedStatement.setString(1,admin_pwd);
- //4.执行DML 语句使用 executeUpdate
- int rows = preparedStatement.executeUpdate();
- System.out.println(rows > 0 ? "执行成功":"执行失败");
- //关闭连接
- preparedStatement.close();
- connection.close();
- }
- }
-