jdbc问题,数据库拒绝访问是什么原因?
报错提示:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'mycinema'
//查看数据分类
public void getAllCategoryList(){
Connection conn=null;
PreparedStatement stam=null;
ResultSet rs=null;
try {
//1、加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//2、获取数据库连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/MyCinema");
//3、获取Statement对象,执行sql语句
String sql="select * from Category";
stam=conn.prepareStatement(sql);
rs=stam.executeQuery();
//4、处理sql执行结果
while(rs.next()){
int id=rs.getInt("id");
String name =rs.getString("name");
System.out.println(id+":"+name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据库
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stam!=null){
try {
stam.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(true){
System.out.println("请选择功能:1.添加分类 2.查看现有分类 3.退出(1/2/3)");
int nr=input.nextInt();
switch(nr){
case 1:
Category c=new Category();
System.out.print("请输入添加的分类名称:");
c.setName(input.next());
System.out.println("添加成功!");
break;
case 2:
System.out.println("现有的分类如下:");
TestMovie tm=new TestMovie();
tm.getAllCategoryList();
break;
case 3:
System.out.println("已退出,谢谢使用!");
return;
default:
System.out.println("输入有误,请重新选择:");
}
}
}