我是java web开发的新手。我正在编写这个基本的javaweb应用程序,它将把客户信息存储到数据库中。我使用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();
}
}
首先,我想指出您的代码对SQL注入是开放的。就我个人而言,我是过程的粉丝,所以我建议:您应该在mysql中创建一个过程来插入一个新记录,然后为该存储过程提供参数(名称、地址等)。
至于手头的问题:在statement.executeUpdate
线路之后,尝试关闭连接。此外,将Class.forName
提取到您的主方法中。它应该只执行一次。此外,一旦你这样做了,就把.newInstance()
从上面去掉
如果所有这些都不起作用,请将整个连接提取到全局可访问的静态变量,并且不要多次分配给它。然后看看你的程序是否有效。如果没有,你就有一个单独的问题。