jdbc使用模板

JDBC模板

使用方法:

1.在项目的resources文件里添加data.properties文件,内容为

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf-8
user=用户名
password=密码

2.项目采取单例模式,getStatement方法获取查询sql操作对象
3.getPreparedStatement方法获取增删改操作对象,预操作防止sql注入
4.使用完之后建议调用closeConnection方法关闭链接。

//
// Created by bianys on 2022/12/20.
// author: bianys
//

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {
    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;
    private static PreparedStatement ps = null;
    private static Properties p = new Properties();
    private static FileInputStream fis= null;
    //单例模式
    static {
        try {
            Path path = Paths.get("src/main/resources/data.properties");
            fis = new FileInputStream(path.toAbsolutePath().toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            p.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String driver=p.getProperty("driver");
        String user=p.getProperty("user");
        String url=p.getProperty("url");
        String password=p.getProperty("password");
        //1.注册驱动
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        try {
            Class.forName(driver);
            //2.获取连接
            conn= DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
    public static Statement getStatement(){
        try
        {
            //3.创建数据库操作对象
            return conn.createStatement();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public static PreparedStatement getPreparedStatement(String sql){

        try
        {
            //3.创建数据库操作对象
            return conn.prepareStatement(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void closeConnection(){

        if (stmt!=null)
        {
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        if (ps!=null)
        {
            try
            {
                ps.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        if (conn!=null)
        {
            try
            {
                conn.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }

        }
        if (rs!=null)
        {
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用JDBC模板进行模糊查询,您可以使用SQL语句的LIKE操作符和JDBC模板提供的方法来实现。以下是一个示例: ```java import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class EmployeeDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List<Employee> searchEmployees(String keyword) { String sql = "SELECT * FROM employees WHERE name LIKE ?"; // 使用?占位符来避免SQL注入攻击 String searchString = "%" + keyword + "%"; // 在关键字前后添加% // 使用RowMapper将查询结果映射为Employee对象 RowMapper<Employee> rowMapper = new RowMapper<Employee>() { @Override public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { Employee employee = new Employee(); employee.setId(rs.getInt("id")); employee.setName(rs.getString("name")); // 其他属性的映射... return employee; } }; // 执行查询并返回结果 return jdbcTemplate.query(sql, new Object[]{searchString}, rowMapper); } } ``` 在上面的示例中,`searchEmployees`方法接收一个关键字作为参数,并在SQL语句中使用LIKE操作符进行模糊查询。通过在关键字前后添加通配符`%`,可以实现模糊匹配。然后,使用`jdbcTemplate.query`方法执行查询,并使用`RowMapper`将结果集映射为Employee对象。 请注意,上述示例中的`Employee`类是一个自定义的Java类,您需要根据自己的实际情况进行调整和完善。另外,还需要在Spring配置文件中配置JDBC模板和数据源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狴犴ys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值