jsp课设数据库工具类

#ORM#

这样就可以直接连接数据库并对javabean对应数据库的相对应的属性

直接使用sql语言调用工具类的方法实现查询和更改


import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

//获取到db.properties文件中的数据库信息
public class JdbcUtil {
    //私有变量
    private static String driver;
    private static String url;
    private static String user;
    private static String password;

    //静态块
    static{
        try{
            //1.新建属性集对象
            Properties properties = new Properties();
            //2通过反射,新建字符输入流,读取db.properties文件
            InputStream input = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
            //3.将输入流中读取到的属性,加载到properties属性集对象中
            properties.load(input);
            //4.根据键,获取properties中对应的值
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //返回数据库连接
    public static Connection getConnection(){
        try{
            //注册数据库的驱动
            Class.forName(driver);
            //获取数据库连接(里面内容依次是:主机名和端口、用户名、密码)
            Connection connection = DriverManager.getConnection(url,user,password);
            //返回数据库连接
            return connection;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static <T> List<T> executeQuery(String sql, Class<T> objectType, Object... params) {
        List<T> results = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            statement = connection.prepareStatement(sql);

            // Set query parameters
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }

            resultSet = statement.executeQuery();

            // Process the result set
            while (resultSet.next()) {
                T result = mapResultSetToObject(resultSet, objectType);
                results.add(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResultSet(resultSet);
            closeStatement(statement);
            closeConnection(connection);
        }

        return results;
    }

    private static <T> T mapResultSetToObject(ResultSet resultSet, Class<T> objectType) throws SQLException {
        T object = null;
        try {
            object = objectType.getDeclaredConstructor().newInstance();

            // Map the columns to the object fields using reflection
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnLabel(i);
                Field field = objectType.getDeclaredField(columnName);
                field.setAccessible(true);
                Object value = resultSet.getObject(i);
                field.set(object, value);
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                NoSuchMethodException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return object;
    }

    // Execute an INSERT, UPDATE, or DELETE query
    public static int executeUpdate(String sql, Object... params) {
        Connection connection = null;
        PreparedStatement statement = null;
        int affectedRows = 0;

        try {
            connection = getConnection();
            statement = connection.prepareStatement(sql);

            // Set query parameters
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }

            affectedRows = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeStatement(statement);
            closeConnection(connection);
        }

        return affectedRows;
    }

    // Close the ResultSet
    private static void closeResultSet(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // Close the PreparedStatement
    private static void closeStatement(PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // Close the Connection
    private static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

现在框架会多mybatis等使用广泛,这个只是为了jsp的课设不能使用框架

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值