From d53a7bf4693193ce885434b7bea6ca202c8afed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= Date: Fri, 19 May 2023 17:50:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?jsp=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 --- .../517\344\275\234\344\270\232.md" | 203 ++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 "\350\224\241\345\230\211\344\271\220/517\344\275\234\344\270\232.md" diff --git "a/\350\224\241\345\230\211\344\271\220/517\344\275\234\344\270\232.md" "b/\350\224\241\345\230\211\344\271\220/517\344\275\234\344\270\232.md" new file mode 100644 index 0000000..a6d679c --- /dev/null +++ "b/\350\224\241\345\230\211\344\271\220/517\344\275\234\344\270\232.md" @@ -0,0 +1,203 @@ +`````java + MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```mysql + create database student_db charset utf8; + use student_db; + create table student ( + id int, + name varchar(10), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + + select * from student; + ``` + + + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + import java.sql.*; + + public class Select { + public static void main(String[] args) { + Connection conn = null; + Statement sta = null; + ResultSet res = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + String sql = "select * from student"; + sta = conn.createStatement(); + res = sta.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String name = res.getString("name"); + String sex = res.getString("sex"); + System.out.println(id+"--"+name+"--"+sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + }finally { + try { + if (res != null){ + res.close(); + } + if (sta != null){ + sta.close(); + } + if (conn != null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放错误"); + e.printStackTrace(); + } + } + } + } + + ``` + + + + 2. 添加功能 + + ```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"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "insert into student values (4,'王六','女')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + + + ``` + + + + 3. 修改功能 + + ```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/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "update student set sex = '男' where name = '王六'"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + + ``` + + + + 4. 删除功能 + + ```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"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "delete from student where id = 4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } +``` +```` +````` + -- Gitee From a1ce0aedaf8fa671cc09adb5391828e129bf78ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= Date: Mon, 22 May 2023 22:10:03 +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 --- ...14\346\254\241\344\275\234\344\270\232.md" | 529 ++++++++++++++++++ 1 file changed, 529 insertions(+) create mode 100644 "\350\224\241\345\230\211\344\271\220/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" diff --git "a/\350\224\241\345\230\211\344\271\220/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/\350\224\241\345\230\211\344\271\220/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b649d00 --- /dev/null +++ "b/\350\224\241\345\230\211\344\271\220/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,529 @@ +````java +### Test类(查) + +```java +import java.sql.*; + +public class Test { + public static void main(String[] args) { + //1.注册驱动 + ResultSet rst = null; + Statement smt = null; + Connection conn = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + + //2.获取连接对象 + String url ="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username ="root"; + String password = "root"; + + conn = DriverManager.getConnection(url, username, password); + + //3.编写SQL语句 + String sql = "select * from student"; + + //4.获取执行SQL的对象 + smt = conn.createStatement(); + + //5.用smt执行SQL + rst = smt.executeQuery(sql); + + //处理返回结果(遍历结果) + while(rst.next()){ + System.out.println(rst.getInt("id") + + "\t" + rst.getString("name") + + "\t " + rst.getString("sex")); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入失败!"); + } catch (SQLException e) { +// e.printStackTrace(); + System.out.println("sql语句出错!"); + } finally { + //7.释放资源 + try { + if(rst!=null){ + rst.close(); + } + if(smt!=null){ + smt.close(); + } + if(conn!=null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("释放资源出错"); + } + } + + } +} +``` + +### Insert类(增) + +```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 username = "root"; + String password = "root"; + + Connection conn = DriverManager.getConnection(url, username, password); + + //3.编写SQL语句 + String sql = "insert into student values(4,'小李','男')"; + + //4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + + //5.用smt执行SQL + int i = smt.executeUpdate(sql); + + //6.处理返回结果(遍历结果) + if(i!=0){ + System.out.println("添加成功!"); + System.out.println("添加了"+i+"条数据"); + }else{ + System.out.println("添加了"+i+"条数据"); + System.out.println("添加失败!"); + } + //7.释放资源 + smt.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入失败!"); + } catch (SQLException e) { +// e.printStackTrace(); + System.out.println("sql语句出错!"); + } + + } +} +``` + +### Delete类(删) + +```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 username = "root"; + String password = "root"; + + Connection conn = DriverManager.getConnection(url, username, password); + + //3.编写SQL语句 + String sql = "delete from student where id=4"; + + //4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + + //5.用smt执行SQL + int i = smt.executeUpdate(sql); + + //6.处理返回结果(遍历结果) + if(i!=0){ + System.out.println("删除成功!"); + System.out.println("删除了"+i+"条数据"); + }else{ + System.out.println("删除了"+i+"条数据"); + System.out.println("删除失败!"); + } + //7.释放资源 + smt.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入失败!"); + } catch (SQLException e) { +// e.printStackTrace(); + System.out.println("sql语句出错!"); + } + } +} +``` + +### Update类(改) + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Scanner; + +public class Update { + 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 username = "root"; + String password = "root"; + + + Connection conn = DriverManager.getConnection(url, username, password); + + //3.编写SQL语句 + String sql = "update student set sex='女' where id=3"; + + //4.获取执行SQL的对象 + Statement smt = conn.createStatement(); + + //5.用smt执行SQL + int i = smt.executeUpdate(sql); + + //6.处理返回结果(遍历结果) + if (i != 0) { + System.out.println("修改成功!"); + System.out.println("修改了" + i + "条数据"); + } else { + System.out.println("修改了" + i + "条数据"); + System.out.println("修改失败!"); + } + //7.释放资源 + smt.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入失败!"); + } catch (SQLException e) { +// e.printStackTrace(); + System.out.println("sql语句出错!"); + } + } +} +``` + +### Mysql + +```mysql +create database student_db charset utf8; +use student_db; + +create table student( + id int, + name varchar(5), + sex varchar(1) +); + +insert into student values +(1,"张三","男"), +(2,"李四","女"), +(3,"王五","男"); +``` +\ No newline at end of file + +288 04 李明健/20230519 JDBC第二次作业 .md 0 → 100644 +### 作业 + +### 工具类 + +~~~ java +import java.sql.*; + +public class Utility { + 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 query(String sql, String... keys) { + //调用方法获取conn对象 + ResultSet rs = null; + try { + Connection conn = getConn(); + //获取执行SQL语句的对象 + PreparedStatement pst = conn.prepareStatement(sql); + //将值给对应的问号 + for (int i = 0; i < keys.length; i++) { + pst.setString(i + 1, keys[i]); + } + //执行SQL语句 + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("执行查询语句出错!"); + e.printStackTrace(); + } + return rs; + } + + //添加 + public static int add(String sql, String... keys){ + int num = getNum(sql, keys); + return num; + + } + + //删除 + public static int delete(String sql, String... keys) { + int num = getNum(sql, keys); + return num; + } + + //修改 + public static int Update(String sql, String... keys){ + int num = getNum(sql, keys); + return num; + } + + //获取执行成功了几条数据 + private static int getNum(String sql, String[] keys) { + int num = 0; + try { + Connection conn = getConn(); + //获取执行SQL语句的对象 + PreparedStatement pst = conn.prepareStatement(sql); + //将值给对应的问号 + for (int i = 0; i < keys.length; i++) { + pst.setString(i + 1, keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + System.out.println("执行删除语句出错!"); + } + return num; + } +} +~~~ + +### 学生类 + +~~~ java +public class Student { + private String id; + private String name; + private String sex; + + public Student() { + } + + public Student(String id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public String getId() { + return id; + } + + public void setId(String 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; + } +} +~~~ + +### 测试类 + +~~~ java +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + String a = null; + Scanner sc = new Scanner(System.in); + while (true) { + System.out.println("-----------欢迎来到学生数据库系统----------\n" + + "\t\t1.添加学生\n" + + "\t\t2.删除学生\n" + + "\t\t3.修改学生\n" + + "\t\t4.查询学生\n" + + "\t\t5.退出"); + + System.out.println("请输入你的选择:"); + String choose = sc.next(); + e: + switch (choose) { + case "1": + System.out.println("添加学生"); + System.out.println("请输入添加语句:"); + //将前面选择后的回车接收 + a = sc.nextLine(); + String addSql = sc.nextLine(); + add(addSql); + break; + case "2": + System.out.println("删除学生"); + System.out.println("请输入删除语句:"); + //将前面选择后的回车接收 + a = sc.nextLine(); + String delSql = sc.nextLine(); + delete(delSql); + break; + case "3": + System.out.println("修改学生"); + System.out.println("请输入修改语句:"); + //将前面选择后的回车接收 + a = sc.nextLine(); + String upSql = sc.nextLine(); + Update(upSql); + break; + case "4": + System.out.println("查询学生"); + System.out.println("请输入查询语句:"); + //将前面选择后的回车接收 + a = sc.nextLine(); + String quSql = sc.nextLine(); + + //创建一个集合用来备份 + ArrayList stuArr = query(quSql); + System.out.println("集合中的数据为:"); + for (int i = 0; i < stuArr.size(); i++) { + Student stu = stuArr.get(i); + System.out.println("编号:"+stu.getId()+"\t姓名:"+stu.getName()+"\t性别:"+stu.getSex()); + } + break; + case "5": + System.out.println("退出"); + break e; + default: + System.out.println("请输入正确的选项!"); + } + } + + //编写SQL语句 + //String sql ="select * from student"; + } + + //添加 + public static void add(String sql) { + int i = Utility.add(sql); + if (i > 0) { + System.out.println("添加成功!"); + } else { + System.out.println("添加失败!"); + } + } + + //删除 + public static void delete(String sql) { + int i = Utility.delete(sql); + if (i > 0) { + System.out.println("删除成功!"); + } else { + System.out.println("删除失败!"); + } + } + + //修改 + public static void Update(String sql) { + int i = Utility.Update(sql); + if (i > 0) { + System.out.println("修改成功!"); + } else { + System.out.println("修改失败!"); + } + } + + //查询 + public static ArrayList query(String sql) { + ArrayList stuArr = new ArrayList(); + //获取执行SQL对象 + ResultSet rs = Utility.query(sql); + System.out.println("数据库中的数据为:"); + //处理返回结果 + try { + while (rs.next()) { + String id = rs.getString(1); + String name = rs.getString(2); + String sex = rs.getString(3); + System.out.println("编号:" + id + "\t姓名:" + name + "\t性别:" + sex); + + Student stu = new Student(); + stu.setId(id); + stu.setName(name); + stu.setSex(sex); + stuArr.add(stu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return stuArr; + } +} +~~~ + +### Mysql + +~~~ mysql +create database student_db charset utf8; +use student_db; + +create table student( + id int, + name varchar(5), + sex varchar(1) +); + +insert into student values +(1,"张三","男"), +(2,"李四","女"), +(3,"王五","男"); +~~~ +```` + -- Gitee