Java,Mysql-列名称标识为变量

时间:2017-06-22 05:02:14

标签: java mysql database variables scope

 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中,

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

regNo被识别为变量。但不是列名。?任何想法。为什么? 代码的其他部分没有错误。我已经多次检查过。有人可以指出错误的地方吗?

2 个答案:

答案 0 :(得分:2)

该值需要单引号

e.g。

SELECT * from reg_courses WHERE regNo='SC9208' and day=1 and start=14.00 LIMIT 1

虽然最好使用PreparedStatementsetString

答案 1 :(得分:1)

更新您的查询变量:

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