【问题标题】:Unable to get connection to mysql database in netbeans java web application无法在 netbeans java Web 应用程序中连接到 mysql 数据库
【发布时间】:2013-07-22 21:03:01
【问题描述】:

我是 Java Web 开发的新手。我正在编写这个基本的 Java Web 应用程序,它将客户信息存储到数据库中。我使用 MVC-2 架构。我的 Jsp 向 servlet 发送一个请求,该 servlet 反过来尝试实例化一个 bean 并将该对象插入数据库。 当我尝试连接到数据库(在调试模式下)时,连接变量返回空。所以无法插入数据。

这是与数据库建立连接的类

/*
 * 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();

            }
        }

【问题讨论】:

  • 另外我想指定我的 netbeans IDE 可以很好地连接到 mysql 数据库,因为我可以从 netbeans 的服务选项卡创建表并插入数据。
  • 您有问题吗?还是您只是提供有关您当前开发工作的状态报告?是否所有这些代码都与问题相关,或者您认为它可能会缩小一点,例如您实际获得数据库连接的位置。有没有办法测试是否成功。是否显示异常或错误消息,或者这只是“不起作用”。也许问题在于对getConnection 方法的调用。那是静态方法还是实例方法? (是不是应该调用db的getConnection方法)?
  • 我有问题。所有代码都与问题相关。 get 连接方法是静态的。我的问题是,每当我启动数据库操作时,我都希望与数据库建立连接,并且我希望该连接位于连接实例变量中。但它不存在。当我运行调试器时,它会跳过语句“connection = DriverManager.getConnection (url, username, password);”我想这就是错误的来源

标签: mysql servlets netbeans


【解决方案1】:

首先,我想指出您的代码对 SQL 注入是开放的。就个人而言,我是过程的粉丝,所以我建议:你应该在 mysql 中创建一个过程来插入一条新记录,然后为该 sproc 提供参数(名称、地址等)。

至于手头的问题:在您的statement.executeUpdate 行之后,尝试关闭连接。另外,将Class.forName 提取到您的主要方法中。它应该只执行一次。此外,一旦你这样做了,请删除 .newInstance()

如果所有这些都不起作用,请将整个连接提取到一个全局可访问的静态变量中并且不要多次分配给它。看看你的程序是否有效。如果没有,你有一个单独的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2017-05-22
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多