From bc96c9eb467ca632f818c57680bc99deaff5a1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E6=9D=8E=E6=8C=AF=E5=8D=8E=E2=80=9D?= <877611343@qq.com> Date: Wed, 17 May 2023 22:49:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\272\223JDBC\344\275\234\344\270\232.md" | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 "02 \346\235\216\346\214\257\345\215\216/20230516 JAVA\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223JDBC\344\275\234\344\270\232.md" diff --git "a/02 \346\235\216\346\214\257\345\215\216/20230516 JAVA\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223JDBC\344\275\234\344\270\232.md" "b/02 \346\235\216\346\214\257\345\215\216/20230516 JAVA\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8fd653b --- /dev/null +++ "b/02 \346\235\216\346\214\257\345\215\216/20230516 JAVA\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,247 @@ +# 增 + + + +```java + +import java.sql.*; + +public class Insert { + public static void main(String[] args) { + ResultSet rs = null; + Statement st = null; + Connection conn = null; + try { + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + + //3 编写SQL语句 + String sql = "insert into student VALUES(1,'张三','男'),(2,'李四','女'),(3,'王五','男')"; + //4 获取执行SQL的对象 + st = conn.createStatement(); + //5 执行SQL语句 + int i = st.executeUpdate(sql); + + //6 处理返回的结果 + if (i>0){ + System.out.println("成功添加了"+i+"行数"); + + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } finally { + //7 释放资源 + try { + + if (st!=null){ + st.close(); + } + if (conn!=null){ + conn.close(); + } + + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + +# 删 + + + +```java + +import java.sql.*; + +public class Delete { + public static void main(String[] args) { + ResultSet rs = null; + Statement st = null; + Connection conn = null; + try { + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + + //3 编写SQL语句 + String sql = "delete from student where id=2 or 3"; + //4 获取执行SQL的对象 + st = conn.createStatement(); + //5 执行SQL语句 + int i = st.executeUpdate(sql); + + //6 处理返回的结果 + if (i>0){ + System.out.println("成功删除了"+i+"行数"); + }else { + System.out.println("删除了"+i+"行数"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } finally { + //7 释放资源 + try { + + if (st!=null){ + st.close(); + } + if (conn!=null){ + conn.close(); + } + + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + +# 改 + + + +```java +import java.sql.*; + +public class Update { + public static void main(String[] args) { + ResultSet rs = null; + Statement st = null; + Connection conn = null; + try { + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + + //3 编写SQL语句 + String sql = "UPDATE student set name='张三' WHERE id=1;"; + //4 获取执行SQL的对象 + st = conn.createStatement(); + //5 执行SQL语句 + int i = st.executeUpdate(sql); + + //6 处理返回的结果 + if (i>0){ + System.out.println("成功修改了"+i+"行数"); + + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } finally { + //7 释放资源 + try { + + if (st!=null){ + st.close(); + } + if (conn!=null){ + conn.close(); + } + + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + +# 查 + + + +```java +import java.sql.*; + +public class Select { + public static void main(String[] args) { + ResultSet rs = null; + Statement st = null; + Connection conn = null; + try { + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql:///student_db?useSSL=false"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + + //3 编写SQL语句 + String sql = "SELECT * FROM student"; + //4 获取执行SQL的对象 + st = conn.createStatement(); + //5 执行SQL语句 + rs = st.executeQuery(sql); + + //6 处理返回的结果 + while (rs.next()) { + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id +"\t" +name +"\t\t"+ sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } finally { + //7 释放资源 + try { + if(rs!=null){ + rs.close(); + } + if (st!=null){ + st.close(); + } + if (conn!=null){ + conn.close(); + } + + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + -- Gitee