mysql jdbc中字段列表中的未知列'开始日期'

时间:2014-05-26 04:29:29

标签: java mysql sql jsp jdbc

我正在尝试提取日期列' startdate'来自名为' course'的mysql表但无法做到。它显示了未知列' startdate'我正在使用jsp获取日期并在jsp文件中打印。专栏' startdate'被提取并存储在java.mysql.Date type中。但它仍显示异常。我无法弄清楚它是什么。 显示以下异常。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
Unknown column 'startdate' in 'field list'

代码如下:

   <%@ page import="java.sql.*"%>
   <%
    Connection con;
    try
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }
    catch(Exception e)
    {
        System.out.println("Failed to load mySQL driver...");
        return;
    }
    PreparedStatement stat ;
    ResultSet result;
    String url = "jdbc:mysql://localhost/ftk?user=root";    
    con = DriverManager.getConnection(url);
    stat = con.prepareStatement("select COUNT(*) from batch  where startdate is NULL and enddate is NULL");
    result = stat.executeQuery();
    result.next();
    int count = result.getInt(1);
    String batchid[] = new String[count];       
    stat = con.prepareStatement("select batchid from batch where startdate is NULL and enddate is NULL");
    result = stat.executeQuery();
    int i=0;
    while(result.next())
    {
        batchid[i++] = result.getString(1);
    }
   %>
    <div id="form" >
    <center>
    <p style="font-family:arial;color:black;font-size:30px;">Generate Schedule</p>
    <form action="scheduleprocess.jsp" method="post">
    <select name = "batchid" required namestyle="width:150px;float:center">
    <%for(i=0; i<count; i++) 
    { 
    %>
    <option  value="<%=batchid[i]%>"><%= batchid[i] %></option>
    <% } 
    %>


       <input type="date" BORDER=5 name="date" value="Date" required>
       <input type="submit" value="Generate">
       </form>
       <%
        if(request.getParameter("schedule")!=null)
        {%>
        <center><script type="text/javascript">
        alert("Batch scheduled successfully! ");
        </script></center>
          </select>
       <p style="font-family:arial;color:black;font-size:30px;">Display Schedule</p>
       <p style="font-family:arial;color:black;font-size:20px;">Phase 1</p>
       <p style="font-family:arial;color:black;font-size:20px;">Foundation Training</p>

       <table style="width:500px">
     <tr style="background-color:grey;">
       <th>CourseID</th>
       <th>Course Name</th>     
       <th>Category</th>
       <th>Start Date</th>
       <th>End Date</th>
     </tr>
        <%
        Statement stmt;
        String coursename,category;
        sdate,edate;
        int courseid,cid;
        stmt = con.createStatement();
        result = stmt.executeQuery("select courseid,coursename,category,startdate,enddate from course where phase=1");
        while(result.next())
        {
            courseid = result.getInt(1);
            coursename = result.getString(2);
            category = result.getString(3);
            sdate = result.getDate(4);
            edate = result.getDate(5);  
            %>
     <tr>
       <td><%= courseid %></td>
       <td><%= coursename %></td>       
       <td><%= category %></td>
       <td><%= sdate %></td>
       <td><%= edate %></td>

     </tr>
      <%
      } 
      %> 

     </table>
     <p style="font-family:arial;color:black;font-size:20px;">Phase 2</p>
       <p style="font-family:arial;color:black;font-size:20px;">Technology Training</p>
       <table style="width:500px">
     <tr style="background-color:grey;">
       <th>CourseID</th>
       <th>Course Name</th>     
       <th>Start Date</th>
       <th>End Date</th>
     </tr>
     <%
        result = stat.executeQuery(); 
        while(result.next())
        {
        courseid = result.getInt(1);
        coursename = result.getString(2);   
        //sdate = result.getDate(4);
        //edate = result.getDate(5);    
        %>
     <tr>
       <td><%= courseid %></td>
       <td><%= coursename %></td>       

     </tr>
      <%
      } 
      %>
     </table>

1 个答案:

答案 0 :(得分:0)

由于使用 Scriptlet

,您的代码中存在一些问题
  1. 未结束}块下面的if

    if(request.getParameter("schedule")!=null){
    
  2. 忘记声明sdateedate的类型,如下所示

    sdate,edate;
    
  3. 应为Date sdate,edate;


    注意:尽可能避免使用Scriplets,而是使用易于使用且不易出错JavaServer Pages Standard Tag Library

    JSP - Standard Tag Library (JSTL) Tutorial

    上查找更多示例