2014-06-21 47 views
0

我已經嘗試過研究此問題,並且大多數時候答案與執行查詢以從數據庫收集數據相關。我試圖將數據插入到數據庫中,我不斷收到此錯誤:遊標狀態無效 - 插入行時沒有當前行

Invalid cursor state - no current row.

這裏是我的代碼: 它沒有問題創建數據庫,但是當我嘗試插入一行我得到的錯誤。

package importtimesheet; 

import java.io.IOException; 
import java.sql.*; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 

public class ImportTimeSheet 
{ 

    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:derby://localhost:1527/PaperRoute"; 

    // Database credentials 
    static final String USER = "<username removed>"; 
    static final String PASS = "<password removed>"; 

    private static String buildit(String names, String types) 
    { 
     String[] na = names.split("\t"); 
     String[] ta = types.split("\t"); 
     String result = "CREATE TABLE HOURS_WORKED ("; 
     for (int i = 0; i < na.length; i++) 
     { 
      result = result + na[i] + " " + ta[i] + " not NULL,"; 
     } 
     result = result + " PRIMARY KEY (DATE))"; 
     return result; 
    } 

    public static void main(String[] args) throws IOException 
    { 
     boolean bon = false; 
     String[] temp; 
     String file_name = "C:\\Users\\Michael_2\\Dropbox\\Programming\\Finished   Programs\\ExportTimeSheet\\data.txt"; 
     String[] aryLines = new String[0]; 
     //IMPORT DATA FROM FLAT FILE 
     try 
     { 
      ReadFile file = new ReadFile(file_name); 
      aryLines = file.OpenFile(); 

    //   for (int i = 0; i < aryLines.length; i++) 
    //   { 
    //    System.out.println(aryLines[i]); 
    //   } 
     } catch (IOException e) 
     { 
      System.out.println(e.getMessage()); 
     } 
     String names = aryLines[0]; 
     String types = aryLines[1]; 

     //CREATE DB TABLE 
     Connection conn = null; 
     Statement stmt = null; 
     ResultSet rs; 

     try 
     { 
      //Register JDBC driver 
      Class.forName("com.mysql.jdbc.Driver"); 

      //Open a connection 
      System.out.println("Connecting to selected database..."); 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 
      System.out.println("Connected to database successfully..."); 

      //Execute a query 
      System.out.println("Creating table in database..."); 
      stmt = conn.createStatement(); 
      String sql = buildit(names, types); 
      stmt.executeUpdate(sql); 
      System.out.println("Created table in database..."); 
     } catch (SQLException se) 
     { 
      //Handle errors for JDBC 
      se.printStackTrace(); 
     } catch (Exception e) 
     { 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     } finally 
     { 
      //finally block used to close resources 
      try 
      { 
       if (stmt != null) 
       { 
        conn.close(); 
       } 
      } catch (SQLException se) 
      { 
      }// do nothing 
      try 
      { 
       if (conn != null) 
       { 
        conn.close(); 
       } 
      } catch (SQLException se) 
      { 
       se.printStackTrace(); 
      }//end finally try 
     }//end try 

     for (int i = 2; i < aryLines.length; i++) 
     { 
      System.out.println(aryLines[i]); 
      temp = aryLines[i].split("\t"); 
      if (temp[1] == "BON") 
      { 
       bon = true; 
      } 
      try 
      { 
       String host = "jdbc:derby://localhost:1527/PaperRoute"; 
       String uName = "<username removed>"; 
       String uPass = "<password removed>"; 
       Connection con; 
       Statement stmnt; 
       con = DriverManager.getConnection(host, uName,uPass); 
       stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,  ResultSet.CONCUR_UPDATABLE); 
       String data = "SELECT * from HOURS_WORKED"; 
       rs = stmnt.executeQuery(data); 

       Time arrived = java.sql.Time.valueOf(temp[2]); 
       Time received = java.sql.Time.valueOf(temp[3]); 
       Time left = java.sql.Time.valueOf(temp[4]); 
       Time started = java.sql.Time.valueOf(temp[5]); 
       Time finished = java.sql.Time.valueOf(temp[6]); 
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
       java.util.Date parsed = sdf.parse(temp[0]); 
       java.sql.Date sql = new java.sql.Date(parsed.getTime()); 
       String[] na = names.split("\t"); 
       rs.moveToInsertRow(); 
       rs.updateDate(1, sql); 
       rs.updateBoolean(2, bon); 
       rs.updateTime(3, arrived); 
       rs.updateTime(4, received); 
       rs.updateTime(5, left); 
       rs.updateTime(6, started); 
       rs.updateTime(7, finished); 
       rs.updateRow(); 
       System.out.println(sql);`` 
      } catch (ParseException pe) 
      { 
       System.out.println(pe.getMessage()); 
      } catch (SQLException se) 
      { 
       System.out.println(se.getMessage()); 
      } 
     } 
    } 
} 
+2

你能發佈拋出的異常嗎? – Typo

+0

沒有一個。這是我的輸出的一個片段: 2014年6月11日\t WED \t 2時40分零零秒\t 2點45分00秒\t 3點55分零零秒\t 4點05分00秒\t 6時35分00秒 遊標狀態無效 - 沒有當前行。 2014年6月12日\t THU \t 3時25分00秒\t 3時25分00秒\t 4點35分00秒\t 4點50分00秒\t 07:00:00 遊標狀態無效 - 無當前行。 2014年6月13日\t FRI \t 2時25分00秒\t 2時三十〇分00秒\t 3時45分〇〇秒\t 04:00:00 \t 6時40分零零秒 遊標狀態無效 - 無當前行。 2014年6月14日\t SAT \t 3時35分零零秒\t 3時四十零分00秒\t四點55分00秒\t 5時05分00秒\t七時35分00秒 遊標狀態無效 - 無當前行。 2014年6月15日\t SUN \t 2點50分零零秒\t 03:00:00 \t 04:30:00 \t 4時45分00秒\t七時30分00秒 遊標狀態無效 - 無當前行。 BUILD SUCCESSFUL(總時間:2秒) – Anazopyreo

+0

我嘗試使用System.out.println(「行號」)來縮小問題發生的位置。它似乎是行rs.updateRow(); – Anazopyreo

回答

1

我相信你忘了你的查詢的東西,

String data = "SELECT * from HOURS_WORKED"; 

應該

String data = "SELECT * from HOURS_WORKED FOR UPDATE"; 
+0

我從來沒有見過這個,它似乎沒有效果。不過我會記住它的。這可能是難題的一部分。 – Anazopyreo

0

我不知道這是如何工作的一切;你正在註冊一個MySQL驅動程序,但使用德比URL。

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:derby://localhost:1527/PaperRoute"; 

也許你想(對Derby數據庫):

static final String JDBC_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 
static final String DB_URL = "jdbc:derby://localhost:1527/PaperRoute"; 

或(對於MySQL數據庫):

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:mysql://localhost:1527/PaperRoute"; 

根據您所使用的數據庫。

也有不同的地方,你'硬編碼'的價值,而不是使用這些常數。

+0

我認爲這是我出錯的地方。我試圖將來自兩個不同網站的指示混合在一起,討論如何使用數據庫。我正嘗試在NetBeans中使用數據庫。我注意到有人在使用德比和其他的mysql,但並沒有真正理解差異,我只是試圖讓一些東西在一起。 – Anazopyreo

+0

我清理了一下我的代碼。我做了一個方法來連接,所以它在一個地方 私人靜態連接連接() {user =「」; String password =「」; String driver =「org.apache.derby.jdbc.EmbeddedDriver」; String url =「jdbc:derby:// localhost:1527/PaperRoute」; Connection con = null; try { con = DriverManager.getConnection(url,user,password); } catch(SQLException s) { } return con; } – Anazopyreo

+0

我仍然收到同樣的錯誤。 – Anazopyreo

1

我終於明白,我得到這個錯誤的原因是我有rs.updateRow();我應該有rs.insertRow(); 現在我已經做了更正(以及上面提到的其他更改),我不再收到錯誤,但數據庫沒有更新。但我想這是一個不同的問題。