更新表时生成异常 - java [英] Exception generated when updating table - java

查看:152
本文介绍了更新表时生成异常 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以正常工作 - 表被更新(在SQL服务器上通过查看执行代码之前和之后的表内容确认)。

The following code works - the table is updated (confirmed on the SQL server by looking at the table contents before and after the code is executed).

但是,当我在NetBeans和命令行中运行程序时,一旦执行 rs = stmt.executeQuery(SQLquery); ,CATCH块就会捕获异常。

But, when I run the program in NetBeans and from a command line, an exception is caught by the CATCH block as soon as the "rs = stmt.executeQuery(SQLquery);" is executed.

我知道这一点,因为CATAL块之前的 System.out.println(更新成功!); 执行。如果我将其移动到 rs = stmt.executeQuery(SQLquery); 之后,也不会执行。

I know this because the line "System.out.println("Updated successfully!");" just before the CATCH block is never executed. Nor is it executed if I move it up to just after "rs = stmt.executeQuery(SQLquery);".

异常错误是:

run:
The statement did not return a result set.
BUILD SUCCESSFUL (total time: 0 seconds)

我不明白我有完成错误。

I can't understand what I have done wrong.

我尝试使用 rs = stmt.executeUpdate(SQLquery); 而不是 rs = stmt。 executeQuery(SQLquery); ,但是当我将鼠标悬停在其上时,NetBeans会带有该注释的红色感叹号:

I did try using "rs = stmt.executeUpdate(SQLquery);" instead of "rs = stmt.executeQuery(SQLquery);", but NetBeans has the red exclamation mark with this note when I hover over it:

incompatible types
    required: ResultSet
    found: int

如果我编译它,NetBeans吐出来:

And if I compile it, NetBeans spits this out:

C:\Java\Example\src\example\Example.java:56: error: incompatible types
            rs = stmt.executeUpdate(SQLquery);
                                   ^
  required: ResultSet
  found:    int
1 error

Grrr。我要坚果了
请帮助!

package example;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class Example
{
    public static void main(String args[])
    {
        String host = "jdbc:sqlserver://SERVER1\\Primrose;databaseName=Primrose";
        String uName = "sa";
        String uPwd = "Pr1m@f@ct";

        String SQLquery = "";

        int totalFolders = 4;

        int getRecId = 0;
        String getUser = "";
        String getSection = "";
        String getKey = "";
        String getValue = "";
        String getExtraInfo = "";

        try
        {
            Connection con = DriverManager.getConnection(host, uName, uPwd);
            Statement stmt = con.createStatement();

            SQLquery = "SELECT * FROM _Registry WHERE (Section = 'RecIds' AND Key_ = '_Folders' AND User_ = 'sc_general')";

            ResultSet rs = stmt.executeQuery(SQLquery);

            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
                newValue = newValue + totalFolders;                 // Change to total + existing value to write back to dB
                getValue = Integer.toString(newValue);              // Convert to string as required by the table
            }

            SQLquery = "UPDATE _Registry SET Value=" + getValue + " WHERE (RecId = 5 AND User_ = 'sc_general' AND Section = 'RecIds' AND Key_ = '_FOLDERS')";

            rs = stmt.executeQuery(SQLquery);

            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( ) );
        }
    }
}


推荐答案

函数stmt.executeUpdate(SQLquery);返回整数,因为您没有从数据库检索任何数据。尝试这样:

The function "stmt.executeUpdate(SQLquery);" returns an Integer, because you are not retrieving any data from the database. Try this:

Integer c = stmt.executeUpdate(SQLquery);

整数值表示有多少行已更改。

The Integer value indicates how many rows have been changed.

这篇关于更新表时生成异常 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆