我有这个Java程序: MySQLConnectExample.java
import java.sql.*; import java.util.Properties;
public class MySQLConnectExample { public static void main(String[] args) { Connection conn1 = null; Connection conn2 = null; Connection conn3 = null;
try {
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null)
System.out.println("Connected to the database test1");
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "aa");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
}
} 我这样编译它:
E:\java mysql code driver>javac MySQLConnectExample.java
E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;. MySQLConnectExample 我收到此错误:
An error occurred. Maybe user/password is invalid java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ aavikme at java.sql.DriverManager.getConnection(DriverManager.java:596) at java.sql.DriverManager.getConnection(DriverManager.java:215) at MySQLConnectExample.main(MySQLConnectExample.java:20) 我究竟做错了什么?
确保首先运行此命令:
Class.forName("com.mysql.jdbc.Driver"); 这迫使驱动程序进行自身注册,以便Java知道如何处理那些数据库连接字符串。
有关更多信息,请参见MySQL Connector参考。来源:stack overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。