JDBC——Java操纵关系型数据库的一套API

图片去外星球了
SUN公司定义接口,各个数据库厂商实现接口,使用JDBC编程,真正执行代码为jar包中的实现类。

JDBC操纵MySQL

  1. 连接数据库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    // 方法用于建立数据库连接
    public class MySQLConnection {
    // 方法用于建立数据库连接
    public Connection getConnection() {
    String url = "jdbc:mysql://localhost:3306/pingpang_plus";
    String user = "root";
    String password = "123456";

    try {
    Connection connection = DriverManager.getConnection(url, user, password);
    System.out.println("成功连接到数据库!");
    return connection;
    } catch (SQLException e) {
    System.err.println("连接数据库时发生错误: " + e.getMessage());
    return null;
    }
    }
    }

    成功连接则返回succeed,否则为failed。

    • 常见报错:
      • No suitable driver found for jdbc:mysql://localhost:3306/mydb
        问题描述:这个错误表明JDBC驱动未正确加载。
        解决方案:查看相应的驱动是否成功配置。
      • Access denied for user ‘root‘@’localhost’ (using password: YES)
        问题描述:这个错误表明数据库连接时用户名或密码不正确。
        解决方案:看账密有没有错
  2. 查询操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    public class MySQLSearch {

    public static Scanner scanner = new Scanner(System.in);
    public void executeSearch() {
    MySQLConnection mySQLConnection = new MySQLConnection();
    Connection connection = mySQLConnection.getConnection();

    if (connection != null) {
    try {
    System.out.println("请输入要执行的查询语句:");
    String userQuery = scanner.nextLine(); // 接受用户输入的查询语句

    //System.out.println("实例化 Statement 对象...");
    Statement stmt = connection.createStatement();

    if (userQuery.trim().toLowerCase().startsWith("select")) {
    ResultSet rs = stmt.executeQuery(userQuery);
    ResultSetMetaData metaData = rs.getMetaData(); // 获取元数据
    int columnCount = metaData.getColumnCount(); // 获取列数

    // 打印表头
    for (int i = 1; i <= columnCount; i++) {
    System.out.print(metaData.getColumnName(i) + "\t");
    }
    System.out.println();

    // 打印查询结果
    while (rs.next()) {
    for (int i = 1; i <= columnCount; i++) {
    System.out.print(rs.getString(i) + "\t"); // 动态获取每列的数据
    }
    System.out.println();
    }

    rs.close();
    } else {
    System.out.println("你输入的语句并不是查询语句");
    }

    // 关闭连接
    stmt.close();
    connection.close();
    } catch (SQLException e) {
    System.err.println("执行查询时发生错误: " + e.getMessage());
    }
    }
    }
    }

    输入你想输入的查询语句,即可完成查询操作。

    • 详细解释:
      • public static Scanner scanner = new Scanner(System.in);
        public:表明类、方法、变量可以在任何类中被访问,而且不受包的限制
        eg.
        public int number; // 公开变量
        MyClass obj = new MyClass();
        obj.number = 10; // 其他类可以直接访问这个public变量
        static:使其后面修饰的无需创建实体就可访问
        eg.
        如果去掉 static,将变成一个实例变量:
        public Scanner scanner = new Scanner(System.in);
        必须通过 创建 MySQLSearch 类的对象 才能访问 scanner
        MySQLSearch mySQLSearch = new MySQLSearch(); // 创建类的实例
        mySQLSearch.scanner.nextLine(); // 通过对象来访问scanner
  3. 插入操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class MySQLAdd {

    public static Scanner scanner = new Scanner(System.in);

    public void MYSQLAdd(){
    MySQLConnection mySQLConnection = new MySQLConnection();
    Connection connection = mySQLConnection.getConnection();

    if(connection != null){
    try {
    System.out.println("在下方输入插入语句:");
    Statement statement = connection.createStatement();
    String sql = scanner.nextLine();
    int ok = statement.executeUpdate(sql);
    if(ok != 0){
    System.out.println("插入成功");
    }
    //关闭连接
    statement.close();
    connection.close();
    }
    catch (SQLException e){
    System.out.println("插入失败");
    }
    }
    }
    }
  4. 删除操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class MySQLDelete {

    public static Scanner scanner = new Scanner(System.in);

    public void MySQLDelete(){
    MySQLConnection mySQLConnection = new MySQLConnection();
    Connection connection = mySQLConnection.getConnection();

    if(connection != null){
    try{
    Statement statement = connection.createStatement();
    System.out.println("输入删除语句");
    String sql = scanner.nextLine();
    int ok = statement.executeUpdate(sql);
    if(ok != 0){
    System.out.println("删除成功");
    }

    //close DB
    statement.close();
    connection.close();
    }
    catch (SQLException e){
    System.out.println("删除失败");
    }
    }
    }
    }
  5. 更新操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class MySQLUpdate {

    public static Scanner scanner = new Scanner(System.in);

    public void MySQLUpdate(){
    MySQLConnection mySQLConnection = new MySQLConnection();
    Connection connection = mySQLConnection.getConnection();

    if(connection != null){
    try{
    Statement statement = connection.createStatement();
    System.out.println("输入更新语句");
    String sql = scanner.nextLine();
    int ok = statement.executeUpdate(sql);
    if(ok != 0){
    System.out.println("更新成功");
    }

    //close DB
    statement.close();
    connection.close();
    }
    catch (SQLException e){
    System.out.println("更新失败");
    }
    }
    }
    }

JDBC却也存在许多弊端

  1. 硬编码——url、user、password难以改动

    • 硬编码:写在程序中固定的值,区别于通过变量、配置文件变化而变化的值。
  2. 繁琐——获取字段太多

    • 当不使用获取元数据的方法执行查询操作时,得一个字段一个字段的获取
      图片去外星球了
  3. 资源浪费——频繁开关连接

    • close方法

MyBatis作为一款持久层框架,较好的解决了上述问题。