import java.sql.*;public class SimpleJdbc {
  public static void main(String[] args)
    throws SQLException, ClassNotFoundException {
    // Load the JDBC driver
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Driver loaded");
    // Establish a connection
    Connection connection = 
      DriverManager.getConnection("jdbc:mysql://localhost/test", "******", "******");//user跟password匿了
    System.out.println("Database connected");
    // Create a statement
    Statement statement = connection.createStatement();
    // Execute a statement
    ResultSet resultSet = statement.executeQuery
      ("select firstName, mi, lastName from Student where lastName = 'Smith' ");
    // Iterate through the result and print the student names
    while (resultSet.next())
      System.out.println(resultSet.getString(1) + "\t" +resultSet.getString(2) + "\t" + resultSet.getString(3));
    // Close the connection
    connection.close();
  }
}
Connection connection = 
      DriverManager.getConnection("jdbc:mysql://localhost/test", "******", "******");
问题就是我安装的MySQL没有提示我输入user跟password,只输入password,现在我使用上面这条语句都出问题了,
我只知道自己MySQL数据库的密码,而不知道用户名,那该怎么办啊?

解决方案 »

  1.   

    我知道了,原来user名是root啊
      

  2.   

     DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");
      

  3.   

    但是又该怎么修改user和password呢?
      

  4.   

    user 是不能改的,只能改 password
      

  5.   

    估计你是使用这样的命令登录的 mysql -u root -p
      

  6.   

    更改 MySQL root 账号的密码可以使用这个 SQL 语句:UPDATE user
       SET password = PASSWORD('new_password')
     WHERE user = 'root' AND host = 'localhost';这是系统级操作,请谨慎操作,操作前请先 SELECT 相同条件的数据做好备份。
      

  7.   

       进入数据库:Server Host:localhost
                  Username:root(超级用户)
                    Password:(自己设置的)
      

  8.   

    默认高级管理员账号 root
    UPDATE user
       SET password = PASSWORD('new_password')
       WHERE user = 'root' AND host = 'localhost';
      

  9.   

    MySql在安装时,可以用 root 创建 其他用户