JDBC驱动程序
JDBC驱动程序组件为java程序连接不同数据库系统提供服务,它通常由数据库系统方开发提供或由第三方提供。
下载对应不同数据库的JDBC
数据库连接信息配置
URL:连接数据库系统资源描述符
DriverClass:数据库系统驱动类名称
UserName:登录数据库系统用户名称
Password:登录数据库系统用户名密码
Others:其它配置信息
数据库连接信息配置
数据库连接信息通常以普通文本属性文件进行配置dbconf.properties
读取连接配置文件信息
定义继承Properties组件的类实现读取信息
提供数据源管理组件连接信息
源程序中不必修改数据库任何连接属性
建立数据源管理组件
提供Connection连接接口对象
提供StatementSQL语句执行接口对象
关闭数据库连接通用功能
## 数据库属性配置信息 jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl jdbc_driver=oracle.jdbc.driver.OracleDriver jdbc_user=system jdbc_password=system
package com.xzit.db.util; import java.util.ResourceBundle; public final class Env { /* 存储从属性文件读取的数据库属性配置信息 */ public static final String JDBC_URL; public static final String JDBC_DRIVER; public static final String JDBC_USER; public static final String JDBC_PASSWORD; static { /* 获取配置文件的名称,使用getBundle()方法 */ ResourceBundle resourceBundle = ResourceBundle.getBundle("dbconf");//不需要写后缀名 /* 获取资源文件中的信息:使用getString()方法 */ JDBC_URL=resourceBundle.getString("jdbc_url"); JDBC_DRIVER=resourceBundle.getString("jdbc_driver"); JDBC_USER=resourceBundle.getString("jdbc_user"); JDBC_PASSWORD=resourceBundle.getString("jdbc_password"); } public static void main(String[] args) { System.out.println(Env.JDBC_DRIVER); System.out.println(Env.JDBC_URL); System.out.println(Env.JDBC_USER); } } //public final class Env extends Properties { // /* 存储从属性文件读取的数据库属性配置信息 */ // public static final String JDBC_URL; // public static final String JDBC_DRIVER; // public static final String JDBC_USER; // public static final String JDBC_PASSWORD; // /* 数据库连接属性文件路径和名称 */ // private static final String CONF_FILE="com/xzit/conf/dbconf.properties"; // private static Env env; // static { // if(env == null) // env = new Env(); // /* 获取当前程序发布的类路径 */ // String classes = env.getClass().getClassLoader().getSystemResource("").getPath(); // System.out.println(classes); // //获取指向属性文件的输入流 // InputStream input = env.getClass().getClassLoader().getResourceAsStream(CONF_FILE); // try { // env.load(input);//加载文件流 // } catch (IOException e) { // e.printStackTrace(); // }finally { // try { // input.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // /* 利用properties中继承方法getProperty获取属性文件信息 */ // JDBC_URL=env.getProperty("jdbc_url"); // JDBC_DRIVER=env.getProperty("jdbc_driver"); // JDBC_USER=env.getProperty("jdbc_user"); // JDBC_PASSWORD=env.getProperty("jdbc_password"); // } // // private Env(){ // String a = env.JDBC_DRIVER; // } // // public static void main(String[] args) { // System.out.println(Env.JDBC_DRIVER); // System.out.println(Env.JDBC_URL); // // } //}
package com.xzit.db.util; import java.sql.*; /* * 数据源管理组件,提供最基本的通用的数据库连接 * */ public final class DataSourceManager { /* * 提供目标数据源的连接通用方法 * */ public static Connection getConnection(){ Connection conn = null; /* 加载数据库驱动 */ try { /* 加载数据库驱动 */ Class.forName(Env.JDBC_DRIVER); conn = DriverManager.getConnection(Env.JDBC_URL,Env.JDBC_USER,Env.JDBC_PASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } /* * 关闭数据库连接的通用方法 * */ public static void close(Connection conn){//关闭Connection try { if(conn != null && conn.isClosed()){ conn.close(); //关闭数据库连接 } } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void close(Statement state){ try { if (state != null && state.isClosed()){ state.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void close(ResultSet set){ try { if (set != null && !set.isClosed()){ set.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>2021_10_13_jdbcapp</artifactId> <version>1.0-SNAPSHOT</version> <name>2021_10_13_jdbcapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>