我尝试过研究这个问题,大多数时候答案都与查询从数据库中收集数据有关。我试图将数据插入数据库,我不断收到此错误:
无效的游标状态 - 没有当前行。
这是我的代码: 它创建数据库没有问题,但是当我尝试插入一行时,我得到了错误。
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());
}
}
}
}
答案 0 :(得分:1)
我相信你在查询中遗忘了一些内容,
String data = "SELECT * from HOURS_WORKED";
应该是
String data = "SELECT * from HOURS_WORKED FOR UPDATE";
答案 1 :(得分:1)
我终于弄明白我之所以遇到这个错误的原因是我有rs.updateRow();我应该有rs.insertRow(); 现在我已经进行了更正(以及上面提到的其他更改)我不再收到错误,但数据库没有更新。但我想这是一个不同的问题。
答案 2 :(得分:0)
我不确定这是怎么回事;您正在注册一个mysql驱动程序,但使用的是derby url。
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:derby://localhost:1527/PaperRoute";
也许你想(对于德比数据库):
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";
取决于您使用的数据库。
还有各种地方可以对这些值进行“硬编码”,而不是使用这些常量。