jdbc连接mysql失败,始终Access denied for user 'root'@'localhost' (using password: YES)
eclipse,tomcat9.0,mysql5.6.35,在mysql用户名密码登录正常,权限也给%了,防火墙也关了,代码:packagecom.DBTool;impo...
eclipse,tomcat9.0,mysql 5.6.35,在mysql用户名密码登录正常,权限也给%了,防火墙也关了,代码:package com.DBTool; import java.sql.Connection; import java.sql.DriverManager; import java.sql.*; import com.mysql.jdbc.Driver; public class DBUtil { private static String url="jdbc:mysql://localhost:3306/stuffinf"; private static String driverClass="com.mysql.jdbc.Driver"; private static String username="root"; private static String password="123456"; private static Connection conn; //装载驱动 static{ try{ //加载驱动程序 Class.forName(driverClass); System.out.println("加载驱动"); } catch(ClassNotFoundException e){ System.out.println("无法加载驱动"); e.printStackTrace(); } } //获取数据库连接 public static Connection getConnection(){ try{ conn = DriverManager.getConnection(url,username,password); if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); } catch(SQLException e){ e.printStackTrace(); System.out.println("连接失败"); } return conn; } //建立数据库连接 public static void main(String[] args){ Connection con=DBUtil.getConnection(); if(conn==null){ System.out.println("数据库连接失败!"); } else { System.out.println("数据库连接成功!"); } } //关闭数据库连接 public static void Close(){ if(conn!=null){ try{ conn.close(); } catch(SQLException e){ e.printStackTrace(); } } } }
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询