我的目标是从servlet将数据插入数据库。 在这里我尝试过: 1)创建了一个简单的servlet,我在其中创建了连接数据并将数据插入mysql数据库的dbconnect。
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)dbconnect类:
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文件调用,它将一些html表单参数传递给servlet。
我想我的问题很明确,希望看到你的意见。
答案 0 :(得分:1)
谁在我的问题上写了负面反馈?
如果这个问题愚蠢或不合适,为什么他不能回答呢?
经过7个小时的思考和尝试后,我找到了解决方案,我会写下来,以便将来有人也能解决这个问题。
因此dbconnect类适用于简单的java类而不是servlet的原因是simple.Simple类自动注册jdbc驱动程序(在本例中为mysql驱动程序)与java应用程序但servlet没有。因此我只需要编写在开始连接到数据库的这一行代码:
Class.forName("com.mysql.jdbc.Driver");
答案 1 :(得分:0)
您好我的代码工作正常我尝试了这个
public class Dbconnect {
Connection conn;
public Dbconnect(){
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "welcome");
String sql = "INSERT INTO student(student_id,student_name,class) VALUES(?,?,?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, 7);
statement.setString(2, "nika1");
statement.setString(3, "nika");
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
servlet-class:
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();
out.println("Inserted..");
}
catch (Exception e)
{}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
在web.xml中:
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>main.myPack.Logic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/logic</url-pattern>
</servlet-mapping>