gpt4 book ai didi

java - 从servlet向mysql数据库添加数据

转载 作者:行者123 更新时间:2023-11-28 23:57:47 25 4
gpt4 key购买 nike

我的目标是将数据从 servlet 插入数据库。这是我尝试过的:1) 创建了简单的 servlet,我在其中创建了 dbconnect,它将数据连接并插入 mysql 数据库。

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;

public class logic extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
dbconnect con = new dbconnect();
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet logic</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet logic at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <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>

}

2) 数据库连接类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;


public class dbconnect {
Connection conn;
Statement stm;

public dbconnect(){
try{
conn = DriverManager.getConnection("jdbc:mysql://villaikalto.ge/villaika_type?user=villaika_type&password=pass");
stm = conn.createStatement();
System.out.println("success");
String sql = "INSERT INTO users (mail, pass, gender, saxeli) VALUES (?, ?, ?, ?)";
System.out.println("1");
PreparedStatement statement = conn.prepareStatement(sql);
System.out.println("2");
statement.setString(1, "tsogiaidze");
statement.setString(2, "nika");
statement.setString(3, "nika");
statement.setString(4, "kaci");
System.out.println("3");
int rowsInserted = statement.executeUpdate();
System.out.println("4");
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}
}
catch (Exception ex){
System.out.println("error");
System.out.println(ex.getMessage());
}
}
}

问题是,当我使用简单的 java 类的主要方法创建 dbconnect 对象时,数据已成功写入数据库,但是当我尝试从 servlet 创建 dbconnect 对象时,它不会将数据添加到数据库中。我使用 netbeans 并将 mysql 驱动程序添加到类路径中。附言servlet由JSP文件调用,传递给servlet一些html表单参数。

我想我的问题很明确,希望看到您的评论。

最佳答案

谁对我的问题写了负面反馈?

如果这个问题很愚蠢或不合适,他为什么不能回答?

经过 7 个小时的思考和尝试,我终于找到了解决方案,我将把它写下来,以便将来有人也能遇到这个问题。

所以 dbconnect 类适用于简单 java 类而不适用于 servlet 的原因很简单。简单类会自动向 java 应用程序注册 jdbc 驱动程序(在本例中为 mysql 驱动程序),但 servlet 不会。因此我只需要编写开始连接数据库时的这行代码:

Class.forName("com.mysql.jdbc.Driver");

关于java - 从servlet向mysql数据库添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com