我从 Oracle 的网站上拿了这个例子,但我无法实现它。它一直显示错误。我已经将 mysql 5.1 驱动程序 jar 添加到我的项目 lib 文件夹以及我的构建路径中。如何修复我的代码?
链接 - http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
错误 -
java.sql.SQLException: No suitable driver found for jdbc:JavaEE://localHost:3306/
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at com.beans.us.dao.Data.getConnection(Data.java:25)
at com.beans.us.dao.Data.main(Data.java:43)
Cannot connect to database
代码 -
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Data {
public static Connection getConnection() {
Connection conn = null;
Properties connectionProps = new Properties();
String userName = "root";
String password = "root";
String dbms = "JavaEE";
String serverName = "localHost";
String portNumber = "3306";
connectionProps.put("user", userName);
connectionProps.put("password", password);
try {
conn = DriverManager.getConnection("jdbc:" + dbms + "://"
+ serverName + ":" + portNumber + "/", connectionProps);
} catch (SQLException sQLException) {
sQLException.printStackTrace();
}
if (conn != null) {
System.out.println("Connected to database");
} else {
System.out.println("Cannot connect to database");
}
return conn;
}
public static void main(String[] args) {
getConnection();
}
}