1

みんな。

私は Java には比較的慣れていませんが (完全な初心者)、VB.Net の経験はあります。

SQL2008 テーブルの行を更新しようとしています。

テーブル名は _Registry です。

以下のコードをコンパイルすると、いくつかのエラーで失敗します (コードの後に​​個別にリストされています)。" // And here is where I run into issues " というセクションを catch ブロックにコメントアウトすると、正常に実行されます (テーブルが更新されていない場合を除く)。

どんな援助でも大歓迎です!

public static void main(String[] args)
{
    String host = "jdbc:sqlserver://SERVER1\\Primrose;databaseName=Primrose";
    String uName = "sa";
    String uPwd = "P@ssw0rd";

// The two SQL statements I am using:

    String queryDB = "SELECT * FROM _Registry WHERE Section = 'RecIds' AND Key_ = '_Folders' AND User_ = 'sc_general'";
    String updateDB = "UPDATE _Registry SET Value WHERE RecID = 5";

// These are the 6 columns in the table named _Registry:
    int getRecId;           // RecId  [int]  IDENTITY(1,4)  
    String getUser;         // User_  [char](64)
    String getSection;      // Section [char](64)
    String getKey;          // Key_  [char](64)
    String getValue;        // Value  [char](255)
    String getExtraInfo;    // ExtraInfo  [text]

    int totalFolders = 10;  // This is a static test value that will be used to ensure I can update the table:

// Everything works from here until I exit the WHILE loop:
    try
    {
        Connection con = DriverManager.getConnection(host, uName, uPwd);
        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(queryDB);

        // Keep reading through the table until I get my desired result:
        while (rs.next())
        {
            getRecId = rs.getInt("RecId");
            getUser = rs.getString("User_");
            getSection = rs.getString("Section");
            getKey = rs.getString("Key_");
            getValue = rs.getString("Value");
            getExtraInfo = rs.getString("ExtraInfo");

            getValue = getValue.trim();    // Strip trailing spaces from string

            int newValue = Integer.parseInt(getValue) + 1;    // Convert string to number so I can add it to total folders in template
            newValue = newValue + totalFolders;    // Change to total + existing value to write back to dB
        getValue = Integer.toString(newValue);
        }

// And here is where I run into issues - I even tried
// this "rs = stmt.executeUpdate(updateDB)" but that had no effect:

        rs = stmt.executeQuery(updateDB);

        rs.updateInt( "RecId", getRecId );
        rs.updateString( "User_", getUser );
        rs.updateString( "Section", getSection );
        rs.updateString( "Key_", getKey );
        rs.updateString( "Value", getValue );
        rs.updateString( "ExtraInfo", getExtraInfo );
        rs.updateRow();

        System.out.println("Updated successfully!");
    }

    catch ( SQLException err )  
    {  
        System.out.println( err.getMessage( ) );
    }  
}  

そして、ここにコンパイラ出力があります:

Compiling 1 source file to C:\Java\DBupdate\build\classes

C:\Java\DBupdate\src\dbupdate\DBupdate.java:73: error: variable getRecId might not have been initialized
            rs.updateInt( "RecId", getRecId );
                                   ^

C:\Java\DBupdate\src\dbupdate\DBupdate.java:74: error: variable getUser might not have been initialized
            rs.updateString( "User_", getUser );
                                      ^

C:\Java\DBupdate\src\dbupdate\DBupdate.java:75: error: variable getSection might not have been initialized
            rs.updateString( "Section", getSection );
                                        ^

C:\Java\DBupdate\src\dbupdate\DBupdate.java:76: error: variable getKey might not have been initialized
            rs.updateString( "Key_", getKey );
                                     ^

C:\Java\DBupdate\src\dbupdate\DBupdate.java:77: error: variable getValue might not have been initialized
            rs.updateString( "Value", getValue );
                                      ^

C:\Java\DBupdate\src\dbupdate\DBupdate.java:78: error: variable getExtraInfo might not have been initialized
            rs.updateString( "ExtraInfo", getExtraInfo );
                                          ^
6 errors

C:\Java\DBupdate\nbproject\build-impl.xml:926: The following error occurred while executing this line:

C:\Java\DBupdate\nbproject\build-impl.xml:268: Compile failed; see the compiler error output for details.

BUILD FAILED (total time: 0 seconds)
4

2 に答える 2

3

エラーが示すように、初期化されていない可能性のある変数を使用しようとしています。それらをnullに初期化するか、それらから読み取る前に何らかの有用な値に設定してください。

SET Valueまた、あなたの SQL 更新クエリは無効ですSET Value=

于 2013-05-02T16:43:34.653 に答える
1

「変数が初期化されていない可能性があります」というエラーは、その内容を意味します。これらの変数の宣言は while ループ内にあるため、データベースから結果が返されなかった場合はどうなるでしょうか? 答え: while ループの本体が実行されないため、変数は初期化されません。これらの変数をループの外でデフォルト値に宣言してみると、コードがコンパイルされるはずです。

于 2013-05-02T16:46:02.700 に答える