Connection connection;
   Statement statement=null;

            try{

             connection = DBConnection.getDBConnection().getConnection();
             statement=connection.createStatement();

                 for (int j = 0; j < students.length; j++) {
                    if(students[j][0]!=null){
                        String sql="SELECT * from students WHERE id="+students[j][0]+" LIMIT 1";
                        ResultSet rs=statement.executeQuery(sql);
                        if(rs.next()){
                             String reg=rs.getString("regNo");
                          //  if(students[j][1]==toDayDate){
                                 float num1=Math.round(Float.valueOf(students[j][2]));
                                 String rrr=String.valueOf(num1)+"0";
                                 String sql1="SELECT * from reg_courses WHERE regNo="+reg+" && day="+students[j][4]+" && start="+rrr+" LIMIT 1";
                                ResultSet rs1=statement.executeQuery(sql1);
                                if(rs1.next()){
                                  if(students[j][3]=="I"){
                                        String sql2="UPDATE attendances SET status = '1' WHERE regNo ="+reg+"&& code="+rs1.getString("code")+"&& time="+rrr+"&& date="+students[j][1];
                                          statement.executeUpdate(sql2);

                                  }
                                  else if(students[j][3]=="O"){
                                       String sql2="UPDATE attendances SET statusA = '1',present = '1', WHERE regNo ="+reg+"&& code="+rs1.getString("code")+"&& time="+rrr+"&& date="+students[j][1]+"&& status='1'";
                                      statement.executeUpdate(sql2);
                                  }
                                }

                           // }
                        }

                    }

            }


            }
            catch(Exception e){
                e.printStackTrace();
            }


我的数据库中有3个表,分别是学生,reg_courses和出勤率。每个表都有一列来存储索引号.regNo是所有表的列名。当我运行它时,它说

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'where子句'中的未知列'SC9208'....
SC9208是表中的索引号。在sql1中,

字符串sql1 =“ SELECT * from reg_courses,其中regNo =” + reg +“ && day =” + students [j] [4] +“ && start =” + rrr +“ LIMIT 1”;

regNo被标识为变量。但不能标识为列名。
代码的其他部分没有错误。我已经检查了很多次。有人可以指出有错误的地方吗?

最佳答案

更新查询变量:



String sql1="SELECT * from reg_courses WHERE regNo='"+reg+"' && day='"+students[j][4]+"' && start='"+rrr+"' LIMIT 1";

// In if block
String sql2="UPDATE attendances SET status='1' WHERE regNo='"+reg+"' && code='"+rs1.getString("code")+"' && time='"+rrr+"' && date='"+students[j][1]+"'";

// In else if block
String sql2="UPDATE attendances SET statusA='1', present='1' WHERE regNo='"+reg+"' && code='"+rs1.getString("code")+"' && time='"+rrr+"' && date='"+students[j][1]+"' && status='1'";


根据您的查询,我假设statusstatusAVARCHARCHAR
如果它们是INT,则需要删除单引号,即status=1

10-07 14:45