sql查询放在哪里?代码风格。爪哇。数据库连接

发布于 2024-12-06 17:47:11 字数 1175 浏览 0 评论 0原文

我正在使用 JDBC 驱动程序,我的问题是将 SQL 查询存储在一个好地方。关键是它将进行大量查询。

Statement s = conn.createStatement ();
s.executeUpdate ("DROP TABLE IF EXISTS animal");
s.executeUpdate (
           "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))");`

更改为这个......

Statement s = conn.createStatement ();
s.executeUpdate (StaticVariables.getDropTableAnimalQuery());
s.executeUpdate (StaticVariables.getCreateTableAnimalQuery());`

并创建带有静态变量的特殊类

public class StaticVariables {
    private static String dropTableAnimalQuery = "DROP TABLE IF EXISTS animal";

    private static String createTableAnimalQuery = "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))";

    public static String getDropTableAnimalQuery() {
        return dropTableAnimalQuery;
    }

    public static String getCreateTableAnimalQuery() {
        return createTableAnimalQuery;
    }
}

也许有人有更好的方法来解决这个问题

I am working with JDBC driver and my problem is storing SQL queries in a good place. The point is that it will be making a large number of queries.

Statement s = conn.createStatement ();
s.executeUpdate ("DROP TABLE IF EXISTS animal");
s.executeUpdate (
           "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))");`

Change to this...

Statement s = conn.createStatement ();
s.executeUpdate (StaticVariables.getDropTableAnimalQuery());
s.executeUpdate (StaticVariables.getCreateTableAnimalQuery());`

... and create special class with static variables

public class StaticVariables {
    private static String dropTableAnimalQuery = "DROP TABLE IF EXISTS animal";

    private static String createTableAnimalQuery = "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))";

    public static String getDropTableAnimalQuery() {
        return dropTableAnimalQuery;
    }

    public static String getCreateTableAnimalQuery() {
        return createTableAnimalQuery;
    }
}

Maybe someone has a better way to solve this

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

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

发布评论

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

评论(3

梦里人 2024-12-13 17:47:11

讨厌将静态常量放入接口中的习惯用法,例如您的StaticVariable示例。

我更喜欢在最大程度使用它们的类中保留常量。如果实现接口的许多子类需要常量,我将向接口添加常量,但也会实现一些方法。

I hate that idiom of putting static constants in an interface like your StaticVariable example.

I prefer to keep constants in the class that uses them to the greatest degree possible. I'll add constants to an interface if many sub-classes that implement it need them, but there will be methods that are implemented as well.

回眸一笑 2024-12-13 17:47:11

我建议您设置 sql 常量。是否将它们放入将使用它们的代码中,或者放入专门为保存常量而创建的另一个类中,这取决于您。

public static final String CREATE_TABLE = "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS animal";

I would recommend making your sql constants. Whether you put them in the code that will use them, or into another class created specifically to hold your constants is up to you.

public static final String CREATE_TABLE = "CREATE TABLE animal ("
           + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
           + "PRIMARY KEY (id),"
           + "name CHAR(40), category CHAR(40))";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS animal";
一瞬间的火花 2024-12-13 17:47:11

这是更好的地方,可以在您需要的地方查询。没有理由将它们替换为不同的类。类应该有字段和方法、状态和行为,否则你就失去了 OOP 的主要思想。因此,让类完全由一堆静态字符串组成是一个坏主意。

顺便看看 Spring Jdbc API。它确实简化了您使用 Jdbc 的日常工作。如果您不想使用它,它不需要 spring 上下文。但使用该 api 来使用 jdbc 会更容易。这是一个可能对您也有帮助的小示例:

    public int countYellowBoxes(final String color) {
        final String query = "select count(*) from boxes where color = ?";
        return jdbcTemplate.queryForInt(query, color);
    }

It is better place that queries where you need them. There is no reason to replace them into distinct class. Classes should have fields and methods, state and behavior, otherwise you kill the main idea of OOP. So to have the classes that are entirely a bunch of static strings is a bad idea.

By the way have a look at Spring Jdbc api. It really simplifies your daily work with Jdbc. It doesn't require a spring context if you don't whant to use it. But it is easier to work with jdbc using that api. Here is a small sample that may helps you too:

    public int countYellowBoxes(final String color) {
        final String query = "select count(*) from boxes where color = ?";
        return jdbcTemplate.queryForInt(query, color);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文