gpt4 book ai didi

java - mysql jdbc 字段列表中的未知列 'startdate'

转载 作者:行者123 更新时间:2023-11-30 00:13:25 29 4
gpt4 key购买 nike

我试图从名为“course”的 mysql 表中提取日期列“startdate”,但无法执行此操作。它显示未知列 'startdate' 我正在使用 jsp 获取日期并在 jsp 文件中打印。列“startdate”被提取并存储在java.mysql.Date 类型中。但仍然显示异常。我无法弄清楚它是什么。显示以下异常。

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>

最佳答案

由于使用 Scriptlet,代码中存在一些问题

  1. 未结束以下 if block 的 }

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

    sdate,edate;

应该是Date sdate,edate;

<小时/>

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

JSP - Standard Tag Library (JSTL) Tutorial 上查找更多示例

关于java - mysql jdbc 字段列表中的未知列 'startdate',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23862729/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com