Servlet未连接到数据库

时间:2015-01-02 13:23:58

标签: mysql

我正在使用java开发一个Web应用程序。一切都在本地工作正常,但是当部署到远程系统时,我的jsp页面重定向到servlet,而servlet中的代码不工作 - 它显示一个空白页面。

我正在附加我的jsp和servlet的代码。有人可以帮忙吗?

AddCustdummy.jsp

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <form id="theForm" name="form1" method="post" action="SelectCustServlet"  enctype="multipart/form-data" target="_self" >
        <%
        String status=request.getParameter("status");
        if(status!=null) 
        //  out.println(status);
        System.out.println("the status   "+status);
        %>
        <% if(status!=null)
        {
        %>
        <dl>
        <dt><label for=""><%=status %></label></dt></dl>
        <%} %>

    <span>

    <strong> 
    <font size="3">
    <span id="error" style="color: Green; display: none">*Accepts Only Numbers</span>
    <span id="error6" style="color: Green; display: none">*Accepts Only Characters</span>
    </font> 
    </strong>
    </span>
    <fieldset class="login"><br>
    <legend>Customer Details - Fill Customer Details</legend>
        <div>
        <label for="Ac_No">Account Number</label> 
        <input type="text" id="Ac_No" name="Ac_No" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" />
        </div>

        <div>
        <label for="Center_No">Center Number</label> <input type="text" id="Center_No" autocomplete="off" class="input username" name="Center_No"  onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"   />
        </div>

        <div>
        <label for="NOM">Customer Name </label> <input type="text" class="input username" autocomplete="off" id="NOM" name="NOM" onkeypress="return character();" ondrop="return false;" onpaste="return false;" />
        </div>



</fieldset><br>
        <center>
        <button type="submit" >Submit</button>
        </center>


    </form>
</body>
</html>

- SelectCustServlet.java

package com.mayuri.servlet;

import java.io.IOException;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(
		description = "SelectCustServlet", 
		urlPatterns = { "/SelectCustServlet" }) 
@MultipartConfig(maxFileSize = 10177215) 
public class SelectCustServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
   
    public SelectCustServlet() {
        super();
    }

    private String dbURL = "jdbc:mysql://XXX.XX.XXX.XX:XXXX/ABC";
    private String dbUser = "abc";
    private String dbPass = "abc";
     
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    	 Connection conn = null; 
    	 
    	 String message = null;  
    	try{
    	
    		
    	        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    	        conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
    	        
    	
        String Ac_No = request.getParameter("Ac_No");
        System.out.println("in Servlettttttttttttttttttttttttt"+Ac_No);
        
        Statement statement1 = conn.createStatement();
		 ResultSet resultset = 
statement1.executeQuery("SELECT Ac_No FROM cust_name1 WHERE close_date <= CAST(CURRENT_TIMESTAMP AS DATE) AND Ac_No="+Ac_No); 
	 
        
		 if(resultset.next()){
			 response.sendRedirect("closeaccountmsg.jsp?Ac_No="+Ac_No);
         }else{
        
        
       
    
       
             String Center_No = request.getParameter("Center_No");
             String NOM = request.getParameter("NOM");
                        
       
      
       
        try {
           
            String sql = "INSERT INTO cust_name1 (Ac_No,Center_No,NOM) values (?,?,?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            
            statement.setString(1, Ac_No);
            statement.setString(2, Center_No);
            statement.setString(3, NOM);
          
            int row = statement.executeUpdate();
            if (row > 0) {
//              
            }
            
            response.sendRedirect("Message.jsp?Ac_No="+Ac_No);
            
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } } } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
             
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
 


          
        }
    }
    	}

在提交jsp中的字段之后,它会进入一个servlet,它应该将字段插入到数据库中,但它不会发生 - 但不要忘记它在本地工作正常。

0 个答案:

没有答案