【发布时间】:2018-10-06 13:18:56
【问题描述】:
正如我在该主题中提到的,我正面临“未使用局部变量 conn 的值”的警告
我清楚地在我的编码中使用了该变量,但它向我显示了这种类型的错误。 如果您能就此提出建议,我们将不胜感激。
[代码]
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class example {
public static void main(String[] ar) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success to find Driver");
}catch(ClassNotFoundException e) {
System.err.println("error = Failed to find driver");
}//*Find MYSQL driver
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/class", "root", "****");
System.out.println("Connected to MySql");
} catch (SQLException e) {
System.err.println("error = Failed to Create connection");
}//* Creating Connection
}
}
[运行后的结果]
成功找到驱动程序 错误 = 无法创建连接
【问题讨论】:
-
好吧,你定义了 conn 但你从来没有对它做任何事情。您期望会发生什么?
标签: java mysql connection