From 1c22e3f1243a1ef3a7593ac84532ad514a4e32e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Sat, 20 May 2023 12:01:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../230517jsp\344\275\234\344\270\232.md" | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 "45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" diff --git "a/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4dd8850 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" @@ -0,0 +1,141 @@ +```mysql +1. 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(5),sex char(2)); + insert into student values + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); +``` + +```java +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + + +import java.sql.*; + +// 1. 查询功能,查询student中所有数据 +public class Select { + 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 = "select * from student"; + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + while (re.next()) { + int id = re.getInt("id"); + String name = re.getString("name"); + String sex = re.getString("sex"); + System.out.println(id+","+name+","+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 2. 添加功能 +class Add { + 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 st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功添加了"+i+"行数据"); + }else { + System.out.println("添加不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 3. 修改功能 +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 id = 5 where id = 4"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功修改了"+i+"行数据"); + }else { + System.out.println("修改不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 4. 删除功能 +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 = 5"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功删除了"+i+"行数据"); + }else { + System.out.println("删除不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` -- Gitee From ddf0576561f3a98baaf127a2eafbf849b35a7568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Sun, 21 May 2023 22:46:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../230520jdbc\344\275\234\344\270\232.md" | 600 ++++++++++++++++++ 1 file changed, 600 insertions(+) create mode 100644 "45 \347\216\213\345\274\272/230520jdbc\344\275\234\344\270\232.md" diff --git "a/45 \347\216\213\345\274\272/230520jdbc\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230520jdbc\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4b845d3 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230520jdbc\344\275\234\344\270\232.md" @@ -0,0 +1,600 @@ +```java +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +//工具类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Arrays; + +public class tool{ + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String name = "root"; + static { + try{ + Class.forName("com.mysql.jdbc.Driver"); + } + catch (ClassNotFoundException e){ + e.printStackTrace(); + } + } + public static Connection getCon() throws SQLException + { + Connection con = DriverManager.getConnection(url,name,name); + return con; + } + public static ArrayList Select(String sql,String ...key) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + state.setString(i,key[i]); + } + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + public static ArrayList Select(String sql) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + + +} +//学生类 + +public class Student { + public Student(){} + public Student(int num,String name,String sex){ + this.setName(name); + this.setSex(sex); + this.setNum(num); + } + private int num; + private String name; + private String sex; + public String toString (){ + return "num = "+ getNum() +"\t name = "+ getName() +"\t sex = "+ getSex(); + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + 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; + } +} +//实现类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Scanner; + +import static java.lang.Class.forName; + +public class p1 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + try { + list = tool.Select(sql); + } + catch (SQLException e){ + e.printStackTrace(); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + } + + +``` +\ No newline at end of file + +184 23 黄浩东/20235017 java的jdbc基础.md 0 → 100644 +6. 1. JDBC作业: + + 1. MySQL中创建一个数据库student_db + + 2. 库中创建student表 + + 3. 表中数据如下 + + 4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```java + create database student_db charset utf8; + use student_db; + create table student ( + num int, + name varchar(20), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ``` + + 5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + import java.util.Scanner; + + import static java.lang.Class.forName; + + public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null ; + int resultSet = 0; + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(state,sc); + break ; + case 4: + System.out.println("添加"); + Add(state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + if (state != null) { + state.close(); + } + if (con !=null) { + con.close(); + } + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select( Statement state) throws SQLException{ + ResultSet res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } + res.close(); + } + public static void delete(Statement state,int num) throws SQLException{ + int res = state.executeUpdate("Delete from student where num ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(Statement state,Scanner sc) throws SQLException{ + int res; + int num = 0; + int all = 0; + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + all++; + if (result.getInt("num") != id){ + num++; + } + if (num == all){ + System.out.println("查无此人"); + result.close(); + return; + } + } + result.close(); + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\', " + + "sex ="+"\'"+sex+"\' where num ="+id); + + if (res >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("num") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } + } + + ```使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +```java +//工具类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Arrays; + +public class tool{ + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String name = "root"; + static { + try{ + Class.forName("com.mysql.jdbc.Driver"); + } + catch (ClassNotFoundException e){ + e.printStackTrace(); + } + } + public static Connection getCon() throws SQLException + { + Connection con = DriverManager.getConnection(url,name,name); + return con; + } + public static ArrayList Select(String sql,String ...key) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + state.setString(i,key[i]); + } + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + public static ArrayList Select(String sql) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + + +} +//学生类 + +public class Student { + public Student(){} + public Student(int num,String name,String sex){ + this.setName(name); + this.setSex(sex); + this.setNum(num); + } + private int num; + private String name; + private String sex; + public String toString (){ + return "num = "+ getNum() +"\t name = "+ getName() +"\t sex = "+ getSex(); + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + 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; + } +} +//实现类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Scanner; + +import static java.lang.Class.forName; + +public class p1 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + try { + list = tool.Select(sql); + } + catch (SQLException e){ + e.printStackTrace(); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + } + + +``` +\ No newline at end of file + +184 23 黄浩东/20235017 java的jdbc基础.md 0 → 100644 +6. 1. JDBC作业: + + 1. MySQL中创建一个数据库student_db + + 2. 库中创建student表 + + 3. 表中数据如下 + + 4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```java + create database student_db charset utf8; + use student_db; + create table student ( + num int, + name varchar(20), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ``` + + 5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + import java.util.Scanner; + + import static java.lang.Class.forName; + + public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null ; + int resultSet = 0; + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(state,sc); + break ; + case 4: + System.out.println("添加"); + Add(state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + if (state != null) { + state.close(); + } + if (con !=null) { + con.close(); + } + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select( Statement state) throws SQLException{ + ResultSet res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } + res.close(); + } + public static void delete(Statement state,int num) throws SQLException{ + int res = state.executeUpdate("Delete from student where num ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(Statement state,Scanner sc) throws SQLException{ + int res; + int num = 0; + int all = 0; + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + all++; + if (result.getInt("num") != id){ + num++; + } + if (num == all){ + System.out.println("查无此人"); + result.close(); + return; + } + } + result.close(); + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\', " + + "sex ="+"\'"+sex+"\' where num ="+id); + + if (res >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("num") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } + } + + +``` -- Gitee