mysql - 如何修复“ DriverManager.getConnection(connectionURL, UN, PW); ”上的错误?
问题描述
我是 MYSQL 和 JSP 的新手。我正在尝试检查 mysql 数据库和 jsp 之间的连接性。当我在服务器上运行项目时,它会引发异常。我在代码上找不到错误。
我在mysql和UN上创建了oop数据库,密码也是正确的。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Connection status </h1>
<%
/* Create string of connection url within specified format with machine name,
port number and database name. Here machine name id localhost and
database name is oop. */
String connectionURL = "jdbc:mysql://localhost:3306/oop";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructors();
/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "raviya");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed()){
%>
<font size="+3" color="green">
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
else{
%>
</font>
<font size="+3" color="red">
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>
错误: 在此处输入图像描述
解决方案
如果您不导入 mysql 连接器 jar 文件,则可能会发生这些类型的错误。因此,您可以下载与您的版本相关的 jar 文件,然后将其粘贴到您自己的项目 /WEB-INF/lib 文件夹中。
推荐阅读
- c++ - C++,无法得到逻辑
- java - 互联网连接更改时自动重新加载 webview
- java - RxJava/RxAndroid 中的 PublishSubject 导致崩溃 onError 并跳过 onNext
- python - 使用 findall() 和 search() 的奇怪正则表达式问题
- acumatica - 库存转移和收据中的 Javascript 问题
- solr - 调整 DIH Apache Solr
- android - Android Studio 不显示带有 Flutter 的 Logcat
- sql-server - 什么是数字的最佳数据
- linux - 如何在 linux 环境下安装 pyhs2?
- c - 从文件中读取内容时出现分段错误