gpt4 book ai didi

java - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO) when ToGet url have db parameter

转载 作者:行者123 更新时间:2023-11-29 23:28:44 26 4
gpt4 key购买 nike

我搜索了论坛,但没有任何东西可以帮助我解决我的问题。我需要通过 doGet mether 从 URL 获取数据库参数“db”我认为发生这种情况是因为程序不会从 ULR 读取我的密码(是的,传递到 URL 是个坏主意,但这正是本练习所需要的。

每次发生这种情况时:

Access denied for user 'root'@'localhost' (using password: NO) 

当我尝试使用以下网址启动我的服务时,会出现此问题:

http://localhost:8080/Pl/?db=jdbc:mysql://localhost:3307/baza?user=root&password=krasnal

有人知道这是问题所在吗?也许 jdbc:mysql://localhost:3307/baza?user=root&password=krasnal 出了问题?我检查了一下,这是一个好的密码。

这是我的错误页面:

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
Plain.getLargestRegionArea(Pl.java:303)
Pl.baza(Pl.java:113)
Pl.doGet(Pl.java:101)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.

我不需要打开它在命令提示符中打开,只需要它在 Eclipse 中工作即可。我的代码还有一些:

public class Pl extends HttpServlet {
private static final long serialVersionUID = 1L;


private static final String QUERY = "SELECT x, y, z FROM baza";

/**
* Method connecting to database and gets the data needed to perform the
* Plain algorithm
*
* @param connector
* @return The Map with coordinates in 2D space with key which is result of
* function z = f(x,y).
*/
public static Map<Float, List<Point2D.Float>> dbconnection(
final String connector) {

Map<Float, List<Point2D.Float>> coordinates = null;

try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
// driver not found
}

try {

Connection conn = DriverManager.getConnection(connector);
Statement statement = conn.createStatement();

coordinates = retriveData(statement.executeQuery(QUERY));

statement.close();
conn.close();

} catch (SQLException e) {
e.printStackTrace();
}
return coordinates;
}

/**
* Main method. Executing and solving the problem. Should have only one
* parameter: -connection_string .
*
* @param args
* connection_string
* @return
*/

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String parametr = request.getParameter("db"); // pobranie parametru z
// adresu url

String k="jdbc:mysql://localhost/baza?user=root&password=krasnal";
String w=parametr;
Locale fmtLocale = Locale.getDefault(Category.FORMAT);
NumberFormat formatter = NumberFormat.getInstance(fmtLocale);
formatter.setMaximumFractionDigits(5);
formatter.setMinimumFractionDigits(5);


response.getWriter().write("Wynik to: "+formatter.format(baza(w))+parametr);
//response.getWriter().write(int.format(Locale.US, "Koszt : %.3f", wynik));
}




public static double baza(String k){
//String par ="jdbc:mysql://localhost/baza?user=root&password=krasnal";
// if (args.length == 1) {
Map<Float, List<Point2D.Float>> coordinates = dbconnection(k);
Plain plain = new Plain(coordinates);
double wynik=plain.getLargestRegionArea();
System.out.format(String.format(Locale.US, "Koszt : %.3f", wynik));

return wynik;
}

/**
* This method gets data from {@link ResultSet} to list contains points in
* 2D space and result of function z = f(x,y).
*
* @param result
* The set with fetch data
* @return The HasjMap with coordinates in 2D space with key which is result
* of function z = f(x,y).
* @throws SQLException
*/


private static Map<Float, List<Point2D.Float>> retriveData(
final ResultSet result) throws SQLException {
Map<Float, List<Point2D.Float>> coordinates = new HashMap<Float, List<Point2D.Float>>();

while (result.next()) {
Point2D.Float point = new Point2D.Float(result.getFloat("x"),
result.getFloat("y"));

if (coordinates.containsKey(result.getFloat("z"))) {
coordinates.get(result.getFloat("z")).add(point);
} else {
List<Point2D.Float> points = new LinkedList<Point2D.Float>();
points.add(point);
coordinates.put(result.getFloat("z"), points);

}
}

return coordinates;
}
}

最佳答案

http://localhost:8080/Pl/?db=jdbc:mysql://localhost:3307/baza?user=root&password=krasnal

password 参数被解释为 servlet 的第二个参数,而不是 db 参数的一部分。本质上,您的 servlet 接收的参数是:

 db       - jdbc:mysql://localhost:3307/baza?user=root
password - krasnal

将数据库密码作为 URL 参数发送到 servlet 是一个极其糟糕的主意。 不要这样做。

话虽这么说,转义 db 参数内容的 URL 可能会导致其被正确解析,例如

?db=jdbc%3Amysql%3A//localhost%3A3307/baza%3Fuser%3Droot%26password%3Dpleasehackme

我对 Java 有点生疏,所以我不确定这是否能“按原样”工作。您可能需要对 servlet 中的参数进行转义才能使用它。

关于java - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO) when ToGet url have db parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26742936/

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