Xampp 服务器上的 Java Applet - 在 jdbc:mysql://localhost:3306/ 上找不到合适的驱动程序
在回答这个问题之前,我想说我在服务器设置和编程方面确实很陌生。
我制作了一个小程序,可以查看 MySQL 数据库中的表中的数据,并可以说它会输出数据。它比这复杂得多,但重点是当我运行该小程序时,该小程序在 Eclipse 中完美运行。我必须将 MySQL-connector-java-5.1.15-bin.jar 文件设置为它的外部库,但这样做后它运行得很好。
问题是每当我将代码放在 Xampp 服务器上时,它就无法正常工作。小程序启动,一切正常,直到小程序尝试使用以下代码连接到 MySQL 数据库(Xampp):
String connectionURL = "jdbc:mysql://127.0.0.1/";
Connection connection = null;
try{
connection = DriverManager.getConnection(connectionURL, "root", "mypassword");
}catch(Exception e){
System.out.println("Error: "+e);
}
在小程序中,我没有使用 System.out.println(),但我确实设法找出了我是得到的是:
java.SQL.Exception: No suitable driver found on jdbc:mysql://127.0.0.1/
我想知道我需要做什么来解决这个错误。我需要将 .jar 连接器放在哪里,或者我需要如何使用我的 Xampp 服务器配置它才能使其正常工作?
I'd like to preface this question by saying that I'm really new at server setup and programming.
I made an applet that views data from tables in a MySQL database and lets just say it outputs it. It's a lot more complicated than that but the point is that the applet runs perfectly in Eclipse when I run it. I had to set the MySQL-connector-java-5.1.15-bin.jar file as an external library for it, but after doing that it runs perfectly.
The problem is whenever I put the code on my Xampp server, it doesn't work right. The applet start and everything works until the applet tries to connect to the MySQL database (Xampp) using the following code:
String connectionURL = "jdbc:mysql://127.0.0.1/";
Connection connection = null;
try{
connection = DriverManager.getConnection(connectionURL, "root", "mypassword");
}catch(Exception e){
System.out.println("Error: "+e);
}
In the applet I didn't use System.out.println(), but I did manage to find out the I was getting was:
java.SQL.Exception: No suitable driver found on jdbc:mysql://127.0.0.1/
I'm wondering what I need to do to get around this error. Where do I need to put the .jar connector or how do I need to configure it with my Xampp server to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要构建 Web 应用程序,您需要编译代码并构建 WAR(Web 应用程序存档),该工件将部署到您的服务器、解压并放入您的 webapps 文件夹中。 WEB-INF 是项目中 Web 内容所在的源文件夹。
在 Eclipse 中,您应该有这个文件夹
src/main/WEB-INF
,您可以在其中放置 CSS(层叠样式表)、JavaScript 文件、图像、JSP 等。事实上,您已经添加了MySQL jar 作为外部依赖项意味着它不会添加到您要部署的 WAR 中。
对你的问题的简短回答是,你需要将 MySQL jar 放在你的 web 应用程序的类路径上,你可以通过将其放置在(例如)
tomcat/webapps//WEB-INF 中来完成此操作/lib
,然后重新启动tomcat(即如果您使用tomcat)。一开始这会是一个学习曲线,但我建议您学习如何使用 Maven。 Maven 将允许您使用依赖项,并构建适当的 WAR(拉取这些依赖项)。
使用 Maven 构建 Web 应用程序的指南。
To build a webapp, you would compile your code and build a WAR (Web Application Archive), which the artifact that would be deployed to your server, unpacked, and be put in your webapps folder. WEB-INF is the source folder where your web content goes in your project.
In eclipse, you should have this folder
src/main/WEB-INF
, where you could place CSS (Cascading Style Sheets), JavaScript files, images, JSPs, etc.The fact that you have added the MySQL jar as an external dependency means that it would not be added to the WAR that you would be deploying.
The short answer to your question is that you need that MySQL jar to be on the classpath of your webapp, which you would do by placing it in (e.g.)
tomcat/webapps/<your project name>/WEB-INF/lib
, then restart tomcat (that is, if you use tomcat).This will be a bit of a learning curve at first, but I would suggest you learn how to use Maven. Maven will allow you to consume dependencies, and build a proper WAR (pulling in these dependencies).
Guide to building a webapp with Maven.
我修好了!
我所要做的就是:
在我想要显示小程序的 html 文件中。
I fixed it!
All I had to do was this:
in the html file that I wanted to display the applet in.