参考文献

JDBC连接数据库的流程

  • 在开发环境中加载指定数据库的驱动程序

  • 在Java程序中加载程序

    1
    ClassName.forName("com.mysql.cj.jdbc.Driver");
  • 创建数据连接对象

    1
    2
    // URL = 协议名 + IP地址 + 端口 + 数据库名称
    Connection connection = DriverManager.getConnection("连接数据库的URL","用户名","密码");
  • 创建Statement对象

    1
    Statement statement = connection.createStatement();
  • 调用Statement对象的相关方法执行对应的SQL

    1
    ResultSet resulte = statement.excuteUpdate("SQL");
  • 关闭数据库连接

    1
    connection.close();

各种数据库URL配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql:
jdbc:mysql://host:port//db_name?

SQL Server:
jdbc:sqlserver://host:port//db_name?

Oracle:
jdbc:oracle:thin:@host:port:db_name

MongoDB:
# 无密码
mongodb://host:port/db_name
# 有密码
mongodb://username:password@host:port/db_name