From 249ac07bf419a50b9e6604d7b713f1b9bc6ad2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E4=BC=9F?= <2152890632@qq.com> Date: Thu, 18 May 2023 00:09:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\344\275\234\344\270\232.md" | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 "06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8c804b1 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" @@ -0,0 +1,167 @@ +# 查询 + +```java +import java.sql.*; + +public class Select { + public static void main(String[] args) { + //0.导包 + //1.注册驱动 + ResultSet rst = null; + Connection conn = null; + Statement smt = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3.编写sql语句 + String sql = "select * from biao"; + //4.获取执行sql的对象 + smt = conn.createStatement(); + //5.smt执行sql + rst = smt.executeQuery(sql); + //6.处理返回的结果 + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + double money = rst.getDouble("money"); + System.out.println(id + ":" + name + "-" + money); + } + //7.释放资源 + + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } finally { + try { + if (rst!=null){ + rst.close(); + } + if (conn!=null){ + conn.close(); + } + if (smt!=null) { + smt.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + +# 添加 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Inset { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="insert into biao values('黎明',6,1000)"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + +# 删除 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Delect { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="delete from biao where id = 6"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功"+i+"条"); + }else { + System.out.println("删除失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + +# 修改 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Update { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="update biao set name ='李明' where id = 6"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("修改成功"+i+"条"); + }else { + System.out.println("修改失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + -- Gitee