这是怎么回事

来源:8-3 DBHelper类设计

再回首是清欢

2018-03-24 02:35

package util;

import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelper {
 private static final String driver = "com.mysql.jdbc.Driver"; //数据库驱动
 //连接数据库的URL地址
 private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8";
 private static final String username="root";//数据库的用户名
 private static final String password="";//数据库的密码
   
 private static Connection conn=null;
 
 //静态代码块负责加载驱动
 static
 {
  try
  {
   Class.forName(driver);
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }
 
 //单例模式返回数据库连接对象
 public static Connection getConnection() throws Exception
 {
  if(conn==null)
  {
   conn = DriverManager.getConnection(url, username, password);
   return conn;
  }
  return conn;
 }
 
 public static void main(String[] args) {
  
  try
  {
     Connection conn = DBHelper.getConnection();
     if(conn!=null)
     {
      System.out.println("数据库连接正常!");
     }
     else
     {
      System.out.println("数据库连接异常!");
     }
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  
 }
}

Sat Mar 24 02:30:06 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1714)
 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1224)
 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2190)
 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
 at java.sql.DriverManager.getConnection(DriverManager.java:664)
 at java.sql.DriverManager.getConnection(DriverManager.java:247)
 at util.DBHelper.getConnection(DBHelper.java:32)
 at util.DBHelper.main(DBHelper.java:42)

写回答 关注

2回答

  • qq_慕姐4508679
    2019-04-02 13:57:15

    我的开始也是这样,应为下载的源代码里边是没有输入密码的,可能你的mysql以前设置了密码,所以提示没有输入密码,应该在DBHelper.java中的private static final String password="";  引号中输入密码。

  • qq_无意识_0
    2018-04-02 15:35:11

    去下个SSL安装就行了

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题