我已經嘗試過研究此問題,並且大多數時候答案與執行查詢以從數據庫收集數據相關。我試圖將數據插入到數據庫中,我不斷收到此錯誤:遊標狀態無效 - 插入行時沒有當前行
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());
}
}
}
}
你能發佈拋出的異常嗎? – Typo
沒有一個。這是我的輸出的一個片段: 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
我嘗試使用System.out.println(「行號」)來縮小問題發生的位置。它似乎是行rs.updateRow(); – Anazopyreo