我在 servlet 中有一个更新查询。语法是正确的,但是当我执行查询时,什么也没有发生。执行保持冻结状态,命令永远不会结束。几分钟后,消息“广告:GRIZZLY0023:中断空闲线程:http-thread-pool-8080(5)”。每隔几秒打印一次
我尝试过:
con.stmt.executeUpdate ("SQL");
con.rset = con.stmt.executeQuery ("SQL");
并使用 PreparedStatement。
这是查询:
System.out.println(""
+ " UPDATE JSP_TABLE SET "
+ " field1 = '" + request.getParameter("input1") + "',"
+ " field2= '" + request.getParameter("input2") + "',"
+ " field3= '" + request.getParameter("input3") + "',"
+ " field4= '" + request.getParameter("input4") + "',"
+ " field5= '" + request.getParameter("input5") + "',"
+ " field6= '" + request.getParameter("input6") + "',"
+ " field7= '" + request.getParameter("input7") + "',"
+ " field8= '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " field9= '" + request.getParameter("input") + " al " + request.getParameter("hasta9") + "'"
+ " WHERE ID_PK = " + request.getParameter("inputPK"));
我所有的语句、结果集和连接在使用结束时都会关闭。我有第二次更新,效果很好。
package Funciones;
import conexion.conectar;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActualizarDinamica extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
conectar con = new conectar();
try {
/*
* TODO output your page here. You may use following sample code.
*/
con.stmt = con.conn.createStatement();
String indicador = "";
switch (Integer.parseInt(request.getParameter("indicador"))) {
case 1:
indicador = "Nivel de Servicio";
break;
case 2:
indicador = "Venta";
break;
case 3:
indicador = request.getParameter("txt_otro");
break;
case 0:
indicador = "Presencia";
break;
};
/* way 1
* con.rset = con.stmt.executeQuery(""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID"));
con.rset = con.stmt.executeQuery(""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID"));
*/
/* way 2
con.stmt.executeUpdate(""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID"));
con.stmt.executeUpdate(""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID"));
*/
//way 3
PreparedStatement updateSmallQuery = null;
PreparedStatement updateBigQuery = null;
String bigQuery=
""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID");
String smallQuery= ""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID");
try {
con.conn.setAutoCommit(false);
updateSmallQuery = con.conn.prepareStatement(smallQuery);
updateBigQuery = con.conn.prepareStatement(bigQuery);
updateBigQuery.executeUpdate();
updateSmallQuery.executeUpdate();
con.conn.commit();
} catch (SQLException e) {
System.out.println("catch!");
System.out.println(e.getMessage());
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.conn.rollback();
} catch (SQLException excep) {
}
}
} finally {
if (updateSmallQuery != null) {
updateSmallQuery.close();
}
if (updateBigQuery != null) {
updateBigQuery .close();
}
con.conn.setAutoCommit(true);
}
response.sendRedirect("sol_env_c.jsp");
} finally {
con.conn.commit();
con.conn.close();
con.stmt.close();
out.close();
}
}
}
希望这有助于找到答案。PD:小的运行没有问题。大的就是问题所在。
编辑:
这是 conectar()
public conectar() {
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Oracle JDBC driver loaded ok.");
conn = DriverManager.getConnection(params, user, password);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}