创建一个准备好的语句错误

时间:2016-05-29 21:30:23

标签: java mysql

我尝试使用mySql数据库在java中创建预准备语句。这不起作用,但我知道如果我硬编码它会。我错过了什么?你能指出我正确的方向吗?

目标是将dbList中的值插入db表。我将表名和arraylist传递给write函数。该方法确定arraylist中有多少项并创建preparestatement。这似乎很好。最初,我使用硬编码值进行测试,当我这样做时它会起作用:

      preparedStatement.setString(1, "100");
      preparedStatement.setString(2, "20160549");
      preparedStatement.setString(3, "11:30");
      preparedStatement.setString(4, "860232630");
      preparedStatement.setString(5, "Brenda");
      preparedStatement.setString(6, "Joy");
      preparedStatement.setString(7, "BrnMulligan@gmail.com");
      preparedStatement.setString(8, "0");
      preparedStatement.setString(9, "Coast Guard");
      preparedStatement.setString(10, "Veteran");
      preparedStatement.setString(11, "1");
     */
      preparedStatement.executeUpdate();
      }

但是当我这样做时:

      String strData="";
      for(int i=1;i<fieldCount;i++){
         //fields start with 1
          System.out.println("i is: "+i);
         //array starts at 0
          int j = i-1;
          strData="\"" + dataArray.get(j) + "\"";
          System.out.println("Data is: "+strData);
          preparedStatement.setString(i,strData);
      }

它停止了。没有错误的堆栈输出,另一个选项卡打开:

output of new tab

enter image description here

我还没有体验它能告诉我什么的经验。我虽然包括整个方法会有所帮助。

 public static void mySqlWrite(String tableName, ArrayList dataArray)        throws SQLException, ClassNotFoundException{
          //Lets take a look at whats comming in...
          System.out.println("The table name is : " + tableName);
          System.out.println("The dataArray is : " + dataArray.size());

          //Since we want to re-use this code for every write to the db,
          // we start manipulating the incoming data 

          int fieldCount = dataArray.size()+1;
          // we can start anywhere, so lets start with the table of the  database
          // we want to insert the values into.
          String strPrepStatement="";

          for(int i=1;i<fieldCount;i++){
              System.out.println("i is: "+i);
              //create the placeholders for the data
              strPrepStatement= strPrepStatement + "?,";
              System.out.println("Statement is: "+strPrepStatement);
          }

          //There's an extra comma, so we remove it.
                                       strPrepStatement=strPrepStatement.substring(0,strPrepStatement.length()-1);


          // Lets see if it worked.           
          System.out.println("Statement is: "+strPrepStatement);

          //Lets create the prepstatement now
          String strPrep = "insert into vms." + tableName + " values (" + strPrepStatement +
                  ")";
          System.out.println("PrepStatement is: "+strPrep);

        // This will load the MySQL driver
          Class.forName("com.mysql.jdbc.Driver");
      // Setup the connection with the DB
      connect = DriverManager
          .getConnection(url,sqluser,sqluserpw);

          // PreparedStatements can use variables and are more efficient
          preparedStatement = connect
              .prepareStatement(strPrep);
          String strData="";
          for(int i=1;i<fieldCount;i++){
              System.out.println("i is: "+i);
              int j = i-1;
              strData="\"" + dataArray.get(j) + "\"";
              System.out.println("Data is: "+strData);
              preparedStatement.setString(i,strData);
          }
     /*     
          preparedStatement.setString(1, "100");
          preparedStatement.setString(2, "20160549");
          preparedStatement.setString(3, "11:30");
          preparedStatement.setString(4, "860232630");
          preparedStatement.setString(5, "Brenda");
          preparedStatement.setString(6, "Joy");
          preparedStatement.setString(7, "BrnMulligan@gmail.com");
          preparedStatement.setString(8, "0");
          preparedStatement.setString(9, "Coast Guard");
          preparedStatement.setString(10, "Veteran");
          preparedStatement.setString(11, "1");
         */
          preparedStatement.executeUpdate();
          }

      // You need to close the resultSet
      private static void closeDB() {
        try {
          if (resultSet != null) {
            resultSet.close();
          }

          if (statement != null) {
            statement.close();
          }

          if (connect != null) {
            connect.close();
          }
        } catch (Exception e) {

        }
      }

0 个答案:

没有答案