连接到 MySQL 并在 Java 中运行数据库查询

发布于 2024-08-19 09:49:41 字数 1433 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漆黑的白昼 2024-08-26 09:49:41

首先获取您的 JDBC 驱动程序,例如 mysql 的 Connector/J - 将 jar 添加到您的项目中。然后,您可以访问数据库,例如:

public class mysqlTest {

public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String protocol = "jdbc:mysql://localhost/";
    String database = "database";
    String username = "mysqlUsername";
    String password = "mysqlPassword";

    Properties props = new Properties();
    props.put("user", username);
    props.put("password", password);

    Connection conn;
    Statement s;
    ResultSet rs;

    String createTableStatment = "CREATE TABLE test ( `id` INTEGER NOT NULL)";
    String insertStatment = "INSERT INTO test VALUES(1)";
    String selectQuery = "SELECT * FROM test";

    try {
        Class.forName(driver).newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        conn = DriverManager.getConnection(protocol + database,props);
        conn.setAutoCommit(true);
        s = conn.createStatement();
        s.execute(createTableStatment);
        s.execute(insertStatment);
        rs = s.executeQuery(selectQuery);
        while (rs.next()){
            System.out.println("id = "+rs.getString("id"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

First get your JDBC driver, such as mysql's Connector/J - add the jar to your project. Then, you can access a database like:

public class mysqlTest {

public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String protocol = "jdbc:mysql://localhost/";
    String database = "database";
    String username = "mysqlUsername";
    String password = "mysqlPassword";

    Properties props = new Properties();
    props.put("user", username);
    props.put("password", password);

    Connection conn;
    Statement s;
    ResultSet rs;

    String createTableStatment = "CREATE TABLE test ( `id` INTEGER NOT NULL)";
    String insertStatment = "INSERT INTO test VALUES(1)";
    String selectQuery = "SELECT * FROM test";

    try {
        Class.forName(driver).newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        conn = DriverManager.getConnection(protocol + database,props);
        conn.setAutoCommit(true);
        s = conn.createStatement();
        s.execute(createTableStatment);
        s.execute(insertStatment);
        rs = s.executeQuery(selectQuery);
        while (rs.next()){
            System.out.println("id = "+rs.getString("id"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

最美不过初阳 2024-08-26 09:49:41

...如果您确实要使用 java 来查询 MySql 服务器,您应该进一步查看并阅读一些有关 的内容JDBCORM事务以及这些概念将如何帮助您使用数据库。

您可以使用 JDBC 来查询 Mysql 数据库,以及几乎任何其他实现了 JDBC 驱动程序的 DBMS。

ORM 系统为程序员提供了对象和数据库之间的一个层,并为您完成所有艰苦的工作(理论上)。

即使有了上一点,您仍然需要了解一些有关事务的基本知识,将您的操作封装在一个原子单元中,该原子单元仅完全执行或根本不执行。

...if you really are going to use java to query a MySql server you should look further and read some about JDBC, ORM, transactions and how this concepts will help you using a database.

You can use JDBC to query an Mysql database, and virtually any other DBMS who have an JDBC driver implemented.

An ORM system provides the programmer with a layer between Objects and Database and makes all the hard work for you (in theory).

Even with the previous point you really need to have some basic knowledge about transactions, that encapsulates your actions in one atomic unit that only executes completely or does not execute at all.

茶底世界 2024-08-26 09:49:41

您可以尝试使用 MySQL JDBC Utilities API 在 Java 中实现 MySQL 连接。

You can try MySQL JDBC Utilities API for MySQL connectivity in java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文