急求解决一下下面这个问题


开始我进入mysql建表的时候一直报错密码不对什么的,我就使用的匿名登录,没有用密码登陆进去了,但是写JDBC程序的时候要连接mysql,程序报错如下:(PS:应该怎么解决呢?急求!!)

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)

at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4417)

at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1278)

at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)

at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)

at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)

at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:408)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)

at java.sql.DriverManager.getConnection(DriverManager.java:664)

at java.sql.DriverManager.getConnection(DriverManager.java:247)

at TestJDBC.main(TestJDBC.java:16)

源程序如下:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;



public class TestJDBC {

    public static void main(String[] args){

   

    ResultSet rs=null;

    Statement stmt=null;

    Connection conn=null;

    try{

    Class.forName("com.mysql.jdbc.Driver");

    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/yuangong","root","root");

    stmt=conn.createStatement();

    rs=stmt.executeQuery("select* from yuangongbiao");

    while(rs.next()){

    System.out.print(rs.getInt("employee_id"));

    System.out.print(rs.getInt("name"));

    System.out.print(rs.getInt("email"));

    System.out.print(rs.getInt("salary"));

    System.out.print(rs.getInt("birthday"));

    }

    }catch(ClassNotFoundException e){

    e.printStackTrace();

   

    }catch(SQLException e){

    e.printStackTrace();

    }finally{

    try{

    if(rs!=null){

    rs.close();

    rs=null;

    }

    if(stmt!=null){

    stmt.close();

    stmt=null;

    }

    }catch(SQLException e){

   

    e.printStackTrace();

    }

    }

   

    }

}


pstereo
浏览 2114回答 3
3回答

慕粉3291149

while(rs.next())循环里面写错了,你这里写的都是getInt(""),兄弟,你这不可能都是整形数据吧,应该是getString("name"),getString("email"),......里面的数据是什么类型,就获取什么类型的

KilSoul

第一行就说明原因了,用户名和密码不对呗。Google一下MySQL改密码
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
SQL Server