当servlet执行时,我收到了MySQL Exception。我找不到查询中语法出错的位置。我得到以下异常 -
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在's1.employee_id ='I123'和s1.creation_date = DATE_FORMAT(sysdate(),'%Y-%m-%d') - '附近使用正确的语法第1行
下面是包含mysql查询的servlet -
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class HomepageTable extends connection {
String sql,table;
ResultSet rs = null;
private static final long serialVersionUID = 1L;
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
String asWeek = dateFormat.format(now);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
System.out.println(asWeek);
get_connection();
if(asWeek.equals("Mon"))
{
sql="select e1.employee_name,d1.dealer_name , d2.distributor_name , s1.invoice_id ,s1.invoice_created_date, r1.region_name , a1.area_name , s1.payment_date, s1.amount " +
"from sales_master_table s1 " +
"left join dealers_info d1 on s1.dealer_visited = d1.dealer_id " +
"left join distributors_info d2 on s1.distributor_visited = d2.distributor_id " +
"left join territory_master_table r1 on r1.region_code = s1.region" +
"left join area_master_table a1 on a1.area_code = s1.area" +
"left join employee_master_table e1 on e1.employee_id=s1.employee_id" +
"where " +
"s1.employee_id=? and" +
"s1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')- INTERVAL 2 DAY" +
"union" +
"select e1.employee_name,d1.dealer_name , d2.distributor_name , s1.invoice_id ,s1.invoice_created_date, r1.region_name , a1.area_name , s1.payment_date, s1.amount " +
"from sales_master_table s1" +
"left join dealers_info d1 on s1.dealer_visited = d1.dealer_id" +
"left join distributors_info d2 on s1.distributor_visited = d2.distributor_id " +
"left join territory_master_table r1 on r1.region_code = s1.region" +
"left join area_master_table a1 on a1.area_code = s1.area" +
"left join employee_master_table e1 on e1.employee_id=s1.employee_id" +
"where" +
"s1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')- INTERVAL 2 DAY" +
"and level1_head = (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?)" +
"or level2_head = (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?)" +
"or level3_head= (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?)";
}
else
{
sql="(select e1.employee_name,d1.dealer_name , d2.distributor_name , s1.invoice_id ,s1.invoice_created_date, r1.region_name , a1.area_name , s1.payment_date, s1.amount " +
"from sales_master_table s1 " +
"left join dealers_info d1 on s1.dealer_visited = d1.dealer_id " +
"left join distributors_info d2 on s1.distributor_visited = d2.distributor_id " +
"left join territory_master_table r1 on r1.region_code = s1.region" +
"left join area_master_table a1 on a1.area_code = s1.area" +
"left join employee_master_table e1 on e1.employee_id=s1.employee_id" +
"where " +
"s1.employee_id=? and " +
"s1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')-INTERVAL 2 DAY)" +
" union" +
"(select e1.employee_name,d1.dealer_name , d2.distributor_name , s1.invoice_id ,s1.invoice_created_date, r1.region_name , a1.area_name , s1.payment_date, s1.amount " +
"from sales_master_table s1" +
"left join dealers_info d1 on s1.dealer_visited = d1.dealer_id" +
"left join distributors_info d2 on s1.distributor_visited = d2.distributor_id " +
"left join territory_master_table r1 on r1.region_code = s1.region" +
"left join area_master_table a1 on a1.area_code = s1.area" +
"left join employee_master_table e1 on e1.employee_id=s1.employee_id" +
"where " +
"s1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')- INTERVAL 2 DAY" +
"and level1_head = (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?)" +
"or level2_head = (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?)" +
"or level3_head= (select distinct e2.employee_name from employee_master_table e2 , sales_master_table s2" +
"where e2.employee_id=s2.employee_id and s2.employee_id=?))";
}
st=con.prepareStatement(sql);
HttpSession session = request.getSession();
String userid = (String)session.getAttribute("userid");
st.setString(1, userid);
st.setString(2, userid);
st.setString(3, userid);
st.setString(4, userid);
rs = st.executeQuery();
table="<thead>" +
"<tr>" +
"<td>Dealer</td>" +
"<td>Distributor</td>" +
"<td>Region</td>" +
"<td>Area</td>" +
"<td>Invoice Date</td>" +
"<td>Payment Date</td>" +
"<td>Amount</td>" +
"</tr></thead><tbody>";
while(rs.next())
{ table+="<tr>";
table+="<td>"+rs.getString("Dealer_name")+"</td>";
table+="<td>"+rs.getString("distributor_name")+"</td>";
table+="<td>"+rs.getString("region_name")+"</td>";
table+="<td>"+rs.getString("area_name")+"</td>";
table+="<td>"+rs.getString("invoice_created_date")+"</td>";
table+="<td>"+rs.getString("payment_date")+"</td>";
table+="<td>"+rs.getString("amount")+"</td>";
table+="</tr>";
}
table+="</tbody>";
response.getWriter().print(table);
st.close();
rs.close();
close_connection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
"left join employee_master_table e1 on e1.employee_id=s1.employee_id" +
"where" +
"s1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')- INTERVAL 2 DAY" +
在许多地方,您不会在关键字之间添加空格。上面的片段变成了:
left join employee_master_table e1 on e1.employee_id=s1.employee_idwheres1.creation_date= DATE_FORMAT(sysdate(), '%Y-%m-%d')- INTERVAL 2 DAY
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
不要像这样构建SQL。尝试使用存储过程或至少从代码中获取查询并进入资源文件。
答案 1 :(得分:1)
检查空格。 SQL对它很严格。
另外,我建议您学习如何调试代码。最简单的方法是回显SQL并查看它的外观。然后复制回显的SQL,在准备语句时添加值并直接在服务器上运行查询(mysqlworkbench或phpmyadmin)。
答案 2 :(得分:1)
您的SQL查询缺少空格。当连接两个字符串时,没有插入空格,所以你的mysql查询并不完全是你写的,因为它缺少空格。
打印您的Sql查询..您将理解