JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数

JDBC从入门到放弃

JDBC(Java Database Connectivity)

在Java中,与数据库进行交互的技术可以分成这么几类:

①JDBC直接访问数据库

②JDO技术,也是一个用于存取数据库中的对象的标准化API

③第三方框架工具,如Hibernate,Mybatis等,但是Hibernate,Mybatis这些框架的底层也是用的JDBC,只是对JDBC进行了二次封装!

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第1张图片

JDBC API是一系列的接口,它能使得应用程序能够进行数据库连接,执行SQL语句,并且获取到数据库返回的结果!

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第2张图片

JDBC连接创建

1:新建JavaWeb maven项目

2:增加依赖

         

          <dependency>

               <groupId>mysqlgroupId>

               <artifactId>mysql-connector-javaartifactId>

               <version>5.1.47version>

          dependency>

3:准备数据

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第3张图片

4:编写数据库链接程序

package com.liujia.jdbc;

 

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

 

import com.mysql.jdbc.Driver;

 

/**

 * 数据库工具类

 *

 * @author liujia

 *

 */

public class DBUtils {

 

     private DBUtils() {

     };

 

     private static DBUtils instance;

 

     public static DBUtils getInstance() {

          if (instance == null) {

               instance = new DBUtils();

          }

          return instance;

     }

 

     public Connection getConnection() {

          Connection connection = null;

          try {

               Driver driver = new com.mysql.jdbc.Driver();

               String url = "jdbc:mysql://127.0.0.1:3306/study";

               Properties info = new Properties();

               info.setProperty("user", "root");

               info.setProperty("password", "123456");

               connection = driver.connect(url, info);

          } catch (SQLException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          }

          return connection;

     }

}

5:测试

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第4张图片

优化之配置文件方式

这样创建数据库连接的方式采用硬编码的方法,将数据库驱动,url,用户名和密码等都写死在程序代码中,一旦项目发布,修改数据库以及用户名密码都需要在代码中进行编码,灵活性很差。

因此,现在需要将其进行优化,将其放置于配置文件中。

增加配置文件

db.driverClass=com.mysql.jdbc.Driver

db.url=jdbc:mysql://127.0.0.1:3306/study

db.user=root

db.password=123456

修改代码

package com.liujia.db.jdbc;

 

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.Driver;

import java.sql.SQLException;

import java.util.Properties;

 

/**

 *

 * @author liujia

 *

 */

public class DBUtils {

 

     private DBUtils() {

     };

 

     private static DBUtils instance;

 

     public static DBUtils getInstance() {

          if (instance == null) {

               instance = new DBUtils();

          }

          return instance;

     }

 

     /**

      * 获取数据库连接

      *

      * @return

      */

     public Connection getConnection() {

          Connection connection = null;

          InputStream in = null;

          try {

               Driver driver;

               in = getClass().getClassLoader().getResourceAsStream("db.properties");

               Properties properties = new Properties();

               properties.load(in);

 

               String driverClass = properties.getProperty("db.driverClass");

               String url = properties.getProperty("db.url");

               String user = properties.getProperty("db.user");

               String password = properties.getProperty("db.password");

 

               driver = (Driver) Class.forName(driverClass).newInstance();

               Properties info = new Properties();

               info.setProperty("user", user);

               info.setProperty("password", password);

               connection = driver.connect(url, info);

          } catch (IOException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } catch (InstantiationException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } catch (IllegalAccessException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } catch (ClassNotFoundException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } catch (SQLException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } finally {

               if (in != null) {

                    try {

                         in.close();

                    } catch (IOException e) {

                         // TODO Auto-generated catch block

                         e.printStackTrace();

                    }

               }

          }

          return connection;

     }

 

     /**

      * 关闭数据库连接

      *

      * @param con

      */

     public static void closeCon(Connection con) {

          if (con != null) {

               try {

                    con.close();

               } catch (SQLException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

               }

          }

     }

}

 

编写测试程序

<%@page import="com.liujia.db.jdbc.DBUtils"%>

<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>你好title>

head>

<body>

<h1>测试服务器是否可用h1>

<% Connection con = DBUtils.getInstance().getConnection();%>

<%=con %>

body>

html>

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第5张图片

进一步优化之使用Drivermanager

我们在项目中一般不直接使用Driver接口而是使用Drivermaager来创建数据库连接。

package com.liujia.db.jdbc;

 

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

 

/**

 *

 * @author liujia

 *

 */

public class DBUtils {

 

     private DBUtils() {

     };

 

     private static DBUtils instance;

 

     public static DBUtils getInstance() {

          if (instance == null) {

               instance = new DBUtils();

          }

          return instance;

     }

 

     /**

      *

      * 创建数据库连接

      * @return

      */

     public Connection getConnection() {

          Connection connection = null;

          InputStream in = null;

          try {

               in = getClass().getClassLoader().getResourceAsStream("db.properties");

              

               Properties properties = new Properties();

               properties.load(in);

               String driverClass = properties.getProperty("db.driverClass");

               String url = properties.getProperty("db.url");

               String user = properties.getProperty("db.user");

               String password = properties.getProperty("db.password");

              

               //加载数据库驱动

               Class.forName(driverClass);

               //使用DriverManager创建数据库连接

               connection =DriverManager.getConnection(url, user, password);

          } catch (IOException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          }  catch (ClassNotFoundException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } catch (SQLException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

          } finally {

               if (in != null) {

                    try {

                         in.close();

                    } catch (IOException e) {

                         // TODO Auto-generated catch block

                         e.printStackTrace();

                    }

               }

          }

          return connection;

     }

 

     /**

      * 关闭数据库连接

      *

      * @param con

      */

     public static void closeCon(Connection con) {

          if (con != null) {

               try {

                    con.close();

               } catch (SQLException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

               }

          }

     }

}

 

JDBC从入门到放弃-01:JDBC连接创建以及优化从配置文件获取jdbc连接参数_第6张图片
源码下载地址如下

https://download.csdn.net/download/caoshangfeidie000/10677277

 

你可能感兴趣的:(JDBC)