我知道这个问题已经问了很多次了,但我一直没有找到一个可行的答案。
我对这一行有问题,说的是通常的ClassNotFoundException。
未处理异常: java.lang.ClassNotFoundException
在控制台里:
java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:xxxxxxxxx
Class.forName("com.mysql.jdbc.Driver");但是我在我的项目设置中包含了jar (mysql-连接器-java-5.1.41-bin.jar),我还把它放在我的java安装(JRE和JDK)的所有lib目录中。
下面是更多代码:公共类连接实现侦听器{
static final String DB_URL = "jdbc:mysql:xxxxxxxxx";
static final String USER = "xxxxxx";
static final String PASS = "xxxxxx";
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
Connection conn = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO Players (UUID, Pseudo, Rank, Level, Current_xp, Nbr_kick, Nbr_ban) VALUES (" + p.getUniqueId() + ", " + p.getName() + ", Dev, 1, 0, 0, 0);";
ResultSet rs = stmt.executeQuery(sql);
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
getLogger().warning(se.toString());
}
}这是我的类石榴:这个项目的分类路径
编辑:
好的,它的工作原理如下:我使用了以下代码(不是我的),经过几次更正之后,它起了作用(不要像我一样,不要忘记jdbc:mysql://xxxx:3306/xxxx中的两个斜杠)
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException es) {
System.out.println("Where is your MySQL JDBC Driver?");
es.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://xxxx:3306/xxxx", USER, PASS);
} catch (SQLException es) {
System.out.println("Connection Failed! Check output console");
es.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}发布于 2017-03-20 22:06:27
您应该将mysql驱动程序依赖项添加到maven或gradle中(如果使用maven或gradle项目),或者从此链接下载jar文件,并将其添加到项目的类路径中。一定要起作用。如果没有-你做了一些不正确的事情。
https://stackoverflow.com/questions/42913667
复制相似问题