Java 如何测试JDBC驱动是否安装正确,是否可以连接数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4215742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:39:58  来源:igfitidea点击:

How to test if JDBC driver is installed correctly and if the DB can be connected?

javamysqljdbc

提问by James Rattray

I tried installing this at home along with Java SDK... The SDK worked fine and I can now use the command prompt to compile java programs into classes...

我尝试在家里将它与 Java SDK 一起安装... SDK 运行良好,我现在可以使用命令提示符将 Java 程序编译为类...

However I am unsure how to test if JDBC is working to connect to my server/databases/mysql.

但是我不确定如何测试 JDBC 是否可以连接到我的服务器/数据库/mysql。

As I have a feeling my server (Which is a shared website/webhost) may not allow the connection...

因为我感觉我的服务器(这是一个共享网站/网络主机)可能不允许连接......

How can I test that the JDBC was installed correctly without having to connect to a server?

如何在无需连接到服务器的情况下测试 JDBC 是否正确安装?

And then how can I test (Seperate code please) that the (now confirmed working) JDBC is connecting to my databases?

然后我如何测试(请使用单独的代码)(现在已确认工作)JDBC 是否连接到我的数据库?

Thanks alot.

非常感谢。

采纳答案by BalusC

How can I test that the JDBC was installed correctly without having to connect to a server?

如何在无需连接到服务器的情况下测试 JDBC 是否正确安装?

Just check if Class#forName()on the JDBC driver doesn't throw ClassNotFoundException.

只需检查Class#forName()JDBC 驱动程序是否不抛出ClassNotFoundException.

try {
    Class.forName(driverClassName);
    // Success.
}
catch (ClassNotFoundException e) {
    // Fail.
}


And then how can I test (Seperate code please) that the (now confirmed working) JDBC is connecting to my databases?

然后我如何测试(请使用单独的代码)(现在已确认工作)JDBC 是否连接到我的数据库?

Just check if DriverManager#getConnection()or DataSource#getConnection()doesn't throw SQLException.

只是检查,如果DriverManager#getConnection()还是DataSource#getConnection()不扔SQLException

try (Connection connection = DriverManager.getConnection(url, username, password)) {
    // Success.
}
catch (SQLException e) {
    // Fail.
}

See also

也可以看看

回答by Tom Anderson

Install a database locally and connect to that.

在本地安装数据库并连接到该数据库。

You can check the connection to the server by using telnet to connect to the port where the database should be listening; if you can open a connection, you know there isn't a network problem (such as a firewall). If JDBC is okay and the network is okay, then you can try connecting to the remote system. If that doesn't work, it's some kind of configuration problem.

可以通过telnet连接数据库应该监听的端口来检查服务器的连接情况;如果您可以打开连接,您就知道没有网络问题(例如防火墙)。如果JDBC没有问题,网络也没有问题,那么你可以尝试连接到远程系统。如果这不起作用,则是某种配置问题。

回答by darioo

First, download MySQL's JDBC driverand put it somewhere in your application's classpath.

首先,下载 MySQL 的JDBC 驱动程序并将其放在应用程序类路径中的某个位置。

Second, try to register that driver in your Java code, using

其次,尝试在您的 Java 代码中注册该驱动程序,使用

Class.forName("com.mysql.jdbc.Driver");

If that doesn't throw an exception, you've managed to register sucessfully.

如果这没有引发异常,则您已成功注册。

Third, check if your connection works:

第三,检查您的连接是否有效:

Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","user", "pass");

Substitute your URL, username and password as needed.

根据需要替换您的 URL、用户名和密码。