JDBC
原理
JDBC是一种执行SQL语句的JavaAPI,为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
所以我们就是要学习JDBC规范来应用这些jar包里面的类。
JDBC访问数据库编码步骤:
1、加载驱动Driver
2、创建数据库连接Connection
3、创建一个发送Sql的发送器Statement
4、通过Statement发送sql语句
5、处理sql语句的结果
6、关闭资源 ResultSet Statement Connection
具体代码步骤
首先我们要先导入lib包才能用里面的类,或者导入依赖
增加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.bitzh;
import java.sql.*;
public class testJDBC { public static void main(String[] args) throws SQLException { Driver driver = new com.mysql.cj.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://127.0.0.1:3306/mytestdb3?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url,user,password); Statement statement = connection.createStatement(); String sql = "insert into user values(2,'小红','深圳莆田','女')"; int rows = statement.executeUpdate(sql); System.out.println("影响的数据:"+rows); statement.close(); connection.close(); } }
|
删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.bitzh;
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.Statement;
public class testJDBC2 { public static void main(String[] args) throws Exception { Driver driver = new com.mysql.cj.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://127.0.0.1:3306/mytestdb3?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); String sql = "delete from user where id = 2"; int i = statement.executeUpdate(sql); System.out.println("影响的数据"+i); statement.close(); connection.close(); } }
|
修改数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.bitzh;
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.Statement;
public class testJDBC3 { public static void main(String[] args) throws Exception{ Driver driver = new com.mysql.cj.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://127.0.0.1:3306/mytestdb3?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url,user,password); Statement statement = connection.createStatement(); String sql = "update user set username='小明' where id = 1"; int i = statement.executeUpdate(sql); System.out.println("影像数据"+i); statement.close(); connection.close();
} }
|
查询数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.bitzh;
import java.sql.*;
public class testJDBC4 { public static void main(String[] args) throws Exception{ Driver driver = new com.mysql.cj.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://127.0.0.1:3306/mytestdb3?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url,user,password); Statement statement = connection.createStatement(); String sql = "select * from user"; ResultSet resultSet = statement.executeQuery(sql); while(resultSet.next()){ int id = resultSet.getInt("id"); String username = resultSet.getString("username"); String address = resultSet.getString("address"); String gender = resultSet.getString("gender"); System.out.println("id:"+id+"\t"+"username:"+username+"\t"+"address:"+address+"\t"+"gender:"+gender); } resultSet.close(); statement.close(); connection.close(); } }
|