JDBC就是使用Java语言操作关系型数据库的一个API
JDBC以jar包形式存在,相当于用Java操作各种数据库的驱动
//注册驱动
class.forName("com.mysql.jdbc.Driver");
//获取连接对象
String url = "jdbc:mysql://127.0.0.1:3306/db1?useSSL=false";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url,username,password);
//定义sql
String sql = "update account set money = 2000 where id = 1";
//获取执行sql对象
Statement stmt = conn.createStatement();
//执行sql
int count = stmtexecuteUpdate(sql);
//处理结果
//sout(count);
//释放资源
stmt.close();
conn.close();
创建一个maven项目(因为maven能导包)
然后在pom.xml里面添加以下依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
刷新一下maven就帮你导入了
这样子的话我们的jar包都是在外部依赖里的,而不是需要自己创建的lib里
package JDBC;
import com.mysql.jdbc.Driver;
import java.sql.DriverManager;
//jdbc快速入门
public class demo {
//psvm+抛出异常
public static void main(String[] args)throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
String url="";
String username="root";
String password="******";
DriverManager.getConnection(url,username,password);
}
}
这里的url大家都应该看得懂的吧,最后一个是数据库名(shema)
然后再对最后一行进行ctrl+alt+v实例化,造出一个变量connection
Connection connection = DriverManager.getConnection(url, username, password);
然后就是写要操作的sql语句了
String sql = "INSERT INTO java_study.jdbc (user_id, user_name, user_num, user_gender)\n" + "VALUES (1, 'Hoorus', '017', 'male');";
发现报时间的错,一看time zone就是jdbc驱动版本太高(mysql8必须要时间域,mysql5不要),那就在xml里面配个5版本的就解决了
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
其实你也可以保留8版本的高驱动,那么就要在url后面加时区参数了
这样的话高版本也是能成功的
然后我们就抛出了1,说明结果成功
这里,我们就实现了用java去操作数据库
mysql5之后可以不用注册驱动了
当然,如果是连接本机的mysql3306,那么可以url缩写为
jdbc:mysql://数据库名称参数键值对
获取sql对象
开事务来执行sql事务语句
选中语句执行,ctrl+alt+t来组织try&cahch
用事务的好处,报错会回滚,不会执行一半就不执行了
执行sql语句
excuteUpdate(sql)//执行DML,DDL语句
ResultSet excuteQuery(sql)//执行DQL语句
在pom文件里面加上此包
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
public class demo2 {
@Test
public void testDML() throws Exception {
//注册驱动
//获取链接
String url = "jdbc:mysql://localhost:3306/java_study?useSSL=false&serverTimezone=GMT";
String username = "root";
String password = "******";
Connection connection = DriverManager.getConnection(url, username, password);
//定义sql语句
String sql1 = "UPDATE java_study.jdbc t\n" +
"SET t.user_num = '017'\n" +
"WHERE t.user_id = 1;\n";
String sql2 = "UPDATE java_study.jdbc t\n" +
"SET t.user_num = '005'\n" +
"WHERE t.user_id = 2;\n";
//获取执行sql的对象Statement
Statement statement = connection.createStatement();
//执行sql,开启事务
int count1 = statement.executeUpdate(sql1);
int count2 = statement.executeUpdate(sql2);
//处理结果
if (count1 == 1 && count2 == 1) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
//释放资源
statement.close();
connection.close();
}
}
然后对着@Test
来alt+enter来解决方法(导包),就可以运行了
作用:1、封装了DQL查询语句的结果 2、获取查询结果
boolean next();
将光标从当前位置向前移动一行,且判断当前行是否为有效行
Xxx getXxx(参数);
获取数据
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//jdbc快速入门
public class demo3 {
@Test
public void testResult() throws Exception {
//注册驱动
//获取链接
String url = "jdbc:mysql://localhost:3306/java_study?useSSL=false&serverTimezone=GMT";
String username = "root";
String password = "sylym321";
Connection connection = DriverManager.getConnection(url, username, password);
//定义sql
String sql="select * from java_study.jdbc where user_name like '%J%'";
//获取statement对象
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);//查询用excuteQuery
//处理结果,遍历resultSet中的所有数据
while (resultSet.next()){
int id = resultSet.getInt(1);//第1列
String name = resultSet.getString(2);//第2列
String number = resultSet.getString(3);//第3列
String gender = resultSet.getString(4);//第4列
System.out.print(id+" ");
System.out.print(name+" ");
System.out.print(number+" ");
System.out.println(gender);
System.out.println("---------------");
}
resultSet.close();
statement.close();
}
}
int id = resultSet.getInt("user_id");//第1列
String name = resultSet.getString("user_name");//第2列
String number = resultSet.getString("user_num");//第3列
String gender = resultSet.getString("user_gender");//第4列
pojo:专门创建数据表的对象从而更容易被其它层调用的文件夹
//demo页
import org.junit.jupiter.api.Test;
import pojo.Account;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//jdbc快速入门
public class demo4 {
@Test
public void testResult() throws Exception {
//注册驱动
//获取链接
String url = "jdbc:mysql://localhost:3306/java_study?useSSL=false&serverTimezone=GMT";
String username = "root";
String password = "sylym321";
Connection connection = DriverManager.getConnection(url, username, password);
//定义sql
String sql="select * from java_study.jdbc where user_name like '%J%'";
//获取statement对象
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
List<Account> arrayList = new ArrayList<>();//创建list只是为了把account类放进去,其实可以不加,这样sout就要循环输出了
//处理结果,遍历resultSet中的所有数据
while (resultSet.next()){
Account account = new Account();//每次循环新建一个account来存储当前行数据
int id = resultSet.getInt("user_id");//第1列
String name = resultSet.getString("user_name");//第2列
String number = resultSet.getString("user_num");//第3列
String gender = resultSet.getString("user_gender");//第4列
account.setUser_id(id);//把列放进account里
account.setUser_name(name);
account.setUser_num(number);
account.setUser_gender(gender);
arrayList.add(account);//再把刚刚的account放进list
}
System.out.println(arrayList);
resultSet.close();
statement.close();
}
}
//pojo页
package pojo;
public class Account {
private int user_id;
private String user_name;
private String user_num;
private String user_gender;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_num() {
return user_num;
}
public void setUser_num(String user_num) {
this.user_num = user_num;
}
public String getUser_gender() {
return user_gender;
}
public void setUser_gender(String user_gender) {
this.user_gender = user_gender;
}
@Override//重写,否则toString输出的是地址
public String toString() {
return "jdbc{" +
"user_id=" + user_id +
", user_name='" + user_name + '\'' +
", user_num='" + user_num + '\'' +
", user_gender='" + user_gender + '\'' +
'}';
}
}
//输出
[jdbc{user_id=3, user_name='Jack', user_num='002', user_gender='male'},
jdbc{user_id=4, user_name='Jim', user_num='007', user_gender='male'}]