本文主要是jdbc基础知识,包括获取连接,常用API,实现步骤以及实现工具类的封装。
文章目录
前言
数据库实现了数据的持久化操作,而Java中要如何访问数据库?,如何进行读写数据操作?
一、JDBC预备知识
1.JDBC本质
其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。
2.常用的API
DriverManager(类):加载管理驱动
Connection(接口):建立连接
Statement(接口):执行命令并返回结果
PreparedStatement(接口):预处理语句
ResultSet(接口):结果集,查询使用,获取到从数据库中读取的数据
3.获取链接的方法
第一步:加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
注:适用于MySQL8,MySQL5.7不加.cj
第二步:获取连接
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/db_shop", "root", "root");
注:
参数一:数据库的地址 jdbc:mysql://localhost:3306/db_shop jdbc协议:mysql://主机:端口/库名。参数二:用户名 参数三:密码。
二、执行对象(用于将sql语句发送到数据库中)
1.静态执行对象创建createStatement
作用:用于执行不带参数的SQL 语句。
特点:每次执行 SQL 语句,数据库都要执行 SQL 语句的编译,仅执行一次查询并返回结果的情形建议使用这个,此时效率高于 PreparedStatement。
2.动态执行对象创建prepareStatement
作用:用于执行带 或 不带参数的预编译 SQL 语句
特点:是预编译的, 在执行可变参数的一条 SQL 语句时,比 Statement 的效率高,安全性好,有效防止 SQL 注入(将转义字符等转为普通字符)等问题,对于多次重复执行的语句,效率更高。
3.Statement常用的方法
executeQuery(String sql) 用于向数据发送查询语句。
executeUpdate(String sql) 用于向数据库发送insert、update或delete语句。
三、实习增删改查操作的步骤
增删改(返回的是boolean类型的值,通用excuteUpdate)
步骤:1.建立连接-->2.编写SQL命令-->3.获取Statement对象:连接对象.createStatement-->4.发送执行命令-->5.关闭连接(释放资源)
查(返回的是ResultSet类型的结果集,通用excuteQuery)
步骤:1.建立连接-->2.编写SQL命令-->3.获取Statement对象:连接对象.createStatement-->4.发送执行命令-->5.使用ResultSet结果集接收数据-->6.关闭连接(释放资源)
四、实现工具类的封装(增删改查)
public class DbHelper {
private static String url;
private static String user;
private static String pwd;
static {
Properties properties=new Properties();
InputStream inputStream=DbHelper.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(inputStream);
url = properties.getProperty("url");
user = properties.getProperty("user");
pwd = properties.getProperty("pwd");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static List<Map<String,Object>> query(String sql,Object... params){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
List<Map<String,Object>> list=new ArrayList<>();
try {
connection=getConn();
statement=connection.prepareStatement(sql);
setParams(statement, params);
resultSet = statement.executeQuery();
ResultSetMetaData metaData=resultSet.getMetaData();
while (resultSet.next()){
Map<String,Object> map=new HashMap<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
map.put(metaData.getColumnLabel(i),resultSet.getObject(i));
}
list.add(map);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}finally {
closeAll(connection, statement, resultSet);
}
return list;
}
public static boolean update(String sql,Object... params){
Connection connection=null;
PreparedStatement statement=null;
boolean result=false;
try {
connection=getConn();
statement=connection.prepareStatement(sql);
setParams(statement,params);
int n = statement.executeUpdate();
if (n>0){
result=true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}finally {
closeAll(connection,statement,null);
}
return result;
}
public static int insertAndSet(String sql,Object... params){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
int id=0;
try {
connection=getConn();
statement= connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
setParams(statement,params);
int n = statement.executeUpdate();
if (n>0){
resultSet=statement.getGeneratedKeys();
if (resultSet.next()){
id = resultSet.getInt(1);
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return id;
}
private static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) {
if (resultSet !=null){
try {
resultSet.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (statement !=null){
try {
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if (connection !=null){
try {
connection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
private static void setParams(PreparedStatement statement, Object[] params) throws SQLException {
if (params !=null){
for (int i = 0; i < params.length; i++) {
statement.setObject(i+1, params[i]);
}
}
}
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(url,user,pwd);
}
}
总结
重点在于工具类的实现,理解大于死记。
更新于2023-4-1