gpt4 book ai didi

java - 如何在上传前将 CSV 文件中的记录与数据库中的记录进行比较

转载 作者:行者123 更新时间:2023-11-30 22:19:06 25 4
gpt4 key购买 nike

我正在尝试将 CSV 文件上传到包含学生成绩的数据库中。我想更新数据库中已存在的学生记录的字段,并为不存在的学生记录插入新记录。 CSV 文件包含新学生和现有学生的记录。

我使用两个字段 - idnumbercourseCode 来唯一标识数据库和 CSV 文件中的学生记录。

这是我到目前为止尝试过的...请问有人能帮帮我吗?

try{

String updateSql = "update ResultUpload set idnumber = ?,ca = ?, exams = ?, total = ?, AAA=?,BPLUS=?,BBB=?,CPLUS=?,CCC=?,DDD=?,EEE=?,FFF=?,gpp=?,ugp=?,remarks=?,unit=?,level=?,courseCode=?,courseName=?,semester=? where idnumber=? and courseCode=?";
String insertSql = "Insert into ResultUpload (idnumber,ca,exams,total,AAA,BPLUS,BBB,CPLUS,CCC,DDD,EEE,FFF,gpp,ugp,remarks,unit,level,courseCode,courseName,semester) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String sql = "select idnumber, courseCode from ResultUpload where idnumber = ? and courseCode = ?"; // no need to execute this statement inside a loop

BufferedReader br = new BufferedReader(new FileReader(filename));
Connection conn = null;

PreparedStatement insertStatement = conn.prepareStatement(insertSql);
PreparedStatement updateStatement = conn.prepareStatement(updateSql);
PreparedStatement pst = conn.prepareStatement(sql);

String line;
while ((line = br.readLine()) != null) {
String[] value = line.split(","); // check this line that it is not null
pst.setString(1, value[0]);
pst.setString(2, value[17]);
ResultSet rs = pst.executeQuery();
if (rs.next()) { // if there is an entry in the DB make update
updateRecord(updateStatement, value);
update_table();
} else {
insertRecord(insertStatement, value);
update_table();
}
rs.close();
}

br.close(); // close these resources in the finally block
insertStatement.close();
updateStatement.close();
conn.close();





}
catch (SQLException | IOException ex) {
ex.printStackTrace(); // handle exception here
}
// handle exception here



finally{
try{
pst.close();
rs.close();
}
catch(Exception e){}
}





}
}


private static void updateRecord(PreparedStatement preparedStatement,String[] value) throws SQLException {
preparedStatement.setString(1, value[0]);
preparedStatement.setString(2, value[1]);
preparedStatement.setString(3, value[2]);
preparedStatement.setString(4, value[3]);
preparedStatement.setString(5, value[4]);
preparedStatement.setString(6, value[5]);
preparedStatement.setString(7, value[6]);
preparedStatement.setString(8, value[7]);
preparedStatement.setString(9, value[8]);
preparedStatement.setString(10, value[9]);
preparedStatement.setString(11, value[10]);
preparedStatement.setString(12, value[11]);
preparedStatement.setString(13, value[12]);
preparedStatement.setString(14, value[13]);
preparedStatement.setString(15, value[14]);
preparedStatement.setString(16, value[15]);
preparedStatement.setString(17, value[16]);
preparedStatement.setString(18, value[17]);
preparedStatement.setString(19, value[18]);
preparedStatement.setString(20, value[19]);


preparedStatement.executeUpdate();
}

private static void insertRecord(PreparedStatement preparedStatement,String[] value) throws SQLException {
preparedStatement.setString(1, value[0]);
preparedStatement.setString(2, value[1]);
preparedStatement.setString(3, value[2]);
preparedStatement.setString(4, value[3]);
preparedStatement.setString(5, value[4]);
preparedStatement.setString(6, value[5]);
preparedStatement.setString(7, value[6]);
preparedStatement.setString(8, value[7]);
preparedStatement.setString(9, value[8]);
preparedStatement.setString(10, value[9]);
preparedStatement.setString(11, value[10]);
preparedStatement.setString(12, value[11]);
preparedStatement.setString(13, value[12]);
preparedStatement.setString(14, value[13]);
preparedStatement.setString(15, value[14]);
preparedStatement.setString(16, value[15]);
preparedStatement.setString(17, value[16]);
preparedStatement.setString(18, value[17]);
preparedStatement.setString(19, value[18]);
preparedStatement.setString(20, value[19]);


preparedStatement.executeUpdate();

}

最佳答案

我稍微修改了你的代码如下(但代码不完整;留作作业:-)):

try {
// Create sql statements

String updateSql = "update ResultUpload set ca = ?, exams = ?, total = ?, AAA=?, BPLUS=?, BBB=?, CPLUS=?, CCC=?, DDD=?, EEE=?, FFF=?, gpp=?, ugp=?, remarks=?, unit=?, level=?, courseName=?, semester=? where idnumber=? and courseCode=?";
String insertSql = "Insert into ResultUpload (idnumber,ca,exams,total,AAA,BPLUS,BBB,CPLUS,CCC,DDD,EEE,FFF,gpp,ugp,remarks,unit,level,courseCode,courseName,semester) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String sql = "select idnumber, courseCode from ResultUpload where idnumber = ? and courseCode = ?";

BufferedReader br = new BufferedReader(new FileReader("fileName"));
Connection conn = null;

// Create PreparedStatement objects for both queries; and this
// should be done outside of the loop
PreparedStatement insertStatement = conn.prepareStatement(insertSql);
PreparedStatement updateStatement = conn.prepareStatement(upadteSql);
PreparedStatement pst = conn.prepareStatement(sql);

String line;
while ((line = br.readLine()) != null) {
String[] value = line.split(","); // check this line that it is not null
pst.setString(1, value[0]);
pst.setString(2, value[1]);
ResultSet rs = pst.executeQuery();
if (rs.next()) { // if there is an entry in the DB make update
updateRecord(updateStatement, value);
} else {
insertRecord(insertStatement, value);
}
rs.close();
}
br.close(); // close these resources in the finally block
insertStatement.close();
updateStatement.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}

private static void updateRecord(PreparedStatement preparedStatement,
String[] value) throws SQLException {
preparedStatement.setString(19, value[0]); // in where clause: value of idnumber
preparedStatement.setString(20, value[1]); // in where clause: value of coursecode

//
preparedStatement.setString(1, value[ca]); // array index for 'ca'
preparedStatement.setString(2, value[exams]); // array index for 'exams'
// set values for rest of the fields ....

preparedStatemtn.executeUpdate();
}

private static void insertRecord(PreparedStatement preparedStatement,
String[] value) throws SQLException {
preparedStatement.setString(1, value[0]); // index of array for 'idnumber'
preparedStatement.setString(2, value[]); // index of array for 'ca'
// set values for rest of the fields ....

preparedStatement.executeUpdate();
}

here you'll find how to use PreparedStatement .希望对您有所帮助。

关于java - 如何在上传前将 CSV 文件中的记录与数据库中的记录进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37223976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com