第一、下载MySQL的驱动
去网上搜一下MySQL驱动:mysql-connector-java-5.1.12-bin.jar 其中5.1.12是版本不是这个版本基本也可以用,只要是mysql-connector-java就好,不用太纠结细节~
然后就是在Eclipse里导入这个jar包到项目中去。
第二、在代码中加载MySQL驱动
/**connect local mysql server*/
public static ResultSet myConnection(String SQL){
// MySQL Driver name
String driver = "com.mysql.jdbc.Driver";
// URL is derected to database wytings
String url = "jdbc:mysql://127.0.0.1:3306/wytings";
String user = "root";
String password = "111";
ResultSet result = null;
Connection conn = null;
Statement statement = null;
try {
// load driver
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()){
System.out.println("Succeeded connecting to the Database!");
}
//excute the SQL
statement = conn.createStatement();
result = statement.executeQuery(SQL);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
就可以取出ResultSet了。