我的Java代码哪里错了(链接mysql)?
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.Statement;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.*;
public class jdbcTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/guanli?user=zhaohui&password=zhaohui";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
while (rs.next()) {
System.out.print("学生的学号:" + rs.getInt(3));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("找不到指定的驱动程序类!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
出错信息:java.sql.SQLException:Access denied for user 'zhaohui'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:371)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at test.main(test.java:12) 展开
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.*;
public class jdbcTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/guanli?user=zhaohui&password=zhaohui";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
while (rs.next()) {
System.out.print("学生的学号:" + rs.getInt(3));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("找不到指定的驱动程序类!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
出错信息:java.sql.SQLException:Access denied for user 'zhaohui'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:371)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at test.main(test.java:12) 展开
展开全部
import java.sql.*;
public class SimpleIntro
{
public static void main(String[] args)
{
try
{
Connection conn;
Statement stmt;
ResultSet res;
//加载Connector/J驱动
//这一句也可写为:Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的连接.password根据实际情况更改
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",
"root", "123");
//执行SQL语句
stmt = conn.createStatement();
res = stmt.executeQuery("select *from userinfo ");
//处理结果集
while (res.next())
{
String name = res.getString("UserName");
String pass=res.getString("UserPwd");
System.out.println(name+"密码"+pass);
}
res.close();
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
这是个范例 你看下哪错了
public class SimpleIntro
{
public static void main(String[] args)
{
try
{
Connection conn;
Statement stmt;
ResultSet res;
//加载Connector/J驱动
//这一句也可写为:Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的连接.password根据实际情况更改
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",
"root", "123");
//执行SQL语句
stmt = conn.createStatement();
res = stmt.executeQuery("select *from userinfo ");
//处理结果集
while (res.next())
{
String name = res.getString("UserName");
String pass=res.getString("UserPwd");
System.out.println(name+"密码"+pass);
}
res.close();
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
这是个范例 你看下哪错了
展开全部
Access denied
很简单的一个错,就是拒绝
3种可能:
1、你的密码错误了
2、数据库没启动
3、端口错了(这个可能性小一些)
很简单的一个错,就是拒绝
3种可能:
1、你的密码错误了
2、数据库没启动
3、端口错了(这个可能性小一些)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据库服务未启动?端口错误?用户名错误?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/guanli?user=zhaohui&password=zhaohui";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
在Connection conn = DriverManager.getConnection(url); 后加上一句:
conn.open();
试试,:)
String url = "jdbc:mysql://localhost:3306/guanli?user=zhaohui&password=zhaohui";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
在Connection conn = DriverManager.getConnection(url); 后加上一句:
conn.open();
试试,:)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.sql.SQLException:Access denied for user 'zhaohui'@'localhost' (using password: YES)
错误信息意思就是说你用户名密码又问题。。
错误信息意思就是说你用户名密码又问题。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询