1、先将下载好jdbc复制粘贴进你的项目
2、右键项目 Build Path -> add Libraries->User Library->User Libraries->New -> 命名extendlib->Add JARS
把上一步复制的jdbc等lib包加进去
3、Build Path ->Configure Build Path ->Add Library->User Library->extendlib
然后连接基本完成。
4、通过建一个 ConnectJdbc.java 测试是否连接成功
import java.sql.*;
public class ConnectJdbc {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/weatheraqi","root","1234");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from bar");
//bar 为你表的名称
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}