Java数据库连接池工具类ConnectionFactory详解

这篇文档介绍了一个简单的Java数据库连接池工具类 ConnectionFactory,它提供以下功能:

  • 获取数据库连接
  • 关闭连接
  • 处理SQL语句执行异常

代码示例

package utils;

import dao.StudentDao;
import vo.Student;

import java.sql.*;
import java.util.ArrayList;

public class ConnectionFactory {
    /**
     * 此类为数据库链接类,产生一个数据库链接
     */
    private ConnectionFactory() {
    }

    public static Connection getConnection() {
        try {
            String driverName = 'com.mysql.jdbc.Driver';
            //          jdbc 8.0
            //       String driverName='com.mysql.cj.jdbc.Driver';

            String userName = 'root';
            String usePwd = 'root';
            String dbName = 'shiyanlou';
            String url = 'jdbc:mysql://localhost:3306/' + dbName + '?user=' + userName + '&password=' + usePwd + '&characterEncoding=utf8';
            //       1. 加载数据库驱动程序
            Class.forName(driverName).newInstance();
            //       2.   创建数据库连接
            Connection conn = DriverManager.getConnection(url);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println('加载数据库驱动异常');
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println('执行SQL语句异常');
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println('其它异常');
        }
        return null;
    }

    public static void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
            if (psmt != null)
                psmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(Connection conn, PreparedStatement psmt) {
        try {
            if (psmt != null)
                psmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//     Connection conn=ConnectionFactory.getConnection();
//     PreparedStatement psmt = null;
//       psmt=conn.prepareStatement('select * from student where stuNo=?');
//        psmt.setInt(1,107);
//       ResultSet rs = psmt.executeQuery();
//       while(rs.next()){
//              System.out.print(rs.getInt(1)+'  ');
//          System.out.print(rs.getString(2)+'  ');
//          System.out.print(rs.getInt(3)+'  ');
//                     }
//
//       System.out.print('数据库操作成功');
//       rs.close();
//       psmt.close();
//       conn.close();
        ArrayList<Student> s = null;
        s = StudentDao.findAll();
//    System.out.println(s);
        for (Student x : s) {
            System.out.println(x.getStuName() + x.getStuNo());
        }
    }

}

执行SQL语句异常的原因

执行SQL语句异常 的出现通常由以下几种情况导致:

  1. 语法错误: SQL 语句本身存在语法错误,请在数据库管理工具中执行相同的语句以验证其正确性。
  2. 表或列不存在: 请确保 SQL 语句中引用的表和列在数据库中确实存在。
  3. 数据类型不匹配: SQL 语句中使用的数据类型与数据库中列定义不一致。例如,数据库中某列为整数类型,但 PreparedStatement 中设置的值为字符串类型。
  4. 数据库连接问题: 数据库连接无效,或者数据库服务器未启动。可以通过尝试连接数据库来验证连接是否正常。
  5. 权限不足: 当前数据库用户缺少执行 SQL 语句所需的权限。

解决方案

  1. 检查 SQL 语句,确保其语法正确无误。
  2. 确认数据库中存在 SQL 语句引用的表和列。
  3. 检查数据类型,确保其与数据库中列定义一致。
  4. 确认数据库连接有效,数据库服务器正常运行。
  5. 为当前数据库用户授予执行 SQL 语句所需的权限。

此外,还可以通过在代码中添加异常捕获和打印语句来获取更详细的错误信息,帮助定位问题所在。

Java数据库连接池工具类ConnectionFactory详解

原文地址: https://www.cveoy.top/t/topic/N3w 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录