gpt4 book ai didi

java.sql.SQLException : No suitable driver found for url+dbName, UserId=?&password=?

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:17 25 4
gpt4 key购买 nike

我正在尝试将 mysql 数据库与 java (eclipse) 连接这是我的代码

Controller.java

package classes;



import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.ResultSet;
import java.sql.DriverManager;

import java.sql.SQLException;


public class Controller{

public static boolean validate(String userId, String Pass){
boolean status = false;
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost/";
String dbName = "meeting_planner";
String driver = "com.mysql.jdbc.Driver";
String UserId = "123";
String password = "hhh";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection("url+dbName,UserId=?&password=?");
pst = con.prepareStatement("select * from administrator where AId=? and Password=?");
pst.setString(1,userId);
pst.setString(2, Pass);
rs = pst.executeQuery();
status = rs.next();
}
catch(Exception e){
System.out.println(e);
} finally {
if(con!=null){
try {
con.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

}
return status;
}
}

AND administrator.java 是

package classes;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class administrator extends HttpServlet {

private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String i = request.getParameter("Userid");
String p = request.getParameter("userpassword");
HttpSession session = request.getSession(false);
if(session!=null)
session.setAttribute("Administrator Id",i);
if(Controller.validate(i,p)){
RequestDispatcher rd = request.getRequestDispatcher("creat_date.jsp");
rd.forward(request, response);
}
else {
out.print("<p style=\"color:red\">Sorry User id or Password error!</p>");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}


}
}

mysql数据库名称是meeting_planner表名:管理员和属性名称是 AId、密码、名称、电子邮件index.jsp 是一个登录页面,它获取属性 AId 和 Password 的值并验证来自数据库的数据并将控件转发给 creat_date.jsp

最佳答案

改变

con = DriverManager.getConnection("url+dbName,UserId=?&password=?");

con = DriverManager.getConnection(url+dbName, UserId, password);

您应该使用驼峰式命名您的变量。 http://en.wikipedia.org/wiki/CamelCase

关于java.sql.SQLException : No suitable driver found for url+dbName, UserId=?&password=?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26453894/

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