JDBC驱动

用户下载 JDBC 驱动 并导入 jar 包后,即可以使用 JDBC 提供的 API。

示例

以下示例为通过 maven 工程使用 JDBC 进行简单的增删改查操作。

  1. pom.xml 中添加 MySQL JDBC 驱动的依赖,以 mysql-connector-java-5.1.38 为例

    1. <dependencies>
    2. <dependency>
    3. <groupId>mysql</groupId>
    4. <artifactId>mysql-connector-java</artifactId>
    5. <version>5.1.38</version>
    6. </dependency>
    7. </dependencies>
  2. 假设本地有默认安装的 MySQL 实例,存在 root 用户密码为 123456,连接到该实例并准备样例使用的数据库 db 和表 tb

    1. CREATE DATABASE db;
    2. USE db;
    3. CREATE TABLE tb (id INT, first_name VARCHAR(128), last_name VARCHAR(128));
  3. 在工程的 src/main/java/com/sequoiadb/sample 目录下添加 JdbcSample.java 文件。

    1. package com.sequoiadb.sample;
    2. import java.sql.*;
    3. public class JdbcSample {
    4. static {
    5. try {
    6. Class.forName("com.mysql.jdbc.Driver");
    7. } catch (ClassNotFoundException e) {
    8. e.printStackTrace();
    9. }
    10. }
    11. public static void main(String[] args) throws SQLException {
    12. String hostName = "127.0.0.1";
    13. String port = "3306";
    14. String databaseName = "db";
    15. String myUser = "root";
    16. String myPasswd = "123456";
    17. String url = "jdbc:mysql://" + hostName + ":" + port + "/" + databaseName + "?useSSL=false";
    18. Connection conn = DriverManager.getConnection(url, myUser, myPasswd);
    19. System.out.println("---INSERT---");
    20. String sql = "INSERT INTO tb VALUES(?,?,?)";
    21. PreparedStatement ins = conn.prepareStatement(sql);
    22. ins.setInt(1, 1);
    23. ins.setString(2, "Peter");
    24. ins.setString(3, "Parcker");
    25. ins.executeUpdate();
    26. System.out.println("---UPDATE---");
    27. sql = "UPDATE tb SET first_name=? WHERE id = ?";
    28. PreparedStatement upd = conn.prepareStatement(sql);
    29. upd.setString(1, "Stephen");
    30. upd.setInt(2, 1);
    31. upd.executeUpdate();
    32. System.out.println("---SELECT---");
    33. Statement stmt = conn.createStatement();
    34. sql = "SELECT * FROM tb";
    35. ResultSet rs = stmt.executeQuery(sql);
    36. boolean isHeaderPrint = false;
    37. while (rs.next()) {
    38. ResultSetMetaData md = rs.getMetaData();
    39. int col_num = md.getColumnCount();
    40. if (!isHeaderPrint) {
    41. System.out.print("|");
    42. for (int i = 1; i <= col_num; i++) {
    43. System.out.print(md.getColumnName(i) + "\t|");
    44. isHeaderPrint = true;
    45. }
    46. }
    47. System.out.println();
    48. System.out.print("|");
    49. for (int i = 1; i <= col_num; i++) {
    50. System.out.print(rs.getString(i) + "\t|");
    51. }
    52. System.out.println();
    53. }
    54. stmt.close();
    55. System.out.println("---DELETE---");
    56. sql = "DELETE FROM tb WHERE id = ?";
    57. PreparedStatement del = conn.prepareStatement(sql);
    58. del.setInt(1, 1);
    59. del.executeUpdate();
    60. conn.close();
    61. }
    62. }
  4. 使用 maven 编译及运行

    1. $ mvn compile
    2. $ mvn exec:java -Dexec.mainClass="com.sequoiadb.sample.JdbcSample"

    得到如下运行结果:

    1. ---INSERT---
    2. ---UPDATE---
    3. ---SELECT---
    4. |id |first_name |last_name |
    5. |1 |Stephen |Parcker |
    6. ---DELETE---