mysql 数据库连接报错误:数据库连接成功数据库连接失败java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: NO),如下图所示:
解决方法:1.连接url格式出现了问题(Connection conn = DriverManager.getConnection (“jdbc:mysql://localhost:3306/数据库名”,“root”,“密码”) 2.驱动字符串出错(com.mysql.jdbc.Driver) 3.Classpath中没有加入合适的mysql_jdbc驱动 4.没有mysql-connector-java-3.1.14-bin.jar的jar包,或者路径出错
检查是否有启动驱动程序db.properties配置文件如下: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false username=root password=123456(注意密码不能错)
或者是以下情况:
修改前的代码如下所示:
package cn.qjq.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
static {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties p = new Properties();
try {
p.load(is);
driver = p.getProperty("driver");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password ");
Class.forName(driver);
System.out.println("驱动成功");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
System.out.print("数据库连接成功");
return DriverManager.getConnection(url,username,password);
}catch (Exception e) {
System.out.print("数据库连接失败");
e.printStackTrace();
}
return null;
}
修改后的代码如下:
package cn.qjq.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
static {
try {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
可能问题出现在以下这一块: driver = p.getProperty(“driver”); url = p.getProperty(“url”); username = p.getProperty(“username”); password = p.getProperty("password ");
结果展示
|