Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JDBC:MySql连接在Ajax请求-Java中不起作用_Java_Mysql_Servlets_Jdbc_Sqlexception - Fatal编程技术网

JDBC:MySql连接在Ajax请求-Java中不起作用

JDBC:MySql连接在Ajax请求-Java中不起作用,java,mysql,servlets,jdbc,sqlexception,Java,Mysql,Servlets,Jdbc,Sqlexception,我在主方法和其他方法中都有完全相同的函数,JDBC连接工作正常。如果调用另一个函数,它将抛出错误java.sql.SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/wine: 我已经在库[Netbeans]中包含了MySql驱动程序 处理请求方法: protected void processRequest(HttpServletRequest request, HttpServletResponse response) thr

我在主方法和其他方法中都有完全相同的函数,JDBC连接工作正常。如果调用另一个函数,它将抛出错误java.sql.SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/wine

我已经在库[Netbeans]中包含了MySql驱动程序

处理请求方法:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String process = (String) request.getParameter("process");
    String name = (String) request.getParameter("process");
    String phone=(String) request.getParameter("phone");
    String email = (String) request.getParameter("email");
    String pwd = (String) request.getParameter("pwd");
    PrintWriter out = response.getWriter();
    out.println("Hello");
   signup(out,process,name,email,phone,pwd);

}
private static int signup(PrintWriter out,String process,String name,String email,String phone,String pwd){
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");


        out.println("Process Not Found");
        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role) VALUES(?,?,?,?,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        out.println(e);
        e.printStackTrace();
        return 0;
    }
    return 1;
}
public static void main(String[] args) {

    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String process = "signup";
    String name = "Test";
    String phone="45885";
    String email = "Test@gmail.com";
    String pwd = "dkjsdh";

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");



        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role,status) VALUES(?,?,?,?,1,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            System.out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        System.out.println(e);
        e.printStackTrace();
    }
}
注册方法:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String process = (String) request.getParameter("process");
    String name = (String) request.getParameter("process");
    String phone=(String) request.getParameter("phone");
    String email = (String) request.getParameter("email");
    String pwd = (String) request.getParameter("pwd");
    PrintWriter out = response.getWriter();
    out.println("Hello");
   signup(out,process,name,email,phone,pwd);

}
private static int signup(PrintWriter out,String process,String name,String email,String phone,String pwd){
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");


        out.println("Process Not Found");
        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role) VALUES(?,?,?,?,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        out.println(e);
        e.printStackTrace();
        return 0;
    }
    return 1;
}
public static void main(String[] args) {

    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String process = "signup";
    String name = "Test";
    String phone="45885";
    String email = "Test@gmail.com";
    String pwd = "dkjsdh";

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");



        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role,status) VALUES(?,?,?,?,1,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            System.out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        System.out.println(e);
        e.printStackTrace();
    }
}
主要方法:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String process = (String) request.getParameter("process");
    String name = (String) request.getParameter("process");
    String phone=(String) request.getParameter("phone");
    String email = (String) request.getParameter("email");
    String pwd = (String) request.getParameter("pwd");
    PrintWriter out = response.getWriter();
    out.println("Hello");
   signup(out,process,name,email,phone,pwd);

}
private static int signup(PrintWriter out,String process,String name,String email,String phone,String pwd){
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");


        out.println("Process Not Found");
        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role) VALUES(?,?,?,?,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        out.println(e);
        e.printStackTrace();
        return 0;
    }
    return 1;
}
public static void main(String[] args) {

    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String process = "signup";
    String name = "Test";
    String phone="45885";
    String email = "Test@gmail.com";
    String pwd = "dkjsdh";

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");



        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role,status) VALUES(?,?,?,?,1,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            System.out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        System.out.println(e);
        e.printStackTrace();
    }
}

有两个选项可以尝试:

Class.forName("com.mysql.jdbc.Driver").newInstance() //older bug

在评论中,当我打算粘贴第二个bug解决方案时,我粘贴了旧的bug解决方案


无论哪种方式,我都很高兴它适合您

这行“Class.forName(“com.mysql.jdbc.Driver”).newInstance();”怎么样?你试过以防万一吗?请看。修复后,让我们知道还有什么问题。@efekActive
newInstance()
完全没有必要(除非您使用的连接器/J在+/-15年前有一个bug)。我指的是没有newInstance()的类.forName(…)。SorryIt从main开始工作,因为驱动程序位于初始类路径上,当驱动程序是web应用程序部署的一部分(而不是在tomcat lib文件夹中)时,
DriverManager将自动检测并加载该驱动程序,然后自动加载驱动程序不起作用,因为类加载器层次结构限制了驱动程序的可见性,因此
DriverManager
无法自动加载它。也就是说:在web应用程序中,通常不使用
DriverManager
创建连接,而是使用
DataSource
(通常使用连接池)。