我正在尝试通过以下方式对数据库进行插入:
package org.gradle.demo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
@WebServlet(name = "HelloServlet", urlPatterns = {"register"}, loadOnStartup = 1)
public class RegisterServlet extends HttpServlet {
private Connection conn;
protected void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
conn = DBUtils.initializeSqlConnection();
Statement statement = conn.createStatement();
int count = statement.executeUpdate("INSERT INTO users (username, pass) VALUES ('" + username + "', '" + password + "')");
System.out.println(count);
} catch (SQLException e) {
System.err.println("SQL Exception trying to insert new user");
System.err.println(e.getMessage());
}
request.setAttribute("username", username);
request.getRequestDispatcher("loginsucces.jsp").forward(request, response);
}
}
我正在使用具有这种依赖关系的Gradle:
dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
testCompile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '2.2.3'
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.13.0'
}
我也已经在另一个执行SELECT的类中测试了我的连接,它可以完美连接,但是一旦我尝试进行INSERT,它就会抛出:
No suitable driver found for jdbc:postgresql://localhost/training_log