zoukankan      html  css  js  c++  java
  • 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();
                }
            }
    
        }
    }
    
  • 相关阅读:
    打开安装 好的Microsoft Dynamics CRM 4.0 报错误为 Caller does not have enough privilege to set CallerOriginToken to the specified value 的解决办法
    基于 Windows Server 2008 的计算机对 Microsoft Dynamics CRM 4.0 的支持
    Microsoft Dynamics CRM 4.0 如何添加自定义按钮
    Microsoft Dynamics CRM 4.0 Plugin 取值,赋值,查询
    C# 中的 enum(枚举) 类型使用例子
    vue事件的绑定
    表单验证2
    node中模块
    node模块的引入
    node中的读文件
  • 原文地址:https://www.cnblogs.com/hedianwei/p/5671357.html
Copyright © 2011-2022 走看看