From dd5af1b90053910fade6d9f8d487fdac11ed82f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BD=B3=E7=82=9C?= <626104790@qq.com> Date: Wed, 17 May 2023 22:38:08 +0800 Subject: [PATCH 1/3] =?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 --- ...02\345\270\270\344\275\234\344\270\232.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" diff --git "a/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" "b/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8964c38 --- /dev/null +++ "b/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" @@ -0,0 +1,42 @@ +# 题目 + +键盘输入两个数,判断是否是数字(不能 为负数),如果是两数相加。如果是数字以外的字符就抛出异常。 + +```java +package yic; + +import java.util.Scanner; + +public class text { + public static void main(String[] args) { + Scanner sz = new Scanner(System.in); + Scanner sz2 = new Scanner(System.in); + try { + style(sz, sz2); + } catch (Exception e) { + System.out.println("不能输入与实际不符的数字"); + } + } + private static void style(Scanner sz,Scanner sz2){ + while (true){ + try{ + System.out.println("请输入第一个数字"); + double s1 = Double.parseDouble(sz.next()); + System.out.println("请输入第二个数字"); + double s2 = Double.parseDouble(sz2.next()); + if(s1>0 && s2>0){ + System.out.println(s1+"+"+s2+"="+(s1+s2)); + return; + } else { + System.out.println("数字不能为负数"); + System.out.println("-------------"); + } + }catch (Exception e){ + throw new RuntimeException(e); + } + } + } +} + +``` + -- Gitee From 63fe2a916fe6aef2ffd9b3202b92384d142b00a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BD=B3=E7=82=9C?= <626104790@qq.com> Date: Wed, 17 May 2023 23:23:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBS\344\275\234\344\270\232.md" | 215 ++++++++++++++++++ ...02\345\270\270\344\275\234\344\270\232.md" | 42 ---- 2 files changed, 215 insertions(+), 42 deletions(-) create mode 100644 "26 \351\231\210\344\275\263\347\202\234/20230517 JDBS\344\275\234\344\270\232.md" delete mode 100644 "26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" diff --git "a/26 \351\231\210\344\275\263\347\202\234/20230517 JDBS\344\275\234\344\270\232.md" "b/26 \351\231\210\344\275\263\347\202\234/20230517 JDBS\344\275\234\344\270\232.md" new file mode 100644 index 0000000..00dc270 --- /dev/null +++ "b/26 \351\231\210\344\275\263\347\202\234/20230517 JDBS\344\275\234\344\270\232.md" @@ -0,0 +1,215 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + ~~~ sql + create database student_db charset utf8; + use student_db; + create table student( + id int, + name varchar(20), + sex varchar(5) + ); + alter table student charset utf8; + insert into student values + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + ~~~ + + + + ~~~ java + // 查询测试类 + import java.sql.*; + import java.util.concurrent.Callable; + + public class Test { + public static void main(String[] args) { + Connection conn = null; + Statement smt = null; + ResultSet rst = null; + // 1.建立驱动 + try { + Class.forName("com.mysql.jdbc.Driver"); + // 2.获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String uesrname = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, uesrname, password); + // 3.定义sql语言 + String sql = "select * from student"; + // 4.获取执行SQL的对象 + smt = conn.createStatement(); + // 5.执行SQL + rst = smt.executeQuery(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出错"); + } + // 7.释放资源 + try { + if (conn!=null){ + conn.close(); + } + if (smt!=null){ + smt.close(); + } + if (rst!=null){ + rst.close(); + } + } catch (SQLException e) { + System.out.println("释放资源出错"); + } + } + } + + ~~~ + + ~~~ java + // 添加测试类 + import java.sql.*; + + public class Insert { + public static void main(String[] args) { + // 1.建立驱动 + try { + Class.forName("com.mysql.jdbc.Driver"); + // 2.获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String uesrname = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, uesrname, password); + // 3.定义sql语言 + String sql = "insert into student values (4,'赵六','女')"; + // 4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + // 5.执行SQL + int i = smt.executeUpdate(sql); + // 6.处理返回结果 + if (i>0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败"); + } + } 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 Delete { + public static void main(String[] args) { + // 1.建立驱动 + try { + Class.forName("com.mysql.jdbc.Driver"); + // 2.获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String uesrname = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, uesrname, password); + // 3.定义sql语言 + String sql = "delete from student where id=4"; + // 4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + // 5.执行SQL + int i = smt.executeUpdate(sql); + // 6.处理返回结果 + if (i>0){ + System.out.println("删除成功"); + }else{ + System.out.println("删除失败"); + } + } 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"); + // 2.获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String uesrname = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, uesrname, password); + // 3.定义sql语言 + String sql = "update student set sex='女' where id=3"; + // 4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + // 5.执行SQL + int i = smt.executeUpdate(sql); + // 6.处理返回结果 + if (i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("编译出错"); + } catch (SQLException e) { + System.out.println("SQL出错"); + } + } + } + + ~~~ + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" "b/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" deleted file mode 100644 index 8964c38..0000000 --- "a/26 \351\231\210\344\275\263\347\202\234/230509\345\274\202\345\270\270\344\275\234\344\270\232.md" +++ /dev/null @@ -1,42 +0,0 @@ -# 题目 - -键盘输入两个数,判断是否是数字(不能 为负数),如果是两数相加。如果是数字以外的字符就抛出异常。 - -```java -package yic; - -import java.util.Scanner; - -public class text { - public static void main(String[] args) { - Scanner sz = new Scanner(System.in); - Scanner sz2 = new Scanner(System.in); - try { - style(sz, sz2); - } catch (Exception e) { - System.out.println("不能输入与实际不符的数字"); - } - } - private static void style(Scanner sz,Scanner sz2){ - while (true){ - try{ - System.out.println("请输入第一个数字"); - double s1 = Double.parseDouble(sz.next()); - System.out.println("请输入第二个数字"); - double s2 = Double.parseDouble(sz2.next()); - if(s1>0 && s2>0){ - System.out.println(s1+"+"+s2+"="+(s1+s2)); - return; - } else { - System.out.println("数字不能为负数"); - System.out.println("-------------"); - } - }catch (Exception e){ - throw new RuntimeException(e); - } - } - } -} - -``` - -- Gitee From 2a493ef25dbf8c7875857c639f4c3a1b40a66f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BD=B3=E7=82=9C?= <626104790@qq.com> Date: Mon, 22 May 2023 11:04:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=81=E8=A3=85=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdbc\345\260\201\350\243\205.md" | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 "26 \351\231\210\344\275\263\347\202\234/jdbc\345\260\201\350\243\205.md" diff --git "a/26 \351\231\210\344\275\263\347\202\234/jdbc\345\260\201\350\243\205.md" "b/26 \351\231\210\344\275\263\347\202\234/jdbc\345\260\201\350\243\205.md" new file mode 100644 index 0000000..fcdaf7a --- /dev/null +++ "b/26 \351\231\210\344\275\263\347\202\234/jdbc\345\260\201\350\243\205.md" @@ -0,0 +1,208 @@ +```java +import java.sql.*; + +public class text { + private static String url = "jdbc:mysql:///student_db?useSSL=false&characterEncoding=utf8"; + private static final String user = "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, user, password); + } catch (SQLException e) { + System.out.println("获取连接对象失败"); + e.printStackTrace(); + } + return conn; +} + +public static ResultSet query(String sql, String... keys) { + ResultSet rs = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("执行查询异常"); + e.printStackTrace(); + } + return rs; +} +} +``` +# 查询测试类 + +```java +public class Test2 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>();//集合 + String sql="select * from studen"; + ResultSet que = Testt.query(sql); + try { + while (que.next()){ + String id=que.getString("id"); + String name=que.getString("name"); + String sex= que.getString("sex"); + Studen stu = new Studen(id, name, sex); + list.add(stu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + for (int i = 0; i list = new ArrayList<>(); + String sql="update studen set name='波仔' where id=1"; + int que = ZSG.query(sql); + try { + System.out.println("修改成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + for (int i = 0; i