serlvet中mysql的语句直接运行没问题 汤姆凯特运行就有语法错
jsp吧
全部回复
仅看楼主
level 3
//这是代码
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
2013年07月01日 01点07分 1
level 10
贴吧的发展需要你的加入,请及时来贴吧看看,如贴吧未能及时解决你的问题,你可以移步到JSP贴吧交流群大家一起交流学习!!
2013年07月04日 01点07分 2
1