简体   繁体   English

尝试将 Java 与数据库连接时出错

[英]Error while trying to connect Java with database

I have class database where I tried to create database, tables and some function on tables.我有类数据库,我试图在其中创建数据库、表和表上的一些函数。

This is part of main code in databasle class: (if you need something more, Im here!).这是数据库类中主要代码的一部分:(如果您需要更多内容,我在这里!)。

public class db {

public static db database = new db();

private static Connection conn = null;
private static String url = "jdbc:mysql://localhost:3307";
private static String db_name = "csdz11";
private static String db_user = "root";
private static String db_pass = "";

This is error: (If you need whole message I will update answer, but I hope this will be enough for this case.)这是错误:(如果您需要完整的消息,我会更新答案,但我希望这对于这种情况就足够了。)

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/csdz11
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at util.db.openConn(db.java:55)
at util.db.prikazBaze(db.java:160)
at zadatak1.Zadatak1.start(Zadatak1.java:69)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Exception in Application start method

However, I made application, JavaFX inputs, but got that error while trying to run it, so, can someone help me?但是,我制作了应用程序,JavaFX 输入,但是在尝试运行它时遇到了该错误,所以,有人可以帮助我吗?

I recommend using the static method within DriverManager to determine what drivers are registered with the runtime.我建议使用 DriverManager 中的静态方法来确定向运行时注册了哪些驱动程序。 Your runtime (outside of your IDE) may not have visibility/access to the MySQL driver.您的运行时(在 IDE 之外)可能无法查看/访问 MySQL 驱动程序。

DriverManager.getDrivers() returns an Enumeration, which you can iterate through (a print statement or log file entry) the properties of each that are available. DriverManager.getDrivers() 返回一个枚举,您可以遍历(打印语句或日志文件条目)每个可用的属性。 If it's not there, work upstream to make sure it's installed.如果它不存在,请在上游工作以确保它已安装。

public class DBDrivers {
public static void main(String[] args) {    
    DriverManager.drivers().forEach(driver -> System.out.println(driver));}

This error suggests that the MySQL-connector jar is not in your classpath.此错误表明 MySQL 连接器 jar 不在您的类路径中。 Include the jar包括罐子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM