zoukankan      html  css  js  c++  java
  • 【转载】使用注解和反射实现通用性…

    JDBC是什么?

    JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据。JDBC代表Java数据库连接。

    JDBC通常的使用步骤有7步:

        加载JDBC驱动程序

       使用数据库的用户名和密码以及 URL 获取链接数据的 connection

        创建一个Statement或者是PreparedStatement(建议使用,放 SQL 注入)

        执行 SQL

        处理结果

        关闭数据库链接释放资源

     

    下面是上述步骤的处理代码

    在介绍 jdbc 连接之前先介绍配置信息的读取;

    注意:要在项目中添加 MySQL 的驱动 jar 包,我用的是5.6.24,驱动包是mysql-connector-java-5.0.7-bin.jar

    src/dbconfig.properties

    driverClass=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=UTF-8

    userName=root

    password=

    本系列文章主要针对 MySQL 数据来讲的,数据名为 test链接数据的用户名和密码分别是root ,由于我使用的是 xampp 集成的数据库所以默认的数据库密码是空的。

    测试过程使用的数据表 user;并手动添加几条测试数据

    DROP TABLE IF EXISTS `user`;

    CREATE TABLE `user` (

      `id` int(11) NOT NULL AUTO_INCREMENT,

      `username` varchar(50) DEFAULT NULL,

      `brith` date DEFAULT NULL,

      `detail_time` datetime DEFAULT NULL,

      PRIMARY KEY (`id`)

    ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;

     

    SET FOREIGN_KEY_CHECKS = 1;

     

    package com.ubuntuvim.utils;


     

    public class DBConnUtils {

                

                private static Connection conn = null;

                private static Properties props = null;

     

                static {

                            props = new Properties();

                            try {

                                        //  加载配置文件

                                        props.load(DBConnUtils.class.getResourceAsStream("/dbconfig.properties"));

                            } catch (IOException e1) {

                                        e1.printStackTrace();

                            }

                            try {

                                        //  读取配置文件的值并加载 MySQL 驱动

                                        Class.forName(props.getProperty("driverClass"));

                            } catch (ClassNotFoundException e) {

                                        e.printStackTrace();

                            }

                }

                

                

                public static Connection getConnection(){

                            try {

                                        // 根据 URL、用户名、密码链接数据库

                                        conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"));

                                        conn.setAutoCommit(false);  // 设置为 false 之后需要conn.commit();才会把SQL 的执行结果提交到数据库

                            } catch (SQLException e) {

                                        e.printStackTrace();

                            }

                            return conn;

                }

     

                

                

                public static void closeConn(){

                            try {

                                        if (conn != null)

                                                    conn.close();

                            } catch (SQLException e) {

                                        e.printStackTrace();

                            }

                            

                }

    }

     

     

    下面请看详细解释;

    1,加载驱动,以 MySQL 为例;

    Class.forName(props.getProperty("driverClass"));

     

    2,使用数据库的用户名和密码以及 URL 获取链接数据的 connection

    // 根据 URL、用户名、密码链接数据库

    conn = DriverManager.getConnection(

        props.getProperty("url"), 

        props.getProperty("username"), 

        props.getProperty("password"));

    得到 connection 之后我们先使用 junit 测试是否链接成功;

    public class DBConnUtilsTest {

     

          private static DBConnUtils dbConnUtil null;

          

          @BeforeClass

          public static void setUpBeforeClass() throws Exception {

                dbConnUtil new DBConnUtils();

          }

     

          @AfterClass

          public static void tearDownAfterClass() throws Exception {

                dbConnUtil null;

          }

     

          @Test

          public void testGetConntion() {

                assertNotNull("对象实例化失败了!!"dbConnUtil);

                assertNotNull("链接数据库失败了!!", DBConnUtils.getConnection());

          }

     

    }

    运行测试得到如下结果

    【转载】使用注解和反射实现通用性的 <wbr>jdbc操作数据库之最简单的 <wbr>jdbc <wbr>操作_1

    结果显示测试通过,connection 对象不为空,说明连接成功了。

    由于字数限制,只能分割为2篇发表,后面的内容接在下面的文章

    使用注解和反射实现通用性的 jdbc操作数据库之最简单的 jdbc 操作_2

     

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    remove white space from read
    optimize the access speed of django website
    dowload image from requests
    run jupyter from command
    crawl wechat page
    python version 2.7 required which was not found in the registry windows 7
    health
    alternate rows shading using conditional formatting
    word
    【JAVA基础】static 关键字
  • 原文地址:https://www.cnblogs.com/ubuntuvim/p/4796525.html
Copyright © 2011-2022 走看看