日期:2014-05-17  浏览次数:20908 次

Java中调用两个SQL语句
我的想法是:先用一条查询语句查询数据库中属否存在该数据(也就是订单号:id),如果存在则通过该id对任务表(rw)进行添加,而我没有用查询条件进行向rw表添加数据时显示正常,没有乱码,而我在加了一个条件:先进行查询确认后再添加,结果都是乱码,不论是向数据库中添加的数据还是在网页上显示的数据,下面是我的代码:
改动之后的代码:
DBOperator DB = new DBOperator();
ResultSet rs = null;
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
String sql = "insert into rw(cphm,sjxm,rwid,wtr,mdd,yn) values(?,?,?,?,?,?)";
String sql1 = "select * from dingdan where id ="+Integer.parseInt(request.getParameter("rwid"));
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>增派任务</TITLE></HEAD>");
out.println(" <BODY>");

try {
conn = DB.createConnection();
rs = DB.executeQuery(sql1);
if(rs.next()){
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("cphm").trim());
pstmt.setString(2, request.getParameter("sjxm").trim());
pstmt.setString(3, request.getParameter("rwid").trim());
pstmt.setString(4, request.getParameter("wtr").trim());
pstmt.setString(5, request.getParameter("mdd").trim());
pstmt.setString(6, "0");
result = pstmt.executeUpdate();
pstmt.close();
conn.close();
if(result==0){
out.println("对不起,增派失败!");
out.println("<a href='/wlxt/zprw.html'>重新输入</a>");
}else{
out.println("恭喜!输入成功!"+"<br>"+"<br>");
out.println("车牌号码:<br>"+request.getParameter("cphm")+"<br>"+"<br>");
out.println("司机姓名:<br>"+request.getParameter("sjxm").trim()+"<br>"+"<br>");
out.println("任务ID:<br>"+request.getParameter("rwid").trim()+"<br>"+"<br>");
out.println("委托人:<br>"+request.getParameter("wtr").trim()+"<br>"+"<br>");
out.println("目的地:<br>"+request.getParameter("mdd").trim()+"<br>"+"<br>");
out.println("<a href='/wlxt/ywgl.html'>返回主菜单</a>");
}
}else{
out.println("对不起,不存在此订单!");
out.println("<a href='/wlxt/zprw.html'>重新输入</a>");
}
} catch (SQLException e) {
System.out.println("连接数据库出错!");
e.printStackTrace();
}

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
改动前的代码:其他都一样 就是中间从try开始

try {
conn = DB.createConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("cphm").trim());
pstmt.setString(2, request.getParameter("sjxm").trim());
pstmt.setString(3, request.getParameter("rwid").trim());
pstmt.setString(4, request.getParameter("wtr").trim());
pstmt.setString(5, request.getParameter("mdd").trim());
pstmt.setString(6, "0");
result = pstmt.executeUpdate();
pstmt.close();
conn.close();
if(result==0){
out.println("对不起,增派失败!");
o