From 7bb5e3ba06251064cb1ae658bc4a70bce32e7b41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B5=B7=E7=91=9E?= <3148024859@qq.com> Date: Thu, 18 May 2023 11:10:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230516 JDBC.md" | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 "50 \345\274\240\350\265\267\347\221\236/20230516 JDBC.md" diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230516 JDBC.md" "b/50 \345\274\240\350\265\267\347\221\236/20230516 JDBC.md" new file mode 100644 index 0000000..ccbbc13 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230516 JDBC.md" @@ -0,0 +1,190 @@ +```java + +import java.sql.*; + +public class Test { + public static void main(String[] args) { +// 0 导包 +// 1 注册驱动 + Connection conn = null; + Statement smt = null; + ResultSet rst= null; + try { + Class.forName("com.mysql.jdbc.Driver"); +// 2 获取连接的对象 + String url = "jdbc:mysql://localhost:3306/student_bb?useSSL=false";//jdbc:mysql://主机地址:端口号/数据库名称 + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); +// 3 编写sql语句 + String sql = "select * from student";//DQL + +// 4 获取执行sql的对象 + smt = conn.createStatement(); +// 5 smt 执行sql + rst = smt.executeQuery(sql); + // 除了查询,还有DDL,DML,insert,update==>smt.executeUpdate(SQL) + //6.处理返回的结果 + + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + String sex= rst.getString("sex"); + System.out.println(id + "+:" + name + "-" + sex); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + e.printStackTrace(); + } finally { + // 7 释放资源 + try { + if (rst!=null){ + rst.close(); + } + if (smt!=null){ + smt.close(); + } + if (conn!=null){ + conn.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 delete { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); +// 2 获取连接的对象 + String url = "jdbc:mysql://localhost:3306/student_bb?useSSL=false&useUnicode=true&characterEncoding=utf8";//jdbc:mysql://主机地址:端口号/数据库名称 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); +// 3 编写sql语句 + String sql = "delete from student where id=3"; +// 4.获取执行sql的对象 + Statement st =conn.createStatement(); +// 5 执行SQL + int i= st.executeUpdate(sql);//除了查询语句返回的是结果集,其他的语句都是返回受影响的行数 +// 6 处理返回的结果 + if(i>0){ + System.out.println("删除成功"); + System.out.println("删除了"+i+"行"); + }else{ + System.out.println("删除失败"); + System.out.println("删除了"+i+"行"); + } +// 释放资源 + st.close(); + conn.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 insert { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); +// 2 获取连接的对象 + String url = "jdbc:mysql://localhost:3306/student_bb?useSSL=false&useUnicode=true&characterEncoding=utf8";//jdbc:mysql://主机地址:端口号/数据库名称 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); +// 3 编写sql语句 + String sql = "insert into student values(3,'王大强','男')"; +// 4 获取执行sql的对象 + Statement st = conn.createStatement(); +// 5 执行sql + int i = st.executeUpdate(sql); +// 6 处理返回值 + if (i > 0) { + System.out.println("插入成功"); + } else { + System.out.println("失败"); + } +// 7 释放 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```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"); +// 2 获取连接的对象 + String url = "jdbc:mysql://localhost:3306/student_bb?useSSL=false&useUnicode=true&characterEncoding=utf8";//jdbc:mysql://主机地址:端口号/数据库名称 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); +// 3 编写sql语句 + String sql ="update student set name='李大明' where id=1"; +// 获取执行sql语句的对象 + Statement st=conn.createStatement(); +// 5 通过st执行sql语句 + int i=st.executeUpdate(sql); +// 6 处理返回的结果 + if(i>0){ + System.out.println("成功修改了"+i+"行"); + }else{ + System.out.println("修改了"+i+"行"); + System.out.println("没东西被修改"); + } +// 7 释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + -- Gitee From 9294f3de7b099f9ff4d2f336479e5de03943e580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B5=B7=E7=91=9E?= <3148024859@qq.com> Date: Mon, 22 May 2023 23:37:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\243\205\351\233\206\345\220\210.md" | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 "50 \345\274\240\350\265\267\347\221\236/20230518 \345\260\201\350\243\205\351\233\206\345\220\210.md" diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230518 \345\260\201\350\243\205\351\233\206\345\220\210.md" "b/50 \345\274\240\350\265\267\347\221\236/20230518 \345\260\201\350\243\205\351\233\206\345\220\210.md" new file mode 100644 index 0000000..ac515be --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230518 \345\260\201\350\243\205\351\233\206\345\220\210.md" @@ -0,0 +1,212 @@ +将id、name、sex封装起来,并放在集合里实现功能 + +```java +public class Test01{ + private int id; + private String name; + private String sex; + + public Test01() { + } + + public Test01(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + @Override + public String toString() { + return "序号:" + id + + ",姓名:" + name + + ",性别:" + sex ; + } +``` + +```java +public class DBUtil { + private static final String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); + e.printStackTrace(); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("连接异常,需要检查"); + e.printStackTrace(); + } + return conn; + + } + public static ResultSet select(String select, Object ...values){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement stmt = conn.prepareStatement(select); + for (int i = 0; i < values.length; i++) { + stmt.setObject((i+1),values[i]); + } + rs = stmt.executeQuery(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } + return rs; + } + public static int update(String sql,Object ...values){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement stmt = conn.prepareStatement(sql); + for (int j = 0; j < values.length; j++) { + stmt.setObject((j+1),values[j]); + } + num = stmt.executeUpdate(); + } catch (SQLException e) { + System.out.println("SQL语句异常"); + e.printStackTrace(); + } + return num; + } +} +``` + +```java +public class Test02{ + static Scanner sc = new Scanner(System.in); + static Connection conn = DBUtil.getConn(); + public static void main(String[] args) { + out: + while (true) { + System.out.print("=========操作序号列表========\n" + + "1.查询信息\n" + + "2.添加功能\n" + + "3.修改功能\n" + + "4.删除功能\n" + + "5.退出系统\n" + + "输入序号进行对应操作:"); + int i = 0; + try { + i = sc.nextInt(); + } catch (Exception e) { + System.out.println("非法字符!请按要求输入!"); + e.printStackTrace(); + } + switch (i){ + case 1: + selectTest01(); + break; + case 2: + addTest01(); + break; + case 3: + updateTest01(); + break; + case 4: + deleteTest01(); + break; + case 5: + try { + conn.close(); + } catch (SQLException e) { + System.out.println("SQL执行错误"); + e.printStackTrace(); + } + System.out.println("退出,请再次登录"); + break out; + default: + System.out.println("请按要求进行操作!!"); + } + } + } + + private static void deleteTest01() { + System.out.print("请输入要删除的学生学号:"); + int id = sc.nextInt(); + int i = DBUtil.update("delete from student where id = ?",id); + if (i>0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + } + + private static void updateTest01() { + System.out.print("请输入要修改的学号:"); + int id = sc.nextInt(); + System.out.print("请输入要修改的姓名:"); + String name = sc.next(); + System.out.print("请输入要修改的性别:"); + String sex = sc.next(); + int i=DBUtil.update("update student set name =?,sex=? where id=? ",name,sex,id); + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + } + + private static void addTest01() { + System.out.print("请输入学号:"); + int id = sc.nextInt(); + System.out.print("请输入姓名:"); + String name = sc.next(); + System.out.print("请输入性别(男或女):"); + String sex = sc.next(); + int i = DBUtil.update("insert into student values (?,?,?)",id,name,sex); + if (i>0){ + System.out.println("添加成功!"); + } + } + + private static void selectTest01() { + ResultSet resultSet = DBUtil.select("select * from student"); + ArrayList list = new ArrayList<>(); + try { + while (resultSet.next()){ + list.add(new Test01(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + for (int i = 0; i < list.size(); i++) { + Test01 test01 = list.get(i); + System.out.println(test01); + } + } +} +``` + -- Gitee