我把一些带有JSP表单的代码插入到JDBC中。 我在编码方面相对较新,但经过长时间的互联网搜索,我仍然无法解决问题。 我需要插入'数据人员在我的jsp表单中输入的表格。
JSP:
<form action="CampaignToJDBC" method="post">
Enter company name: <input type="text" name="pname" /><br/>
Enter company email: <input type="email" name="pemail" /><br/>
<input type="submit" value="Apply" />
</form>
Servlet:
Connection con = null;
PreparedStatement ps = null;
// ____________________________________________
String pname = request.getParameter("pname");
String pemail = request.getParameter("pemail");
Class.forName(driver);
con = DriverManager.getConnection(URL);
String Query = "insert into campaigns(pname, pemail) values(?, ?)";
ps = con.prepareStatement(Query);
ps.setString(1, pname);
ps.setString(2, pemail);
ps.executeUpdate();
SQL:
DROP TABLE campaigns;
CREATE TABLE campaigns(
Pname varchar2(32),
Pemail varchar2(32)
);
我的问题是它没有将它插入数据库,并且凭借我新获得的经验,我不知道为什么。我查了几个例子,但它们看起来都像我的。
FULL SERVLET:
@WebServlet(name = "CampaignToJDBC", urlPatterns = {"/CampaignToJDBC"})
public class CampaignToJDBC extends HttpServlet {
public static String driver = "oracle.jdbc.driver.OracleDriver";
public static String URL = "jdbc:oracle:thin:@datdb.cphbusiness.dk:1521:dat";
public static String ID = "";
public static String PW = "";
private int id = 0;
Connection con = null;
PreparedStatement ps = null;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pname = request.getParameter("pname");
String pemail = request.getParameter("pemail");
try {
Class.forName(driver);
con = DriverManager.getConnection(URL, ID, PW);
String Query = "insert into campaigns(Pname, Pemail) values(?, ?)";
ps = con.prepareStatement(Query);
ps.setString(1, pname);
ps.setString(2, pemail);
ps.executeUpdate();
id = id + 1;
} catch (Exception e) {
} finally {
try {
ps.close();
} catch (SQLException ex) {
Logger.getLogger(CampaignToJDBC.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(CampaignToJDBC.class.getName()).log(Level.SEVERE, null, ex);
}
}
RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:0)
请将您的业务逻辑与JSP和Servlet端分开。为逻辑创建一个单独的类,让我们说经理/控制器或其他什么。在那里实现逻辑并从控制台或UnitTest尝试。
如果工作正常,请从Servlet调用该方法。 首先,尝试使用常量参数。 请参阅日志文件,生成日志信息或使用System.out.println();看看发生了什么。在日志中搜索例外。
SELECT查询是否适用于Servet?意味着连接很好。 试着靠近。
答案 1 :(得分:0)
问题已解决。
代码是正确的,但我忘了添加我需要的驱动程序。所以我所做的只是添加带驱动程序的库,它工作正常。对不起,我的坏!
感谢大家的帮助:)