无法通过JDBC与MySQL数据库连接
我试图通过JDBC与MySQL数据库进行连接。但是我无法将其与VS代码联系起来。我只是一个问题,我们需要Mavan项目总是与DB连接。我还添加了代码,请参考并建议我如何与DB连接。 谢谢!
我连接时遇到的错误。 在此处输入图像描述
package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
public class createDb {
// JDBC and database properties.
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "root";
public static void main(String args[]) {
Connection conn = null;
try {
// Register the JDBC driver
Class.forName(DB_DRIVER);
// Open the connection
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
if (conn != null) {
System.out.println("Successfully connected.");
} else {
System.out.println("Failed to connect.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have tried to connect with the mysql database by the JDBC. But I am no able to connect it with the vs code.I just have a question that we need the mavan project to connect with db always. I have also add the code please Refer and suggest me how can i connect with the db.
Thanks!
The error that i have got while connecting.
enter image description here
package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
public class createDb {
// JDBC and database properties.
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "root";
public static void main(String args[]) {
Connection conn = null;
try {
// Register the JDBC driver
Class.forName(DB_DRIVER);
// Open the connection
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
if (conn != null) {
System.out.println("Successfully connected.");
} else {
System.out.println("Failed to connect.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论