java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
这错误到底怎么改啊?本人还是菜鸟,希望大家能说得详细一点,谢谢啦!!
package com.jingxin.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCText {
public static void main(String[] args) {
String sql="SELET * FROM tbl_user";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");//注册JDBC驱动程序,forname用来初始化参数指定的类,并创建一个指定的实例对象。
String url = "jdbc:mysql://127.0.0.1:3306/testdb?"
+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
conn = DriverManager.getConnection(url);
st=conn.createStatement();
rs=st.executeQuery(sql);//发送sql语句
/*对ResultSet对象进行遍历*/
while(rs.next()){
System.out.println(rs.getInt("id")+" ");//用来获取数据库中字段类型为整型的字段值。
System.out.println(rs.getString("name")+" ");
System.out.println(rs.getString("password")+" ");
System.out.println(rs.getString("email")+" ");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
}
try {
st.close();
} catch (Exception e2) {
// TODO: handle exception
}
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
使用Java获取MySQL连接时出现错误,在网上找了一些方法,都说是因为数据库连接资源等待时间太短的问题,但是在配置文件my.ini中加上wait_timeout=2073600(对应时间为24天),重启服务后仍出现错误。
有没有遇到相同情况的,帮忙解释下。......
答案就在这里:Java获取MySql连接错误
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
Access denied for user 'root'@'localhost' (using password: YES)
确认一下账号密码是否正确。
String url = "jdbc:mysql://127.0.0.1:3306/testdb?"
+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
检查这一行的用户名密码
package com.mucfc;
import java.sql.*;
public class JdbcTest {
//定义数据库驱动程序
private static final String DBDRIVER="com.mysql.jdbc.Driver";
//数据库连接地址
private static final String DBURL="jdbc:mysql://localhost:3306/school";//school表示数据库
//数据库用户名
private static final String DBUSER="root";
//电脑上的数据库密码
private static final String DBPASS="christmas258@";
public void testDDL(){
try {
//1.注册驱动
Class.forName(DBDRIVER);
//2.获取连接
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
//3.创建Statement对象
Statement stmt = conn.createStatement();
//4.准备sql语句
String sql ="CREATE TABLE student(sid INT PRIMARY KEY,sname VARCHAR(20),age INT)";
//5.通过statement对象发送sql语句,返回执行结果
int count = stmt.executeUpdate(sql);
System.out.println("CREATE TABLE student......");
//6.打印执行结果
System.out.println("影响了"+count+"条记录");
//执行插入操作
System.out.println("Inserting records into the table...");
sql = "INSERT INTO student " +
"VALUES (100, '小文', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO student " +
"VALUES (101, '大林', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO student " +
"VALUES (102, '阿白', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO student " +
"VALUES(103, '小小', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
//执行查找操作
sql = "SELECT* FROM student";
System.out.println("SELECT records FROM the table...");
ResultSet rs = stmt.executeQuery(sql);
//输出查找结果
while(rs.next()){
//先获取数据
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
int age = rs.getInt("age");
//打印结果
System.out.print("sid: " + sid);
System.out.print(" sname: " +sname);
System.out.println(" age: " + age);
}
rs.close();
//7.关闭资源
try {
if(stmt!=null)
{
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(conn!=null)
{
conn.close();
}
} catch (Exception e) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JdbcTest jdbcTest=new JdbcTest();
jdbcTest.testDDL();
}
}
http://blog.csdn.net/evankaka/article/details/45370609
看上去还是数据库用户名密码不正确,或者你的编码格式不对,导致验证不通过。
这个错误应该就是用户名密码不对吧
你要是么有设置用户的话,默认就是root,现在需要确认你的密码是否正确了。如果你有mysql客户端或者命令行的话,启动他们输入你的密码看看能不能连接上。
应该是密码不正确,确认密码正确后在连接看看
Access denied for user 'root'@'localhost' (using password: YES)
账号密码正确吗,确认就是想你安装时候密码啊,账户,这都不能忘记的!,忘记了只能重装,,
characterEncoding=UTF-8
密码错误,可以尝试在mysql的命令行中试一下这个密码可行不