双子littleboy 双子littleboy
关注数: 9 粉丝数: 58 发帖数: 1,005 关注贴吧数: 42
serlvet中mysql的语句直接运行没问题 汤姆凯特运行就有语法错 //这是代码 public class queryServlet extends HttpServlet { static String orderid,location,pay,date,detial,id,seller; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { orderid=request.getParameter("orderid"); try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/wuliu"; String username="root"; String password="123"; Connection conn=DriverManager.getConnection(url,username,password); String sql = "select * from wuliu.order where orderid = ? "; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,Integer.parseInt(orderid)); ResultSet rs=ps.executeQuery(sql); while(rs.next()){ location=rs.getString("location"); pay=rs.getString("pay"); date=rs.getString("date"); detial=rs.getString("detial"); seller=rs.getString("seller"); } String[] arr={orderid,date,location,detial,seller,pay}; request.setAttribute("ORDER", arr); rs.close(); ps.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("order_list.jsp").forward(request, response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doGet(request,response); } //这是测试 sql语法是否有错的 运行有结果输出 public static void main(String[] args){ try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/wuliu"; String username="root"; String password="123"; Connection conn=DriverManager.getConnection(url,username,password); String sql = "select * from wuliu.order where orderid = ? "; //PreparedStatement ps = null; String s="1"; PreparedStatement ps = conn.prepareStatement(sql); Integer.parseInt(s); ps.setInt(1, Integer.parseInt(s)); ResultSet rs = ps.executeQuery(); if(rs.next()){ System.out.println(rs.getString("location")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } jsp运行后报错: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
1 下一页