0

当我尝试在我的数据库(MS - SQL)中插入一些数据时,我遇到了一个小问题。我正在使用这段代码(我只是复制粘贴所有代码,所以有人可能会理解我不理解的东西)

 try 
              {
                  Connection connection = null;
                  Statement Statement = null;
                  ResultSet ResultSet = null;
                  String query = "";

              // jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
              String host = "jdbc:sqlserver://server;databaseName=db";
              String username = "user";
              String password = "pass";
              Statement = connection.createStatement();
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              Calendar cal = Calendar.getInstance();

              DateFormat dateFormat1 = new SimpleDateFormat("dd.MM.yyyy");
              Calendar cal1 = Calendar.getInstance();

              for (int k = 0; k < idArray.length ; k ++ )
              {
                  query = "INSERT INTO Companies (ID,Company,Company2,Company3,Country,ZIP,City,Street," +
                        "Phone,Fax,Email,Internet,CustomerNo,AccountMngr,Icon,AddressSource," +
                        "UserDefined1,UserDefined2,CreatedOn,CreatedBy,ColectedInformation) " +
                        " VALUES ('" + UUID.randomUUID() + "','" + company_nameArray[k] 
                                + "','" + company_name2Array[k] + "','" + company_name3Array[k] + "','DE','"   
                                + zipArray[k] + "','" + cityArray[k] + "','" + streetArray[k] + "','"  
                                + phone_Array[k] + "','" + faxArray[k] + "','" + emailArray[k] + "','"
                                + internetArray[k] + "','" + customer_noArray[k] 
                                + "','d','131','60','Baufinder Import','Import Datum "
                                + dateFormat1.format(cal1.getTime()) + "','" 
                                + dateFormat1.format(cal1.getTime()) + "','d','" 
                                + "cxcxvcx" +  
                                "')";

                  ResultSet = Statement.executeQuery(query);   
              }

              connection.close();
          }
          catch(NullPointerException e)
          {
                System.out.println("NullPointerException");
          }
          catch ( SQLException err ) 
          {
              System.out.println( "SQL Exception:" ) ;

              // Loop through the SQL Exceptions
              while( err != null )
                 {
                  System.out.println( "State  : " + err.getSQLState()  ) ;
                  System.out.println( "Message: " + err.getMessage()   ) ;
                  System.out.println( "Error  : " + err.getErrorCode() ) ;

                  err = err.getNextException() ;
                 }
          }
          catch( Exception e )
          {
              System.out.println( "There is a problem with " + e ) ;
          }

    }

我收到了这个错误:

SQL 异常:状态:空 消息:该语句没有返回结果集。错误:0

我不明白结果测试的问题。我一直在使用它,因为我尝试从另一个数据库读取数据,现在我尝试将它们插入另一个数据库。此外,所有这些表都不包含空值。

先感谢您:)

4

1 回答 1

0

使用Statement#executeUpdate(String)而不是 Statement#execute(query);

for (int k = 0; k < idArray.length ; k ++ )
          {
              query = "INSERT INTO Companies (ID,Company,Company2,Company3,Country,ZIP,City,Street," +
                    "Phone,Fax,Email,Internet,CustomerNo,AccountMngr,Icon,AddressSource," +
                    "UserDefined1,UserDefined2,CreatedOn,CreatedBy,ColectedInformation) " +
                    " VALUES ('" + UUID.randomUUID() + "','" + company_nameArray[k] 
                            + "','" + company_name2Array[k] + "','" + company_name3Array[k] + "','DE','"   
                            + zipArray[k] + "','" + cityArray[k] + "','" + streetArray[k] + "','"  
                            + phone_Array[k] + "','" + faxArray[k] + "','" + emailArray[k] + "','"
                            + internetArray[k] + "','" + customer_noArray[k] 
                            + "','d','131','60','Baufinder Import','Import Datum "
                            + dateFormat1.format(cal1.getTime()) + "','" 
                            + dateFormat1.format(cal1.getTime()) + "','d','" 
                            + "cxcxvcx" +  
                            "')";

              int updatedRowCount = Statement.executeUpdate(query);   
          }

笔记:

  1. 如果某些数据由用户直接输入,请考虑使用PreparedStatement以防止SQL 注入的可能性。
  2. 如果您需要提高插入性能,请尝试使用批量插入。
于 2013-01-28T09:14:45.120 回答