列不在FROM列表Java数据库搜索中的任何表中

时间:2017-12-13 10:03:39

标签: java sql database

我正在研究Java中的数据库程序,我遇到了一个问题,它告诉我我正在搜索的内容不在表中。

public static void displayRecord(ResultSet rs) throws SQLException
{
    String assignment = rs.getString("assignment");
    int grade = rs.getInt("grade");
    System.out.println(assignment + " | " + grade + "\n");
}

public void findGrade(String assignment){

    String assignString;
    int gradeInt;

    try{
        Class.forName(driver).newInstance();
    }
    catch (Exception err){
        System.err.println("Unable to load the embedded driver.");
        err.printStackTrace(System.err);
        System.exit(0);
    }
    Connection connected = null;

    try{

        int ble = 25;
        connected = DriverManager.getConnection(protocol + databaseName);

        Statement statement = connected.createStatement();

        ResultSet rs = null;

        rs = statement.executeQuery("SELECT assignment, grade FROM student WHERE assignment = " + assignment);

        if(rs == null ) {
            System.out.println("Assignment not found!");
        }
        else {
            displayRecord(rs);
        }

        rs.close();

        connected.close();


    }

    catch (SQLException err){
        System.err.println("SQL error.");
        err.printStackTrace(System.err);
        System.exit(0);
    }
}

它最终给我以下错误

  

SQL错误。   java.sql.SQLSyntaxErrorException:列'ASSIGNMENT1'要么不在FROM列表中的任何表中,要么出现在连接规范中,并且不在连接规范的范围内,或者出现在HAVING子句中,并且不在GROUP BY列表中。如果这是CREATE或ALTER TABLE语句,则'ASSIGNMENT1'不是目标表中的列。       at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(未知来源)       在org.apache.derby.impl.jdbc.Util.generateCsSQLException(未知来源)       at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(未知来源)       at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(未知来源)       at org.apache.derby.impl.jdbc.EmbedConnection.handleException(未知来源)       at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)       在org.apache.derby.impl.jdbc.EmbedStatement.execute(未知来源)       at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source)       在Student.findGrade(Student.java:153)       在GradeBookTester.main(GradeBookTester.java:17)

我不确定为什么它会给我错误,因为我的数据库中存在错误。

我将为测试人员和数据库创建者本身提供代码,以查看是否存在错误,但这一切似乎都非常可靠。

数据库:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateGradeBook{

private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static final String protocol = "jdbc:derby:";

public static void main(String[] args)
{
    try{
        Class.forName(driver).newInstance();
        System.out.println("Loaded the embedded driver.");
    }
    catch (Exception err){
        System.err.println("Unable to load the embedded driver.");
        err.printStackTrace(System.err);
        System.exit(0);
    }

    String dbName = "GradeBook";
    Connection connected = null;

    try{
        System.out.println("connecting to and creating the database...");
        connected = DriverManager.getConnection(protocol + dbName + ";create=true");
        System.out.println("database created.");

        Statement statement = connected.createStatement();

        statement.execute("CREATE TABLE student" + "(student_name varchar(50), student_id int, assignment varchar(80), grade int)");
        System.out.println("created Student table.");


        connected.close();
    }

    catch (SQLException err){
        System.err.println("SQL error.");
        err.printStackTrace(System.err);
        System.exit(0);
    }
}

}

这是测试人员的代码

public class GradeBookTester {

public static void main(String[] args) {
    Student bob = new Student();

    bob.setStudentName("Bob");
    bob.setStudnetID(12345);

    bob.addGrade("ASSIGNMENT1", 100);
    bob.addGrade("assignment2", 50);
    bob.addGrade("assignment3", 25);
    bob.addGrade("assignment4", 100);

    bob.displayGrades();

    bob.findGrade("ASSIGNMENT1");
}

}

1 个答案:

答案 0 :(得分:1)

你必须提到带有如下引号的搜索字符串,

rs = statement.executeQuery("SELECT assignment, grade FROM student WHERE assignment = '" + assignment + "'");

问题是您的查询被解释如下,

  

SELECT分配,成绩来自学生WHERE assignment = ASSIGNMENT1

而不是

  

SELECT分配,成绩来自学生WHERE assignment = 'ASSIGNMENT1'

使查询解释器将其视为列而不是值