有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mysql jdbc中字段列表中的java未知列“startdate”

我正在尝试从名为“course”的mysql表中提取日期列“startdate”,但无法执行此操作。它显示了未知列“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) 个答案

  1. # 1 楼答案

    由于使用了Scriptlet,代码中出现了一些问题

    1. 不结束低于if块的}

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

      sdate,edate;
      

    应该是Date sdate,edate;


    注意:尽量避免使用涂鸦,而使用易于使用且不易出错的JavaServer Pages Standard Tag Library

    JSP - Standard Tag Library (JSTL) Tutorial上找到更多样本