java and mysql ????

java and mysql ????

#javac MysqlJDBCDemo.java
#java MysqlJDBCDemo

error--
Exception in thread "main" java.sql.SQLException: General error: Access denied for user: '@swh' to database 'students'
       at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:935)
       at com.mysql.jdbc.Connection.createNewIO(Connection.java:1823)
       at com.mysql.jdbc.Connection.connectionInit(Connection.java:971)
       at com.mysql.jdbc.Driver.connect(Driver.java:400)
       at java.sql.DriverManager.getConnection(DriverManager.java:512)
       at java.sql.DriverManager.getConnection(DriverManager.java:171)
       at MysqlJDBCDemo.main(MysqlJDBCDemo.java:17)


/*------------MyJDBCEemo.java--------*/
import java.sql.*;

public class MysqlJDBCDemo {
  public static void main(String[] args) throws Exception {

    String mySqlDriver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/students?user=root;password=";

    String query, subject, answer;

    Connection conn;
    Statement statement;
    ResultSet resultSet = null;


    Class.forName(mySqlDriver);
    conn = DriverManager.getConnection(url, "", "");

    statement = conn.createStatement();
    query = "select * from student";
    resultSet = statement.executeQuery(query);
    while (resultSet.next()) {
      subject = resultSet.getString("name");
      answer = resultSet.getString("grade");
      System.out.print("name = " + subject);
      System.out.println(",grade = " + answer);
    }

    resultSet.close();
    statement.close();
    conn.close();
  }
}

help me !!!!      
问题出在mysql
请尝试一下步骤
1.查看mysql里users表,看对应信息是否是你想要的
2.给root用户加密码或者用其他有密码的用户(没有建一个也可以)
3.如果你的应用和数据库不在同一台计算机上,看看远程计算机能否连通你的mysql      
[QUOTE]最初由 Terran 发布
[B]问题出在mysql
请尝试一下步骤
1.查看mysql里users表,看对应信息是否是你想要的
2.给root用户加密码或者用其他有密码的用户(没有建一个也可以)
3.如果你的应用和数据库不在同一台计算机上,看看远程计算机能否连通你的mysql [/B][/QUOTE]

我这样:
#mysql -u root mysql -p
mysql> INSERT INTO user VALUES('localhost','monty',PASSWORD('something'),
    ->                 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
mysql> FLUSH PRIVILEGES;

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    ->            ON student.*
    ->            TO monty@localhost
    ->            IDENTIFIED BY 'stupid';
---------------
String mySqlDriver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/students?user=monty;password=something";

--------------
# javac MysqlJDBCDemo.java
# java MysqlJDBCDemo
错误提示一样
:confused: :confused: :confused:      
你用mysql -u monty -psomething student能否连接到数据库,可不可以进行一般操作?如果我没有猜错,你的计算机名称叫swh,报的错误里有swh,也许jdbc连接mysql是用的swh的计算机名而不是localhost,按照JDBC的要求来给你的用户赋予权限吧。      
[QUOTE]最初由 Terran 发布
[B]你用mysql -u monty -psomething student能否连接到数据库,可不可以进行一般操作?如果我没有猜错,你的计算机名称叫swh,报的错误里有swh,也许jdbc连接mysql是用的swh的计算机名而不是localhost,按照JDBC的要求来给你的用户赋予权限吧。 [/B][/QUOTE]

os : redhat8.0
我的计算机名是 swh
#mysql -u monty -psomething students
ERROR 1045: Access denied for user: 'monty@localhost' (Using password: YES)

mysql 数据库是 students, 其中有一表为:student
现要读取表 student 中的资料
您帮我设一个MYSQL用户??
我不会。
先谢了!!      
mysql -u root -pxxxx mysql
mysql>grant all on students.* to monty@localhost identified by "something";
mysql>exit
service mysqld restart
然后你再试试
mysql -u monty -psomething students      
[QUOTE]最初由 Terran 发布
[B]mysql -u root -pxxxx mysql
mysql>grant all on students.* to monty@localhost identified by "something";
mysql>exit
service mysqld restart
然后你再试试
mysql -u monty -psomething students [/B][/QUOTE]

感谢 Treean 的回复

我现在用mysql -u monty -psomething students能连接到数据库,可以进行一般操作
但我这样----
String mySqlDriver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/students?user=monty;password=something";

--------------
# javac MysqlJDBCDemo.java
# java MysqlJDBCDemo
错误提示一样
改为---
String url = "jdbc:mysql://swh/students?user=monty;password=something";
错误提示还是一样
我已经弄了一星期了,
我该怎麽办哦???
:confused: :confused: :confused:      
mysql -u root -pxxxx mysql
mysql>grant all on students.* to monty@swh identified by "something";
mysql>exit
service mysqld restart      
[QUOTE]最初由 Terran 发布
[B]mysql -u root -pxxxx mysql
mysql>grant all on students.* to monty@swh identified by "something";
mysql>exit
service mysqld restart [/B][/QUOTE]

我这样(@localhost-->@swh):
#mysql -u root -p mysql
mysql>grant all on students.* to monty@swh identified by "something";
mysql>exit
#service mysqld restart

String mySqlDriver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/students?user=monty;password=something";

--------------
# javac MysqlJDBCDemo.java
# java MysqlJDBCDemo
错误提示一样
改为---
String url = "jdbc:mysql://swh/students?user=monty;password=something";
# javac MysqlJDBCDemo.java
# java MysqlJDBCDemo
错误提示还是一样
:confused: :confused: :confused:      
[QUOTE]最初由 Terran 发布
[B]mysql -u root -pxxxx mysql
mysql>grant all on students.* to monty@swh identified by "something";
mysql>exit
service mysqld restart [/B][/QUOTE]

感谢 Treean 的回复
现在可以了(;-->&)
String url = "jdbc:mysql://localhost/students?user=monty;password=something";
String url = "jdbc:mysql://localhost/students?user=monty&password=something";