mysql - jsp字符串文字错误

标签 mysql jsp

所以我想使用 fusioncharts 从 jsp 创建图表。我会选择 fusioncharts,然后选择 JFree、canvasjs 或任何其他,因为我有在 php 语言中使用它的经验。在jsp中应该没有问题,因为jsp和php实际上是相同的,映射在html之上,唯一的区别是它们的语法。但是,正如我完成的代码一样,它返回 Stringliteral is notproperly close by a double-quote error on sql 语句。请帮助我,因为我是 jsp 和 java 环境的初学者。谢谢。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="com.google.gson.*" %>
<%@page import="readConfig.readConfig" %>

<%
String hostdb = readConfig.getProperties("conUrl");              // MySQl host
String userdb = readConfig.getProperties("dbUser");              // MySQL username
String passdb = readConfig.getProperties("dbPass");                 // MySQL password
String driver = readConfig.getProperties("dbDriver");           // MySQL driver               

DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection(hostdb , userdb , passdb);

%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Creating Charts with Data from a Database - fusioncharts.com</title>

    <script src="vendor/fusioncharts/fusioncharts.js"></script>
</head>
<body>
     <div id="chart"></div>

    <%@page import="fusioncharts.FusionCharts" %>

    <%
        Gson gson = new Gson();

        String sql= "SELECT m.month,IFNULL(x.cnt, 0) AS cnt FROM
(SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m
LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS cnt FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month
ORDER BY m.month DESC";

        PreparedStatement pt=con.prepareStatement(sql);    
        ResultSet rs=pt.executeQuery();

        Map<String, String> chartobj = new HashMap<String, String>();

        chartobj.put("caption", "Top 10 most populous countries");
        chartobj.put("showValues", "0");
        chartobj.put("theme", "zune");

        ArrayList arrData = new ArrayList();
        while(rs.next()) {
            Map<String, String> lv = new HashMap<String, String>();
            lv.put("label", rs.getString("Monthly"));
            lv.put("value", rs.getString("New Demand"));
            arrData.add(lv);             
        }

        rs.close();

         Map<String, String> dataMap = new LinkedHashMap<String, String>();  

         dataMap.put("chart", gson.toJson(chartobj));
         dataMap.put("data", gson.toJson(arrData));

        FusionCharts columnChart= new FusionCharts(
                    "column2d",                
                    "chart1",                
                    "500","300",            
                    "chart",                
                    "json",                    
                    gson.toJson(dataMap)     
                );

        %>

        <%=columnChart.render()%>

</body>

eclipse 终端出错

Mar 27, 2018 2:07:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/registration] threw exception [Unable to compile class for JSP: 

An error occurred at line: 78 in the jsp file: /dashboard.jsp
String literal is not properly closed by a double-quote
75: 
76: 
77:             // Form the SQL query that returns the number of sales in 2017
78:             String sql= "SELECT m.month, IFNULL(x.cnt, 0) AS cnt FROM
79:     (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m
80:     LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS cnt FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month
81: ORDER BY m.month DESC";


Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 78 in the jsp file: /dashboard.jsp
String literal is not properly closed by a double-quote
75: 
76: 
77:             // Form the SQL query that returns the number of sales in 2017
78:             String sql= "SELECT m.month, IFNULL(x.cnt, 0) AS cnt FROM
79:     (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m
80:     LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS cnt FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month
81: ORDER BY m.month DESC";

最佳答案

在 Java 中字符串文字不允许超过一行。

所以你可以使用'+'像

String sql= "SELECT m.month,IFNULL(x.cnt, 0) AS cnt FROM "+
" (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m "+
" LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS cnt FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month "+
" ORDER BY m.month DESC";

或者写在一行

String sql="SELECT m.month,IFNULL(x.cnt, 0) AS cnt FROM (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS cnt FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month ORDER BY m.month DESC";

关于mysql - jsp字符串文字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49504406/

相关文章:

javascript - 使用 javascript 访问 php 变量

php - 将文本文件导入数据库

php - 散列/加盐时密码不匹配

java - 不能显示多个错误消息。 Java 小服务程序

java - 试图为Java(JSP)中的随机数设置范围,但我不确定如何处理

sql - 如何创建 MySQL 查询别名?

javascript - 将 JSP 文件链接到另一个 JSP 中

java - 如何为 css、js、jsp 文件提供版本控制

html - 使用与 bean 不同的名称将 bean 值输入到 Struts (1.x) 中的隐藏输入中

php - 随机执行 mysql_query。