• JDBC 初识


    JDBC是 "Java Database Connective" 的缩写,是使用Java去连接数据库进行数据操作的过程。

    首先通过Eclipse 创建动态项目,Dynamic Web Project

    这里记得勾选,生成web.xml为日后开发做好准备

    给新建的项目添加开发包(连接驱动开发包),可以到“ www . mvnrepository . com ”下载。

    这里添加Jar包的版本是:mysql-connector-java-5.1.47.jar

    接着创建连接工具类:

     1 public class DBUtil {
     2     // mysql数据库的链接地址 demo为MySQL里面的数据库
     3     private static String URL = "jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8";
     4     // mysql登录用户名
     5     private static String USER = "root";
     6     // mysql登录密码
     7     private static String PASSWORD = "TIGER";
     8     // 取得驱动程序(当程序运行的时候需要加载该类到运行时数据区)
     9     private static String DRIVER = "com.mysql.jdbc.Driver";
    10     // 使用静态代码块去加载驱动程序
    11     static {
    12         // 使用反射加载
    13         try {
    14             Class.forName(DRIVER);
    15         } catch (ClassNotFoundException e) {
    16             e.printStackTrace();
    17         }
    18     }
    19 
    20     /**
    21      * 获取连接的方法
    22      * 
    23      * @return
    24      */
    25     public static Connection getConnection() {
    26         try {
    27             return DriverManager.getConnection(URL, USER, PASSWORD);
    28         } catch (SQLException e) {
    29             e.printStackTrace();
    30         }
    31         return null;
    32     }
    33 
    34     /**
    35      * 实现关闭连接的方法
    36      * 
    37      * @param conn
    38      */
    39     public static void close(Connection conn) {
    40         if (conn != null) {
    41             try {
    42                 conn.close();
    43             } catch (SQLException e) {
    44                 e.printStackTrace();
    45             }
    46         }
    47     }
    48 }

    到此,连接工具类已经完成。

    注意事项:

    • private static String URL =jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8

      • useSSL=true : 表示在程序和数据库之间进行数据交互的时候使用加密的方式传输数据。

      • useUnicode=true&characterEncoding=UTF-8: 表示在数据传输的过程中进行编码的转换(比如项目 的编码是 GBK ,数据库的编码是 UTF-8)

        • 从程序中向数据库插入数据:将数据解码成字节码,之后再编码为 utf-8, 再存入数据库。

        • 从数据库中取得数据到程序中:将 utf-8 的数据解码成字节码,之后再编码成 gbk, 再读取到数据。

    • private static String URL = "jdbc:mysql://localhost:3306";

      • 如果是在 windows 系统上运行程序一般不会出现问题,但是如果将 windows 上的 项目移植到 Linux 上就可能出问题。

      • 所以为了避免跨平台产生乱码的风险,应该把地址信息补全。
  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/lwhsummer/p/10724368.html
Copyright © 2020-2023  润新知