javaSE(7)——JDBC

JDBC

引言

实体包名 映射 数据库表:entity, bean, pojo, model, vo, orm
dao, controller, service,

JDBC概述:
为什么要使用JDBC?
SQL语句能够直接访问数据库,但是在JAVA中谁来操作SQL语句?

什么是JDBC?
● Java DataBase Connectivity
● 是一种用于执行SQL语句的Java API, 它由一组用Java语编写的类和接口组成。通过这些类和接口,JDBC把SQL语句发送给不同类型的数据库进行处理并接收处理结果。

JDBC两大类
● 对Java开发人员而言是API,对数据库提供商而言是接口。
● 面向开发人员:作为API,JDBC为程序开发提供标准的接口。
● 面向数据库厂商作为接口,让数据库厂商按标准方法来实现数据库连接与操作(数据库驱动程序)。

数据库驱动程序
在这里插入图片描述

1 实现步骤

在这里插入图片描述
首先要导包:
mysql-connector-java-5.1.26-bin.jar

  1. 加载驱动
    Class.forName(“com.mysql.jdbc.Driver”);
  2. 获取连接
    Connection conn = DriverManager.getConnection(url, username, password);
  3. 获得执行对象
    Statement statment=conn.createStatement();
    // 执行对象
    private static Statement statement;
    private static PreparedStatement preparedStatement;
    private static CallableStatement callableStatement;
  4. 执行sql,获得结果集
    ResultSet rs = statement.executeQuery(sql);
    int rs = statement.executeUpdate(sql); // insert,update,delete,注意返回值为int
  5. 处理结果集
    where(rs.next()) {
    String product_id = rs.getString(1);

    }
  6. 释放资源
    在这里插入图片描述
    自己封装JDBC的工具类:
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtil {
	// 固定的配置信息
	// 主协议:子协议://IP:端口 / 数据库名
	private static String url = "jdbc:mysql://localhost:3306/saledb";
	private static String user = "root";
	private static String password = "1234";
	// 固定对象,连接对象
	private static Connection connection;
	// 执行对象
	private static Statement statement;
	private static PreparedStatement preparedStatement;
	private static CallableStatement callableStatement;
	// 结果集
	private static ResultSet resultSet;
	private static int row;
	/**
	 * 1.加载驱动
	 */
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.err.println("驱动错误!!!");
		}
	}
	/**
	 * 2.获得连接
	 */
	public static Connection getConnection() {
		try {
			connection = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			System.err.println("账户密码错误,连接错误,没有权限!!!");
		}
		return connection;
	}
	/**
	 * 3.获得执行对象
	 */
	public static Statement getStatement() {
		// 先执行获得连接对象
		getConnection();
		if(connection != null) {
			try {
				statement = connection.createStatement();
			} catch (SQLException e) {
				System.err.println("获得执行对象错误!!!");
			}
		}
		return statement;
	}
	public static PreparedStatement getPreparedStatement(String sql) {
		if(connection == null) {
			getConnection();
		}
		if(connection != null) {
			try {
				preparedStatement = connection.prepareStatement(sql);
			} catch (SQLException e) {
				System.err.println("sql语句异常!!!");
			}
		}
		return preparedStatement;
	}
	/**
	 * 4.获得结果集
	 */
	// 查询sql
	public static ResultSet find(String sql,Object... arr) { // List<T>泛型,反射(Class<T> cl, String sql)
		if(statement == null) {
			getStatement();
		}
//		if(preparedStatement == null) {
//			getPreparedStatement(sql);
//		}
//		try {
//			if(preparedStatement.isClosed()) {
//				getPreparedStatement(sql);
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		if(preparedStatement != null) {
//			try {
//				if(arr != null) {
//					// 填写参数
//					for(int i = 0; i < arr.length; i++) {
//						preparedStatement.setObject(i+1, arr[i]);
//					}
//				}
//				resultSet = preparedStatement.executeQuery();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				System.out.println("执行错误");
//			}
//		}
		
		if(statement != null) {
			try {
				resultSet = statement.executeQuery(sql);
			} catch (SQLException e) {
				System.err.println("sql语法问题!!!");
			}
		}
		return resultSet;
	}
	//  update
	public static int update(String sql) {
		getStatement();
		if(statement != null) {
			try {
				row = statement.executeUpdate(sql);
			} catch (SQLException e) {
				System.err.println("sql语法问题!!!");
			}
		}
		return row;
	}
	public static int updates(String sql,Object... args) {
		if(preparedStatement == null) {
			getPreparedStatement(sql);
		}
		try {
			if(preparedStatement.isClosed()) {
				getPreparedStatement(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(preparedStatement != null) {
			try {
				row = preparedStatement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return row;
	}
	/**
	 * 5.关闭
	 */
	public static void closeAll() {
		if(resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(statement != null) {
			try {
				statement.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();
			}
		}
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值