2013-07-22 61 views
0

我是新到web開發到Java。我正在編寫這個基本的Java Web應用程序,它將把客戶信息存儲到數據庫中。我使用MVC-2架構。我的Jsp發送一個請求到一個servlet,它反過來試圖安裝一個bean並將該對象插入到數據庫中。 當我嘗試連接到數據庫(在調試模式下)連接變量返回空。所以數據不能被插入。無法連接到NetBeans Java Web應用程序中的mysql數據庫

這是說對數據庫連接類

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package customer; 

import java.io.Serializable; 
import java.sql.Connection; 
import java.sql.DriverManager; 

/** 
* 
* @author 
*/ 
public class DatabaseOperations implements Serializable 
{ 
    private static Connection connection; 
    public DatabaseOperations() 
    { 
     try 
     { 
      String username = "root"; 
      String password = "root"; 
      String url = "jdbc:mysql://localhost/test"; 
      Class.forName ("com.mysql.jdbc.Driver").newInstance(); 
      connection = DriverManager.getConnection (url, username, password); 

      System.out.println("Database connection established"); 
     } 
     catch(Exception e) 
     { 

     } 
    } 

    public static Connection getConnection() 
    { 
     return connection; 
    } 
} 

這是增加了客戶對數據庫的方法

public void addCustomer(CustomerBean customer) throws SQLException { 
     DatabaseOperations db = new DatabaseOperations(); 
     connection = DatabaseOperations.getConnection(); 
     statement = connection.createStatement(); 

     String query = "insert into customer (name, address, phone, email) " 
       + "values (" + customer.name + "," 
       + customer.address + "," 
       + customer.phone + "," 
       + customer.email + "," + ")"; 

     statement.executeUpdate(query); 
    } 

,最終這就是我所說的方法,該servlet添加一個客戶

/* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package customer; 

    import java.io.IOException; 
    import java.io.PrintWriter; 
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import customer.CustomerBean; 
    import javax.servlet.RequestDispatcher; 

    /** 
    * 
    * @author 
    */ 
    public class CustomerServlet extends HttpServlet { 

     /** 
     * Processes requests for both HTTP 
     * <code>GET</code> and 
     * <code>POST</code> methods. 
     * 
     * @param request servlet request 
     * @param response servlet response 
     * @throws ServletException if a servlet-specific error occurs 
     * @throws IOException if an I/O error occurs 
     */ 
     protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
       throws ServletException, IOException { 
      response.setContentType("text/html;charset=UTF-8"); 
      PrintWriter out = response.getWriter(); 

      CustomerBean customer = new CustomerBean(); 
      try { 
       out.println("tests"); 

       customer.setName(request.getParameter("name")); 
       customer.setEmail(request.getParameter("email")); 
       customer.setAddress(request.getParameter("address")); 
       customer.setPhone(request.getParameter("phone")); 

/************** ADD CUSTOMER TO DB HERE***********************/ 
       customer.addCustomer(customer); 

       request.setAttribute("cust", customer); 
       request.getRequestDispatcher("/index.jsp").forward(request, response); 
      } 
      catch (Exception e) 
      {    
       e.printStackTrace(); 

      } 
     } 
+0

另外我想指定我的netbeans IDE連接到mysql數據庫,因爲我可以創建表並從netbeans中的服務選項卡插入數據。 – mbass

+0

您有問題嗎?或者你只是提交一份關於你當前開發工作的狀態報告?是否所有的代碼都與問題相關,或者您認爲它可能會縮小一點,例如,實際獲得數據庫連接的位置。有沒有辦法來測試這是否成功。是否顯示異常或錯誤消息,或者這只是「不能正常工作」。也許問題在於調用'getConnection'方法。這是一個靜態方法,還是一個實例方法? (你是否應該調用'db'的getConnection方法)? – spencer7593

+0

我有問題。所有的代碼都與問題有關。 get連接方法是靜態的。我的問題在於,每當我安裝數據庫操作時,我都希望建立到數據庫的連接,並且我希望連接位於連接實例變量中。但它不在那裏。當我運行調試器時,它跳過語句「connection = DriverManager.getConnection(url,username,password);」我想這是錯誤的來源 – mbass

回答

0

首先,我想指出你的代碼是開放給SQL注入的撓度。就我個人而言,我是程序迷,所以我會建議:你應該在mysql中創建一個過程來插入一條新記錄,然後用參數(名稱,地址等)提供該sproc。

至於手頭的問題:在您的statement.executeUpdate行後,嘗試關閉連接。另外,將Class.forName提取到您的主要方法。它應該只執行一次。此外,一旦你這樣做,刪除.newInstance()

如果所有這些都不起作用,請將整個連接提取到全局可訪問的靜態變量,並且不要多次分配給它。看看你的程序是否有效。如果不是,你有一個單獨的問題。

相關問題