我试着研究这个问题,大多数时候的答案都与从数据库收集数据的查询有关。我试图插入数据到数据库,我一直得到这个错误:
下面是我的代码:它创建数据库没有问题,但是当我试图插入一行时,我得到了错误。游标状态无效-没有当前行。
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());
}
}
}
}
我相信你在查询中忘记了什么,
String data = "SELECT * from HOURS_WORKED";
应该String data = "SELECT * from HOURS_WORKED FOR UPDATE";
我终于弄清楚,我一直得到这个错误的原因是,我有rs.updateRow();这里应该有rs。insertrow ();现在我已经进行了更正(以及上面提到的其他更改),我不再得到错误,但数据库没有更新。但我想那是另一个问题。
我不确定这是如何工作的;您正在注册mysql驱动程序,但使用Derby 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";
or(对于mysql数据库):
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:1527/PaperRoute";
取决于你正在使用的数据库。
也有不同的地方,你'硬编码'的值,而不是使用这些常量。