SQL可以正常工作,但预编译的语句没有

时间:2016-05-22 22:49:26

标签: java mysql prepared-statement

我准备好的语句在日志中产生以下错误“getVenturerSectDtls中的SQLException:java.sql.SQLException:未找到列'公里'。

准备好的声明是:

    -- Nathan Rivera
-- Week 3 Lab

-- Drop Tables if they exist

set foreign_key_checks = 0;

drop table if exists ZIPCODE;
drop table if exists INSTRUCTOR;
drop table if exists STUDENT;
drop table if exists COURSE;
drop table if exists SECTION;
drop table if exists ENROLLMENT;

set foreign_key_checks = 1;



create table ZIPCODE
(

Zip     integer (11),
City    VARCHAR (25)    NOT NULL,
State   CHAR    (2)     NOT NULL,
constraint pk_zip primary key (Zip)
);

create table INSTRUCTOR
(

Instructor_ID       integer(6),
Salutation          varchar(5),
First_Name          varchar(25) not null,
Last_Name           varchar(25) not null,
Street_Address      varchar(50),
Zip                 integer(11) not null,
constraint pk_instructor_id primary key (Instructor_ID)
);




create table STUDENT
(
Student_ID          integer(6),
Salutation          varchar(5),
First_Name          varchar(25) not null,
Last_Name           varchar(25) not null,
Street_Address      varchar(50),
Zip                 integer(11) not null,
Phone               varchar(15) not null,
Employer            varchar(50),
Registration_Date   date        not null,
constraint pk_student_id primary key (Student_ID)
);




create table COURSE
(
Course_ID       integer(6),
Description     varchar(50) not null,
Cost            decimal(8,2),
-- on the Data Dictionary it says that Prerequisite is a FK however there are no other tables that have this entry so i cannot create a FK for or from it
Prerequisite    integer(6),
constraint pk_course_id primary key (Course_ID)
);



create table SECTION
(
Section_ID          integer(8),
Course_Section_Name integer(6)  not null,
Start_Date_Time     datetime    not null,
Location            varchar(10),
Capacity            integer(3),
Instructor_ID       integer(6)  not null,
Course_ID           integer(6)  not null,
constraint pk_section_id primary key (Section_ID)
);

create table ENROLLMENT
(
Section_ID  integer(8),
Student_ID  integer(6),
Enroll_Date date        not null,
Final_Grade char(1),
constraint pk_section_id_enrollment primary key (Section_ID, Student_ID)
);

alter table INSTRUCTOR add constraint fk_zip_instructor foreign key (Zip) references ZIPCODE (Zip);
alter table STUDENT add constraint fk_zip_student foreign key (Zip) references ZIPCODE (Zip);
alter table SECTION add constraint fk_instructor_id_section foreign key (Instructor_ID) references INSTRUCTOR (Instructor_ID);
alter table SECTION add constraint fk_course_id_section foreign key (Course_ID) references COURSE (Course_ID);
alter table ENROLLMENT add constraint fk_section_id_enrollment foreign key (Section_ID) references SECTION (Section_ID);


insert into ZIPCODE (Zip,City,State) 
values 
(7024,"Ft. Lee","NJ"),
(7047,"North Bergen","NJ"),
(10005,"New York","NY"),
(10015,"New York","NY"),
(10025,"New York","NY"),
(10035,"New York","NY"),
(11419,"Richmond Hill","NY"),
(11435,"Jamaica","NY");

insert into STUDENT (Student_ID, Salutation, First_Name, Last_Name, Street_Address, Phone, Employer, Registration_Date, Zip) 
values
(102,"Mr.","Fred","Crocitto","101-09 120th St.",718-555-5555,"Albert Hildegard Co.",1/22/2007,11419),
(103,"Ms.","J.","Landry","7435 Boulevard East #45",201-555-5555,"Albert Hildegard Co.",1/22/2007,7047),
(104,"Ms.","Laetia","Enison","144-61 87th Ave",718-555-5555,"Albert Hildegard Co.",1/22/2007,11435),
(105,"Mr.","Angel","Moskowitz","320 John St.",201-555-5555,"Alex. & Alexander",1/22/2007,7024),
(163,"Ms.","Nicole","Gillen","4301 N Ocean #103",904-555-5555,"Oil of America Corp.",2/2/2007,10025),
(223,"Mr.","Frank","Pace","13 Burlington Dr.",203-555-5555,"Board Utilities",2/8/2007,10025),
(399,"Mr.","Jerry","Abdou","460 15th St. #4",718-555-5555,"Health Mgmt.Systems",2/23/2007,10025);

insert into INSTRUCTOR (Instructor_ID, Salutation, First_Name, Last_Name, Street_Address, Zip)
values
(101,"Mr","Fernand","Hanks","100 East 87th",10015),
(102,"Mr","Tom","Wojick","518 West 120th",10025),
(103,"Ms","Nina","Schorin","210 West 101st",10025),
(104,"Mr","Gary","Pertez","34 Sixth Ave",10035),
(105,"Ms","Anita","Morris","34 Maiden Lane",10015),
(106,"Rev","Todd","Smythe","210 West 101st",10025),
(107,"Dr","Marilyn","Frantzen","254 Bleeker",10005);

insert into COURSE (Course_ID,Description,Cost,Prerequisite)
values
(330,"Network Administration",1195,130),
(310,"Operating Systems",1195,NULL),
(142,"Project Management",1195,20),
(140,"Systems Analysis",1195,20),
(130,"Intro to Unix",1195,310),
(25,"Intro to Programming",1195,140),
(20,"Intro to Information Systems",1195,NULL);

insert into SECTION (Section_ID,Course_ID,Course_Section_Name,Start_Date_Time,Location,Instructor_ID,Capacity)
values
(81,20,2,"2007-7-24 9:30:00","L210",103,15),
(86,25,2,"2007-6-10 9:30:00","L210",107,15),
(89,25,5,"2007-5-15 9:30:00","L509",103,25),
(92,25,8,"2007-6-13 9:30:00","L509",106,25),
(104,330,1,"2007-7-14 10:30:00","L511",104,25),
(119,142,1,"2007-7-14 9:30:00","L211",103,25),
(155,122,4,"2007-5-4 9:30:00","L210",107,15);

所以我接受SELECT语句并将其粘贴到MS Word中并将所有“和+替换为空格并将?替换为30(从客户端传递的值)然后将结果传递给phpMyAdmin SQL并运行并返回预期的结果。

我正在使用Eclipse,GWT,java,mySQL。

那么准备好的声明有什么问题吗?

我在phpMyAdmin中使用的编辑代码是:

    String selectQry = ("SELECT cdID, surname, firstName, dob, archived, scoutNo, grpName, startDate, endDate, SUM(kilometres), SUM(nights) from ( " +
            "SELECT at_cub_details.cd_id as cdID, at_cub_details.cd_surname as surname, at_cub_details.cd_first_name as firstName," +
                " at_cub_details.cd_dob as dob, at_cub_details.cd_archived as archived, at_cub_details.cd_scout_no as scoutNo, " +
                " at_group.grp_name as grpName, " +
                " at_section_details.sd_start_date as startDate, at_section_details.sd_end_date as endDate, " +
                " null as kilometres, null as nights " +
            " FROM at_cub_details, at_account_group, at_group, at_section_details " + 
            " WHERE at_account_group.acc_id = ? " + 
                " AND at_account_group.grp_id = at_cub_details.grp_id " +
                " AND at_cub_details.grp_id = at_group.grp_id " +
                " AND at_cub_details.cd_id = at_section_details.cd_id " +
                " AND at_section_details.sd_start_date = (SELECT MIN(section.sd_start_date) " +
                                                            " FROM  at_section_details section " +
                                                            " WHERE section.cd_id = at_cub_details.cd_id " +
                                                                " AND section.sd_section = at_group.grp_section) " +

    " UNION ALL " +

    " SELECT at_cub_details.cd_id as cdID, at_cub_details.cd_surname as surname, at_cub_details.cd_first_name as firstName," +
        " null as dob, null as archived, null as scoutNo, " +
        " null as grpName, " +
        " null as startDate, null as endDate, " +
        " SUM(hr_kilometres) as kilometres, SUM(hr_nights_under_canvas) as nights " +  
     " FROM at_account_group, at_group, at_section_details, at_cub_details, at_hiking_record " +
     " WHERE at_account_group.acc_id = ? " +
         " AND at_account_group.grp_id = at_cub_details.grp_id " +
         " AND at_cub_details.grp_id = at_group.grp_id " +
         " AND at_cub_details.cd_id = at_section_details.cd_id " +
         " AND at_section_details.sd_start_date = (SELECT MIN(section.sd_start_date) " +
                                                     " FROM  at_section_details section " +
                                                     " WHERE section.cd_id = at_cub_details.cd_id " +
                                                         " AND section.sd_section = at_group.grp_section) " +
         " AND at_cub_details.cd_id = at_hiking_record.cd_id " +
         " AND at_hiking_record.hr_start_date > (SELECT MIN(section2.sd_start_date) " +
                                                                 " FROM  at_section_details section2 " +
                                                                 " WHERE section2.cd_id = at_cub_details.cd_id " +
                                                                     " AND section2.sd_section = at_group.grp_section) " +
     " GROUP BY at_cub_details.cd_id " +

    " UNION ALL " +

    " SELECT at_cub_details.cd_id as cdID, at_cub_details.cd_surname as surname, at_cub_details.cd_first_name as firstName," +
        " null as dob, null as archived, null as scoutNo, " +
        " null as grpName, " +
        " null as startDate, null as endDate, " +
        " null as kilometres, SUM(event_nights_canvas) as nights " +
     " FROM at_account_group, at_group, at_section_details, at_cub_details, at_event " +
     " WHERE at_account_group.acc_id = ? " +
         " AND at_account_group.grp_id = at_cub_details.grp_id " +
         " AND at_cub_details.grp_id = at_group.grp_id " +
         " AND at_cub_details.cd_id = at_section_details.cd_id " +
         " AND at_section_details.sd_start_date = (SELECT MIN(section.sd_start_date) " +
                                                     " FROM  at_section_details section " +
                                                     " WHERE section.cd_id = at_cub_details.cd_id " +
                                                         " AND section.sd_section = at_group.grp_section) " +
         " AND at_cub_details.cd_id = at_event.cd_id " +
         " AND at_event.event_date_start > (SELECT MIN(section2.sd_start_date) " +
                                                                 " FROM  at_section_details section2 " +
                                                                 " WHERE section2.cd_id = at_cub_details.cd_id " +
                                                                     " AND section2.sd_section = at_group.grp_section) " +
        " GROUP BY at_cub_details.cd_id " +
     " ) a " +
      "GROUP BY surname, firstName;");

1 个答案:

答案 0 :(得分:0)

在第一个选择语句中更改" SUM(千米),SUM(夜晚)"以公里为单位的公里数(公里数),晚上的公式(晚数)和#34;。