JDBC:Java Database Connectivity,即Java数据库连接。是一种用于执行SQL语句的Java API,它是Java的数据库连接规范。
API:Application Programming Interface,即应用程序编程接口。它提供了一组类/方法,可以让程序员直接调用。
JDBC是怎么出现的呢?
不同的数据库厂商都会提供各自的一组数据库API供程序员使用,这样就会带来两个问题:1.程序员的学习成本太高,得学习多种API来使用不同的数据库 2. 当哪一天需要给项目更换数据库时,改动非常大,不方便。 而Java是一门可移植性高的语言,它为了解决这些问题就提供了一组统一风格的数据库API,然后要求各个厂商适配这些API,程序员只要掌握这一组API就可以操作各种数据库了,这组API就叫做JDBC
JDBC为多种关系型数据库提供了统一访问方式,


3. 右击新建好的目录,点击 Add as Library

//1.创建数据源对象
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/learning?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("xu0123.");

结合我们上面讲到的JDBC原理图,DataSource属于JDBC模块,MysqlDataSource属于驱动模块,DataSource是MysqlDataSource的父类,我们在使用JDBC进行数据库操作时都需要通过驱动将JDBC转化为数据库原生API才能让对应的数据库识辨指令。因此,在创建对象的时候也需要创建一个驱动对象。

首先,setURL这个方法存在于MysqlDataSource中在DataSource中没有,所以需要向下转型。那为什么没有呢? 因为DataSource是JDBC中有的类,用来操作使用的数据库。但是,只有客户端—服务器结构的数据库才有URL的说法来定位服务器位置,对于非客户端—服务器结构的数据库不需要使用URL,所以DataSource就不能包含setURL方法。
其次,URL是什么呢? URL就是 唯一资源定位符 。用来定位位置的。


输入数据库的用户名,默认都是 root 。用来登录数据库。
输入数据库的密码,每个用户都有自己的密码。用来登录数据库。

//2.让代码和数据库服务器建立连接
Connection connection = dataSource.getConnection();

注意在导包的时候使用第一个 不要使用第二个
- 还可以使用DriverManager的静态方法来建立数据库连接,但是一般不用!
Connection connection = DriverManager.getConnection(url);
- 他们俩的区别是: DriverManager:每次getConnection都需要重新建立连接; DataSource:则是内置了连接池,实现了连接复用,提高了效率;
//3.构造要执行的SQL语句
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要插入的学号");
int id = scanner.nextInt();
System.out.println("请输入要插入的姓名");
String name = scanner.next();
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.setString(2,name);
首先,把SQL语句写成一个字符串,并且使用占位符? 代替真正要操作的数据,这是为了避免有人使用SQL注入的方式来破坏你的数据库。
其次,使用PreparedStatement进行预处理SQL语句,得到一个预处理过的SQL语句对象。
然后,通过set方法来填充预处理SQL语句中的占位符?,把真正的数据替换进去。set方法的第一个参数表示替换第几个?,第二个参数表示填充的数据是啥。
//4.执行SQL语句
int n = statement.executeUpdate();
执行方法有俩个:
executeUpdate:对应插入、修改、删除语句使用。返回这次SQL操作影响到的行数。
executeQuery:对应查询语句使用。返回查询的结果集。
//5.关闭释放资源
statement.close();
connection.close();
释放资源的顺序要和申请资源的顺序相反,即:先申请的后释放,后申请的先释放。
public static void main(String[] args) throws SQLException {
//1.创建数据源对象
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/learning?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("xu0123.");
//2.让代码和数据库服务器建立连接
Connection connection = dataSource.getConnection();
//3.构造要执行的SQL语句
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要插入的学号");
int id = scanner.nextInt();
System.out.println("请输入要插入的姓名");
String name = scanner.next();
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.setString(2,name);
//4.执行SQL语句
int n = statement.executeUpdate();
//5.关闭释放资源
statement.close();
connection.close();
}
public static void main(String[] args) throws SQLException {
//1.建立数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/learning?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("xu0123.");
//2.建立程序和数据库的连接
Connection connection = dataSource.getConnection();
//3.构造SQL语句
String sql = "select* from student where id = ?";
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的id");
int inputId = scanner.nextInt();
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,inputId);
//4.执行SQL语句
ResultSet resultSet = statement.executeQuery();
//5.处理结果集
while (resultSet.next()){
//每次循环能得到结果集的一行
//get方法里的参数代表要得到第几列的数据
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
System.out.println("id:"+ id + "name:" + name);
}
//6.释放资源
resultSet.close();
statement.close();
connection.close();
}
public static void main(String[] args) throws SQLException {
//1.创建数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/learning?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("xu0123.");
//2.建立程序和数据库的连接
Connection connection = dataSource.getConnection();
//3.构造SQL语句
String sql = "update student set name = ? where id = ?";
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改成什么名字");
String name = scanner.next();
System.out.println("请输入要修改的id");
int id = scanner.nextInt();
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
//4.执行SQL
int n = statement.executeUpdate();
//5.释放资源
statement.close();
connection.close();
}
public static void main(String[] args) throws SQLException {
//1.创建数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/learning?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("xu0123.");
//2.建立程序和数据库的连接
Connection connection = dataSource.getConnection();
//3.构造SQL语句
String sql = "delete from student where id = ?";
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的id");
int id = scanner.nextInt();
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
//4.执行SQL语句
int n = statement.executeUpdate();
//5.释放资源
statement.close();
connection.close();
}