代码如下:importjava.sql.*;publicclasstest_mySQL{publicstaticvoidmain(String[]args)throwsSQLException,ClassNotFoundException{Class.forName("com.mysql.jdbc.Driver");String...
代码如下:
import java.sql.*;
public class test_mySQL {
public static void main(String[] args)
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:..\\MySQL-5.0.90\\data\\mydb";
String userName = "root";
String password = "aptx4869";
Connection connection = DriverManager.getConnection(url,userName,password);
Statement statement= connection.createStatement();
String sql = "SELECT * FROM userinfo";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
String name = resultSet.getString(2);
String address = resultSet.getString(3);
System.out.println(name + " " + address);
System.out.println();
}
resultSet.close();
statement.close();
connection.close();
}
}
报错如下:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:..\MySQL-5.0.90\data\mydb
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test_mySQL.main(test_mySQL.java:9)
是不是驱动没导入对啊我的mySQL是5.0.90版本,用的mysql-connector-java5.0.8和5.1.7都下了,应该用哪个,怎么导入啊?还要不要设置环境变量啊?
展开