前面我介绍了数据库一些用法,是时候合体了—JDBC
啥意思呢,百度
我们怎么来用呢,下载软件(因为eclipse,idea都没有办法直接连接数据库)
网上下载~(和很重要的一个能力,自学自查,除了学校不在有老师不厌其烦的给你详细讲解,同事给你讲那是出于面子,有些东西网上解决办法可用就别问,实在不行再去,作为最后一个选项)
这里小知识点,jar就是Java的压缩文件,zip–压缩文件格式,rar自带压缩,war是web的版块后面会详细介绍
mysql连接参数
我们前面写的数据库连接方式
mysql-> mysql -hlocalhost -uroot -p
前面没讲这个作用,只是说了连接数据库,其实这里给了很重要的信息
1.确定连接哪一个数据库 mysql
2.明确当前数据库的主机地址,ip地址,域名地址 h-host local–当地 host–主机
3.u–user 用户名,root–权限
4.-p password密码(以后数据库密码建议文本保存)
敲黑板
jdbc连接数据库也是需要四个条件
1.确定连接的数据库所在网络地址和对应操作哪一个数据库
这里符合jdbc规范的URL,就是连接数据库的标识
jdbc:mysql://localhost:3306/javaloveme
jdbc:mysql:当前操作数据库的主协议是jdbc,子协议是mysql
localhost:3306当前数据库所在的网络地址,3306数据库默认端口号
javaloveme:当前URl连续操作的数据库名字
2.用户名 user root
3 密码 password +++++
jdbc连接数据库
下载好了jar包,毫无疑问要导入~
打开idea(自己百度怎么改快捷键~和eclipse一样!!!!!!!)
几个重要的
1.ALT+insert 方法,你懂的
2 ctrl+shift+/ 注释
创建lib文件包
点击file,找到project str…
找到dependencies,左边有个加号,点击
选择第一个
看自己下载的jar包在哪,点击ok就行了,效果图如下
代码连接数据库
1.先测试(敲黑板,里面的代码必须理解会背)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* JDBC连接数据库操作,获取数据库连接方式
*
* @author Anonymous 2020/3/23 15:51
*/
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
/*
1. 加载驱动
*/
Class.forName("com.mysql.jdbc.Driver");
/*
2. 准备必要的数据
*/
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true";
String user = "root";
String password = "123456";
/*
3. 获取数据库连接
*/
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
/*
4. 关闭资源
*/
connection.close();
}
}
里面是我的数据库名字,看好自己的~
这是连接成功的标志,启动不成功的原因都是
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true";
String user = "root";
String password = "123456";
这一段代码很重要,自己看好,一般错误都在这个地方,不要问怎么知道的?
数据库加载过程
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
//
// Register ourselves with the DriverManager
// 在.class文件加载到内存时运行,并且有且只执行一次
// 代码初始化过程!!!
static {
try {
// DriverManager驱动管理器注册了当前com.mysql.jdbc.Driver
// 相对于当前Java程序拥有了连接MySQL数据库的必要的驱动条件
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
/**
* Construct a new driver and register it with DriverManager
*
* @throws SQLException
* if a database error occurs.
*/
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
}
}
4. JDBC核心API[能记住最好,记不住拉倒]
class java.sql.DriverManager 驱动管理类
–| static java.sql.Connection getConnection(String url, String user, String password);
/*
这里是根据听的数据库连接URL,对应的user用户名和password密码,获取数据库连接对象
*/
interface java.sql.Connection 数据库连接接口
–| java.sql.Statement createStatement();
/*
获取数据库SQL语句搬运工对象,从Java程序搬运SQL语句到数据库中,同时Statement也是一个资源对象。
/
–| java.sql.PreparedStatement prepareStatement(String sql);
/
获取数据库SQL语句【预处理】搬运工对象,Java程序的SQL语句,在创建PreparedStatement对象时,将SQL语句交给数据库预处理操作,可以解决一定的【SQL语句注入问题】,同时提高一定的效率,PreparedStatement也是一个资源对象
*/
interface java.sql.Statement 数据库SQL语句搬运工对象
–| int executeUpdate(String sql);
/*
执行数据库修改数据,insert,update,delete…,返回值类型是int类型,是当前SQL语句搬运到数据库执行之后,数据库运行对于当前操作受到影响的行数
2 rows affected in 5 ms
/
–| java.sql.ResultSet executeQuery(String sql);
/
执行数据库查询语句,select操作,执行的结果是一个java.sql.ResultSet,结果集对象,当前操作返回值never null
*/
interface java.sql.PreparedStatement 数据库SQL语句【预处理】搬运工对象
PreparedStatement extends java.sql.Statement
–| int executeUpdate();
/*
执行数据库修改操作,insert,update,delete…处理的SQL语句是在创建PreparedStatement对象过程预处理的SQL语句,并且返回值是int类型, 为当前操作对于数据表中收到影响的行数
/
–| java.sql.ResultSet executeQuery();
/
执行数据库查询语句,select操作,的SQL语句是在创建PreparedStatement对象过程预处理的SQL语句,执行的结果是一个java.sql.ResultSet,结果集对象,当前操作返回值never null
/
–| setXXX(int index, XXX value)
/
PreparedStatement预处理的SQL语句是可以带有参数的,这里是对于SQL语句参数进行赋值操作,这里有指定的操作下标,和对应的数据,数据类型繁多
*/
interface java.sql.ResultSet 数据库结果集接口
–|XXX getXXX(int columnIndex);
/*
根据查询结果中,字段所处的位置下标获取对应数据,XXX是指定类型
/
–|XXX getXXX(String fieldName);
/
根据查询结果中,字段所处的字段名获取对应数据,XXX是指定类型
/
–| boolean next();
/
判断当前查询结果集中是否还有可以键遍历操作的数据,如果没有。或则当前结果集中是无数据情况 Empty Set,直接返回fasle
*/
5. Statement操作SQL语句
5.1 Statement插入SQL数据操作
@Test
public void testInsert() {
Statement statement = null;
Connection connection = null;
try {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 准备必要的连接数据
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true";
String user = "root";
String password = "123456";
//3. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 4. 获取Statement对象
statement = connection.createStatement();
// 5. 准备SQL语句
String sql = "insert into nzgp2001.user(userName, password) value ('老黑', '123456')";
// 6. 执行SQL语句
int affectedRows = statement.executeUpdate(sql);
System.out.println("affectedRows:" + affectedRows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 7. 关闭资源
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
5.2 Statement修改SQL数据操作
@Test
public void testUpdate() {
Statement statement = null;
Connection connection = null;
try {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 准备必要的连接数据
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true";
String user = "root";
String password = "123456";
//3. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 4. 获取Statement对象
statement = connection.createStatement();
// 5. 准备SQL语句
String sql = "update nzgp2001.user set userName ='航海中路彭于晏' where id = 1";
// 6. 执行SQL语句
int affectedRows = statement.executeUpdate(sql);
System.out.println("affectedRows:" + affectedRows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 7. 关闭资源
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
5.3 Statement删除SQL数据操作
@Test
public void testDelete() {
Statement statement = null;
Connection connection = null;
try {
// 1. 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 准备必要的连接数据
String url = "jdbc:mysql://localhost:3306/nzgp2001?useSSL=true";
String user = "root";
String password = "123456";
//3. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 4. 获取Statement对象
statement = connection.createStatement();
// 5. 准备SQL语句
String sql = "delete from user where id > 2";
// 6. 执行SQL语句
int affectedRows = statement.executeUpdate(sql);
System.out.println("affectedRows:" + affectedRows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 7. 关闭资源
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}