Mysql ORDER BY长度(col),col不起作用

时间:2014-05-31 08:58:07

标签: jquery html mysql jsp

     Mysql employee table 

Mysql database table

注意:这里的emp_id是varchar

  String query="select * from employee ORDER BY length(emp_id),emp_id";

  if i run only the above query it works fine......
  Query ouput :

enter image description here

当我运行以下jsp代码时,此查询无法正常工作

<html><head>
        <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <link href="jquery.dataTables.css" rel="stylesheet" type="text/css"/>
    <script src="jquery.dataTables.js"></script>
</head>
<body>
    <div class="container">
        <table id="example" class="display" width="100%" border="1">
            <thead><tr>
                                        <th>EMPLOYEE_ ID</th>
                    <th>NAME</th>
                                        <th>DEPARTMENT</th>
                                        <th>DESIGNATION</th>              
               </tr></thead>            
<tbody>
        <%    
          try
             { 
              String query="select * from employee ORDER BY length(emp_id),emp_id";
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = (Connection)   DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
              Statement stmt=con.createStatement();
              ResultSet rs=stmt.executeQuery(query);
              while(rs.next())
                              {                                   
                               %>
                               <tr> 
                                   <td><%=rs.getString(1)%></td>
                                   <td><%=rs.getString(2)%></td>
                                   <td><%=rs.getString(3)%></td>
                                   <td><%=rs.getString(4)%></td>
                               </tr>
                               <% } %>
    </tbody></table>
                            <%
                              rs.close(); stmt.close();con.close();
             }
              catch(Exception e)
                               {  e.printStackTrace(); }
                            %>          
            </div>
<script>
     $(document).ready( function () {
 var table = $('#example').DataTable();
     } );
    </script></body></html>

输出jsp:

enter image description here 注意:在我的表中,emp_id是varchar .......................................... .................................................. ....................................... 请帮帮我......

2 个答案:

答案 0 :(得分:1)

可能是你的jquery采用默认的排序样式......

答案 1 :(得分:1)

使用JSP Standard Tag LibraryJSP Expression Language代替使用更易于管理且不易出错Scriplet。使用提供JSTL SQL标记的SQL Tag Library来访问JSP中的数据库。


如果查询在数据库中给出了正确的结果,则UI部分中存在错误。请使用JSTL SQL sql:query Tag再次使用以下示例教程。

示例代码:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>
<head>
<title>JSTL sql:query Tag</title>
</head>
    <body>
        <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost/TEST" user="root" password="pass123" />

        <sql:query dataSource="${snapshot}" var="result">
            SELECT * from Employees;
        </sql:query>

        <table border="1" width="100%">
            <tr>
                <th>Emp ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Age</th>
            </tr>
            <c:forEach var="row" items="${result.rows}">
                <tr>
                    <td><c:out value="${row.id}" /></td>
                    <td><c:out value="${row.first}" /></td>
                    <td><c:out value="${row.last}" /></td>
                    <td><c:out value="${row.age}" /></td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

现在尝试访问上面的JSP,它应该显示以下结果:

enter image description here