ecplise2019-03连接数据库mysql8.0出错,我的ecplise连接SQL是好的
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class a1 {
public static void main(String []args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// The newInstance() call is a work around for some
// broken Java implementations
try { Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC","root","qing980104");
System.out.println("连接成功");
}
} catch (SQLException ex) {
// handle any errors
System.out.println("连接失败");
}
}
}
结果显示
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at experiment1.a1.main(a1.java:7)
我把代码换成运行成功的SQL连接代码,然后改了下
import java.sql.*;
public class a1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn= DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/shop?serverTimezone=GMT%2B8","root","qing980104");
Statement stm=conn.createStatement();
ResultSet re=stm.executeQuery("select * from book");
while(re.next()) {
System.out.println("书名:"+re.getString(1)+"书号:"+re.getString(2)+"价格:"+re.getString(3));
}
re.close();
stm.close();
conn.close();
}
}
结果显示
Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at mysql.connector.java@8.0.15/com.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)
我以前写的java课设中连接数据库也是显示上面(using password: NO)这个结果
网上有说是MySQL8.0用了强密码的缘故,我重下装了改成旧方式,但还是不行,我又重装了n次,结果现在只能是旧方式,无法改成强密码方式。而且程序还是不行。求大佬解答