赞
踩
数据库提供了各种语言版本的API(一组函数/一组类),调用这些API就能操作数据库
而在Java中,又将各种数据库的api再次抽象封装成一套统一的API——JDBC
在中央仓库 Maven Repository: Search/Browse/Explore (mvnrepository.com)下载jar包
目录
先在要运行的项目中创建一个目录(lib)
右击项目名 -> New -> Directory
其实就是一个文件夹
把下载好的jar包拷贝到这个目录中,右击jar包,选择Add as Library(库)...
在新的对话框中点击OK
导入成功,这样就可以直接使用了~
DataSource这个概念,表示“数据库在哪”
- public static void testInsert() {
- //1、创建一个数据源对象
- DataSource dataSource = new MysqlDataSource();
- }
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java101?characterEncoding=utf8&useSSL=false");
URL典型结构:
协议名称: jdbc:mysql( jdbc表示使用jdbc访问数据库,mysql表示访问的数据库是mysql)
IP地址:一般写作“点分十进制”
要访问的数据库名:/xxx
额外参数:?characterEncoding=utf8&useSSL=false
指定字符编码:characterEncoding(此处写的是utf8,得和数据库服务器设置的编码方式统一)
useSSL:是否加密
在MySQL Workbench中可以查看 数据库的ip端口 数据名
如下图:127.0.0.1表示本机自身,端口号为安装服务器时手动设置,默认为3306
IP 区分是哪个主机,port端口号 是区分主机上的哪个程序
root表示默认管理员
((MysqlDataSource)dataSource).setUser("root");
MySQL登录密码
((MysqlDataSource)dataSource).setPassword("xxxx");
连接数据库,进行网络通信
- import java.sql.Connection; //注意导入标准库中的
- Connection connection = dataSource.getConnection();
构造SQL语句,为插入做准备
- // 构造一个SQL语句,为插入做准备!
- // 本质还是用SQL写
- // 此处不需要use 数据库,因为在URL中已经设置好数据库的名字
- String sql = "insert into student values(1,'张三')";
- // 光搞一个String的 sql 还不够,要想真正运行SQL,还需要一个专门的对象,PreparedStatement
- PreparedStatement statement = connection.prepareStatement(sql);
- // 执行SQL
- // 客户端把 SQL 通过网络请求,发送给Mysql服务器
- // mysql 服务器来解析这个 SQL 请求,执行具体操作,返回响应结果
- // 此处使用 executeUpdate 来完成数据库内容的变更,(变更包含 insert, update, delete)
- // 返回值表示这个操作影响的行数
- int ret = statement.executeUpdate();
- System.out.println("ret: "ret);
-
- // 执行完 SQL 之后,回收资源
- statement.close();
- // 客户端与服务器的连接
- connection.close();
- // 删除操作
- public static void testDelete() throws SQLException {
- //1. 创建数据源,设置信息
- DataSource dataSource = new MysqlDataSource();
- ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
- ((MysqlDataSource)dataSource).setUser("root");
- ((MysqlDataSource)dataSource).setPassword("1230");
- //2. 和数据库建立连接
- Connection connection = dataSource.getConnection();
- //3. 构造 SQL
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要删除的id:");
- int id = scanner.nextInt();
- String sql = "delete from student where id = ?";
- PreparedStatement statement = connection.prepareStatement(sql);
- statement.setInt(1,id);
- //4. 执行 SQL
- int ret = statement.executeUpdate();
- System.out.println("ret=" + ret);
- //5. 释放资源
- statement.close();
- connection.close();
- }

- //修改操作
- public static void testUpdate() throws SQLException {
- //1.创建数据源
- DataSource dataSource = new MysqlDataSource();
- ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
- ((MysqlDataSource)dataSource).setUser("root");
- ((MysqlDataSource)dataSource).setPassword("1230");
- Connection connection = dataSource.getConnection();
- // 构造SQL
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要修改的id:");
- int id = scanner.nextInt();
- System.out.println("请输入要修改的名字:");
- String name = scanner.next();
- String sql = "update student set name = ? where id = ?";
- PreparedStatement statement = connection.prepareStatement(sql);
- statement.setString(1,name);
- statement.setInt(2,id);
-
- //执行SQL
- int ret = statement.executeUpdate();
- System.out.println("ret: "+ ret);
-
- //回收资源
- statement.close();
- connection.close();
- }

- // 查找操作
- public static void testSelect() throws SQLException {
- //数据源
- DataSource dataSource = new MysqlDataSource();
- ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
- ((MysqlDataSource)dataSource).setUser("root");
- ((MysqlDataSource)dataSource).setPassword("1230");
- //建立连接
- Connection connection = dataSource.getConnection();
- //构造SQL
- String sql = "select * from student";
- PreparedStatement statement = connection.prepareStatement(sql);
- //执行SQL
- ResultSet resultSet = statement.executeQuery();
- //遍历结果集合,结果集合非常类似于迭代器
- while (resultSet.next()) {
- int id = resultSet.getInt("id");
- String name = resultSet.getString("name");
- System.out.println("id: "+id+",name: "+name);
- }
- //释放资源
- resultSet.close();
- statement.close();
- connection.close();
- }

MySQL是一个"客户端-服务器"结构的程序
服务器是MySQL的本体,负责管理数据
客户端有很多种形态(cmd,workbench,JDBC代码…)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。