jdbc使用Java语言连接MySQL数据库方法流程

  1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
  1. 用户信息和URL
String url = "jdbc:mysql://localhost:3306/[数据库名]?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "[用户名]";
String password = "[密码]";
  1. 连接数据库对象
Connection connection = DriverManager.getConnection(url,username,password);
  1. 执行SQL对象
Statement statement = connection.createStatement();//statement对象用于向数据库发送SQL语句
  1. 执行SQL
String sql = "";
ResultSet resultSet;
resultSet = statement.executeQuery(sql);//执行查询操作,返回ResultSet
resultSet = statement.execute(sql);//执行任何sql操作
resultSet = tatement.executeUpdate(sql);//更新、插入、删除操作,返回受影响的行数
resultSet.getObject();//不知道列类型的情况下使用
//-----------遍历-------------
resultSet.beforeFirst();//移动到最前面
resultSet.afterLast();//移动到最后面
resultSet.next();//移动到下一个数据
resultSet.previous();//移动到前一行
resultSet.absolute([行数]);//移动到指定行
  1. 释放连接(必须做,非常消耗资源)
resultSet.close();
statement.close();
connection.close();
    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
    </dependencies>
package com.ola.test;

import java.sql.*;

/**
 * ClassName:TestJdbc Package:com.ola.test
 *
 * @author morningj
 * @date 2021/1/10 14:25
 */
public class TestJdbc {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url = "jdbc:mysql://localhost:3306/computer03?useUnicode=true&characterEncoding=UTF-8";
    String username = "root";
    String password = "niushijian";
    // 注意这里,自从mysql-connector 6 开始使用新特性,驱动名称为com.mysql.cj.jdbc.Driver!!
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement statement = connection.createStatement();
    String sql = "select * from Affiliation where Affiliated = '清华大学'";
    ResultSet resultSet = statement.executeQuery(sql);
    while (resultSet.next()) {
      System.out.println("AuthorID=" + resultSet.getObject("AuthorID"));
      System.out.println("Affiliated=" + resultSet.getObject("Affiliated"));
      System.out.println("----------------------------------------");
    }
    resultSet.close();
    statement.close();
    connection.close();
  }
}

主要参数说明:

user                       数据库用户名(用于连接数据库)                                                                                                                  
password                   用户密码(用于连接数据库)                                                                                                                     
useUnicode                 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true
characterEncoding          当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk
autoReconnect              当数据库连接异常中断时,是否自动重新连接?
autoReconnectForPools      是否使用针对数据库连接池的重连策略 
failOverReadOnly           自动重连成功后,连接是否设置为只读?
maxReconnects              autoReconnect设置为true时,重试连接的次数
initialTimeout             autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒
connectTimeout             和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时
socketTimeout              socket操作(读写)超时,单位:毫秒。 0表示永不超时    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值