1

我有这个 Java 方法,我将使用它来将 JSF 表单中的数据插入 Oracle:

public int saveData(int result) throws SQLException, java.text.ParseException, NoSuchAlgorithmException {

        String SqlStatement = null;

        if (ds == null) {
            throw new SQLException();
        }

        Connection conn = ds.getConnection();
        if (conn == null) {
            throw new SQLException();
        }

        PreparedStatement ps = null;

        /*

        CREATE TABLE USERS(
            USERID INTEGER NOT NULL,
            GROUPID INTEGER,
            SPECIALNUMBER VARCHAR2(60 ),
            USERNAME VARCHAR2(50 ),
            PASSWD VARCHAR2(50 ),
            DATETOCHANGEPASSWD DATE,
            ADDRESS VARCHAR2(60 ),
            STATEREGION VARCHAR2(50 ),
            COUNTRY VARCHAR2(50 ),
            USERSTATUS VARCHAR2(30 ),
            TELEPHONE VARCHAR2(50 ),
            DATEUSERADDED DATE,
            USEREXPIREDATE DATE,
            DATEUSERLOCKED CHAR(20 ),
            CITY VARCHAR2(50 ),
            EMAIL VARCHAR2(50 ),
            DESCRIPTION CLOB
        )
        /
        */

        try {
            conn.setAutoCommit(false);
            boolean committed = false;
            try {           /* insert into Oracle the default system(Linux) time */
        InsertSqlStatement = "INSERT INTO USERS"
                        + " (USERID, GROUPID, SPECIALNUMBER, USERNAME, PASSWD, DATETOCHANGEPASSWD,"
                        + " ADDRESS, STATEREGION, COUNTRY, USERSTATUS, TELEPHONE, DATEUSERADDED," 
                        + " USEREXPIREDATE, DATEUSERLOCKED, CITY, EMAIL, DESCRIPTION)"
                        + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        UpdateSqlStatement = "UPDATE USERS "
                                + "SET "
                                    + "USERID = ?, "
                                    + "GROUPID = ?, "
                                    + "SPECIALNUMBER = ?, "
                                    + "USERNAME = ?, "
                                    + "PASSWD = ?, "
                                    + "DATETOCHANGEPASSWD = ?, "
                                    + "ADDRESS = ?, "
                                    + "STATEREGION = ?, "
                                    + "COUNTRY = ?, "
                                    + "USERSTATUS = ?, "
                                    + "TELEPHONE = ?, "
                                    + "DATEUSERADDED = ?, "
                                    + "USEREXPIREDATE = ?, "
                                    + "DATEUSERLOCKED = ?, "
                                    + "CITY = ?, "
                                    + "EMAIL = ?, "
                                    + "DESCRIPTION = ? "
                        + "WHERE USERID = " + id;

                ps = conn.prepareStatement(SqlStatement);

                ps.setString(1, settingsMap.get("USERID"));
                ps.setString(2, settingsMap.get("GROUPID"));
                ps.setString(3, settingsMap.get("SPECIALNUMBER"));
                ps.setString(4, settingsMap.get("USERNAME"));
                ps.setString(5, passwdConvert(settingsMap.get("PASSWD")));
                ps.setDate(6, toDate(settingsMap.get("DATETOCHANGEPASSWD")));
                ps.setString(7, settingsMap.get("ADDRESS"));
                ps.setString(8, settingsMap.get("STATEREGION"));
                ps.setString(9, settingsMap.get("COUNTRY"));
                ps.setString(10, settingsMap.get("USERSTATUS"));
                ps.setString(11, settingsMap.get("TELEPHONE"));
                ps.setDate(12, toDate(settingsMap.get("DATEUSERADDED")));
                ps.setDate(13, toDate(settingsMap.get("USEREXPIREDATE")));
                ps.setDate(14, toDate(settingsMap.get("DATEUSERLOCKED")));
                ps.setString(15, settingsMap.get("CITY"));
                ps.setString(16, settingsMap.get("EMAIL"));
                ps.setString(17, settingsMap.get("DESCRIPTION"));

                ps.executeUpdate();

                conn.commit();
                committed = true;
            }
            finally 
            {
                if (!committed) {
                    conn.rollback();
                }
            }
        } finally {
            /* Release the resources */
            ps.close();
            conn.close();
        }
        return result;
    }

现在我无法测试 SQL 查询。你能告诉我它是否有效以及如何优化 SQL 查询的性能?

4

4 回答 4

3

现在我无法测试 SQL 查询。你能告诉我它是否有效...

没有任何把握。(为什么不等到可以测试它??)

...以及如何优化 SQL 查询的性能?

目前尚不完全清楚您要做什么。但是,这里有一些关于性能的建议:

  • 您正在为每个执行的 SQL 语句创建和释放数据库连接。这对性能肯定是不利的。
  • 无需在更新同一记录后进行插入操作……如果您打算这样做的话。
  • 您将通过批量或批量插入或更新而不是一次插入一条记录来获得性能。
  • 如果您将大量数据插入到具有大量索引的空表中,那么如果您先进行插入然后再创建索引,则可能会获得更好的性能。

在单个查询级别(即“UPDATE”),您可能无法使查询显着加快。

于 2012-07-26T12:18:54.023 回答
2

你唯一能做的改进就是把idas '?' 还:

UPDATE USERS "
                            + "SET "
                                + "USERID = ?, "
                                + "GROUPID = ?, "
                                + "SPECIALNUMBER = ?, "
                                + "USERNAME = ?, "
                                + "PASSWD = ?, "
                                + "DATETOCHANGEPASSWD = ?, "
                                + "ADDRESS = ?, "
                                + "STATEREGION = ?, "
                                + "COUNTRY = ?, "
                                + "USERSTATUS = ?, "
                                + "TELEPHONE = ?, "
                                + "DATEUSERADDED = ?, "
                                + "USEREXPIREDATE = ?, "
                                + "DATEUSERLOCKED = ?, "
                                + "CITY = ?, "
                                + "EMAIL = ?, "
                                + "DESCRIPTION = ? "
                    + "WHERE USERID = ?";

当然,还要添加一个 set decleration:

 ps.setInt(18, id);
于 2012-07-26T12:14:54.427 回答
1

如果要插入多行,则可以通过重用数据库连接以及准备好的语句来提高性能。后者也需要按照 ftom2建议的方式将用户 ID 视为一行。除此之外,性能优化的空间很小。

于 2012-07-26T12:18:14.793 回答
1

我认为没有什么可以优化的,因为您只插入一个表。更新也一样。没有连接或分组,所以你真的可以做任何事情。也许只是一个注释-您可以StringBuilder用于代码格式化:-)

于 2012-07-26T12:12:53.393 回答