21xrx.com
2024-11-22 12:44:37 Friday
登录
文章检索 我的文章 写文章
Java数据库介绍与案例分析
2023-06-15 17:31:21 深夜i     --     --
Java 数据库 MySQL Oracle MongoDB

Java语言中,访问数据库是非常常见的操作。为了方便快捷地处理数据操作,Java提供了一系列数据库相关的API,通过这些API,我们可以方便地连接数据库、执行SQL语句、获取结果集等操作。接下来,我们来介绍一些常见的Java数据库和对应的案例。

1. MySQL

MySQL是目前世界上最流行的开源数据库之一,由于其稳定性和易用性,被广泛应用于Web应用程序中。在Java中使用MySQL,我们需要使用Java Connector / J驱动程序,它提供了一系列的API,方便我们连接MySQL数据库、执行SQL语句和获取结果集等操作。下面是一个简单的Java程序,用于连接MySQL数据库并执行查询操作。


import java.sql.*;

public class MySQLDemo {

  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

  static final String DB_URL = "jdbc:mysql://localhost:3306/test";

  static final String USER = "root";

  static final String PASS = "root";

  public static void main(String[] args) {

    Connection conn = null;

    Statement stmt = null;

    try {

      Class.forName(JDBC_DRIVER);

      System.out.println("Connecting to database...");

      conn = DriverManager.getConnection(DB_URL, USER, PASS);

      System.out.println("Creating statement...");

      stmt = conn.createStatement();

      String sql;

      sql = "SELECT id, name, age FROM users";

      ResultSet rs = stmt.executeQuery(sql);

      while (rs.next()) {

        int id = rs.getInt("id");

        String name = rs.getString("name");

        int age = rs.getInt("age");

        System.out.print("ID: " + id);

        System.out.print(", Name: " + name);

        System.out.println(", Age: " + age);

      }

      rs.close();

      stmt.close();

      conn.close();

    } catch (SQLException se) {

      se.printStackTrace();

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      try {

        if (stmt != null)

          stmt.close();

      } catch (SQLException se2)

      

      try {

        if (conn != null)

          conn.close();

      } catch (SQLException se) {

        se.printStackTrace();

      }

    }

    System.out.println("Goodbye!");

  }

}

2. Oracle

Oracle是一种商用的关系型数据库,适用于处理大量的数据和高并发请求。在Java中使用Oracle,我们同样需要下载相应的JDBC驱动程序。这里我们同样提供一个简单的Java程序,用于连接Oracle数据库并执行查询操作。


import java.sql.*;

public class OracleDemo {

  static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";

  static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";

  static final String USER = "username";

  static final String PASS = "password";

  public static void main(String[] args) {

    Connection conn = null;

    Statement stmt = null;

    try {

      Class.forName(JDBC_DRIVER);

      System.out.println("Connecting to database...");

      conn = DriverManager.getConnection(DB_URL, USER, PASS);

      System.out.println("Creating statement...");

      stmt = conn.createStatement();

      String sql;

      sql = "SELECT id, name, age FROM users";

      ResultSet rs = stmt.executeQuery(sql);

      while (rs.next()) {

        int id = rs.getInt("id");

        String name = rs.getString("name");

        int age = rs.getInt("age");

        System.out.print("ID: " + id);

        System.out.print(", Name: " + name);

        System.out.println(", Age: " + age);

      }

      rs.close();

      stmt.close();

      conn.close();

    } catch (SQLException se) {

      se.printStackTrace();

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      try {

        if (stmt != null)

          stmt.close();

      } catch (SQLException se2)

      

      try {

        if (conn != null)

          conn.close();

      } catch (SQLException se) {

        se.printStackTrace();

      }

    }

    System.out.println("Goodbye!");

  }

}

3. MongoDB

MongoDB是一种开源的文档数据库,它使用类似JSON的格式存储数据,非常适合处理半结构化或非结构化的数据。在Java中使用MongoDB,我们同样需要下载相应的JDBC驱动程序。这里我们同样提供一个简单的Java程序,用于连接MongoDB数据库并执行查询操作。


import java.sql.*;

public class MongoDBDemo {

  static final String JDBC_DRIVER = "mongodb.jdbc.MongoDriver";

  static final String DB_URL = "jdbc:mongo://localhost:27017/mydb";

  static final String USER = "username";

  static final String PASS = "password";

  public static void main(String[] args) {

    Connection conn = null;

    Statement stmt = null;

    try {

      Class.forName(JDBC_DRIVER);

      System.out.println("Connecting to database...");

      conn = DriverManager.getConnection(DB_URL, USER, PASS);

      System.out.println("Creating statement...");

      stmt = conn.createStatement();

      String sql;

      sql = "SELECT firstname, lastname, age FROM users";

      ResultSet rs = stmt.executeQuery(sql);

      while (rs.next()) {

        String firstname = rs.getString("firstname");

        String lastname = rs.getString("lastname");

        int age = rs.getInt("age");

        System.out.print("Name: " + firstname + " " + lastname);

        System.out.println(", Age: " + age);

      }

      rs.close();

      stmt.close();

      conn.close();

    } catch (SQLException se) {

      se.printStackTrace();

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      try {

        if (stmt != null)

          stmt.close();

      } catch (SQLException se2)

      

      try {

        if (conn != null)

          conn.close();

      } catch (SQLException se) {

        se.printStackTrace();

      }

    }

    System.out.println("Goodbye!");

  }

}

总结

以上是三种常见的Java数据库及其相应的案例。在实际开发中,我们需要根据具体业务需求选择合适的数据库,以及相应的JDBC驱动程序,才能快速、高效地操作数据库。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复