Java JDBC

Java JDBC


一、简介

  • JDBC:Java Database Connectivity:Java数据库连接

  • 是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成,JDBC提供了一种基类,可以构建高级的工具和接口。

    JDBC包含两个包:
  • 1、java.sql:基本功能。这个包中的类和接口主要针对基本的数据库编程服务,如何生存连接、执行语句以及准备语句和运行批处理查询等,同事也有一些高级的处理,比如批处理更新、事务隔离和可回滚结果集等。

  • 2、javax.sql:扩展功能。它主要为数据方面的高级操作提供了接口和类,如为连接管理、分布式事务和旧有的连接提供了更好的抽象,它引入了容器管理的连接池、分布式事务和行集等。


二、实现代码:


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionDataBase {

    /**
     * 数据库驱动类名称
     */
    // sqlServer
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    // Oracle
    //private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";

    /**
     * 连接字符串
     */
    // sqlServer
    private static final String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=sqlserverDB";
    // Oracle
    //private static final String URLSTR = "jdbc:oracle:thin:@localhost:1521:orcl";

    /**
     * 用户名
     */
    private static final String USERNAME = "sa";

    /**
     * 密码
     */
    private static final String USERPASSWORD = "tcaccp";

    /**
     * 创建数据库连接对象
     */
    private Connection connnection = null;

    /**
     * 创建PreparedStatement对象
     */
    private PreparedStatement preparedStatement = null;

    /**
     * 创建CallableStatement对象
     */
    private CallableStatement callableStatement = null;

    /**
     * 创建结果集对象
     */
    private ResultSet resultSet = null;

    /**
     * 
     * @description 建立连接
     *
     * @return 数据库连接
     */
    public Connection getConnection(){
        try {
            //加载驱动
            Class.forName(DRIVER);
            //进行连接
            connnection = DriverManager.getConnection(URLSTR, USERNAME, USERPASSWORD);
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动失败!");
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("连接获取失败!");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return connnection;
    }
    /**
     * @description 关闭资源
     */
    public void closeAll() {
        // 关闭Connection 对象
        if (connnection != null) {
            try {
                connnection.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            } 
        }
        // 关闭PreparedStatement对象
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        // 关闭CallableStatement 对象
        if (callableStatement != null) {
            try {
                callableStatement.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        // 关闭结果集
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值