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
- 加载驱动
Class.forName(“com.mysql.jdbc.Driver”); - 获取连接
Connection conn = DriverManager.getConnection(url, username, password); - 获得执行对象
Statement statment=conn.createStatement();
// 执行对象
private static Statement statement;
private static PreparedStatement preparedStatement;
private static CallableStatement callableStatement; - 执行sql,获得结果集
ResultSet rs = statement.executeQuery(sql);
int rs = statement.executeUpdate(sql); // insert,update,delete,注意返回值为int - 处理结果集
where(rs.next()) {
String product_id = rs.getString(1);
…
} - 释放资源
自己封装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();
}
}
}
}