From 4736f406dda4fb3c5c1f68fb2ecfbbccda98e474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=98=E6=96=87=E8=AF=9A?= <3287861587@qq.com> Date: Wed, 17 May 2023 18:09:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?5=E6=9C=8816=E5=8F=B7=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBC\344\275\234\344\270\232.md" | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 "08 \345\256\230\346\226\207\350\257\232/20230517 JDBC\344\275\234\344\270\232.md" diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230517 JDBC\344\275\234\344\270\232.md" "b/08 \345\256\230\346\226\207\350\257\232/20230517 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..1cc16c0 --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230517 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,207 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + + ~~~ sql + CREATE database student_db charset utf8; + use student_db; + ~~~ + + + +2. 库中创建student表 + + ~~~ sql + CREATE TABLE student( + id int, + `name` varchar(20), + sex char + ); + ~~~ + + + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ~~~ sql + INSERT into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); + ~~~ + + + +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"; + String username="root"; + String password="root"; + conn = DriverManager.getConnection(url, username, password); + String sql="select * from student"; // sql语句 + 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("驱动包导入错误"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("语法错误"); + e.printStackTrace(); + } + try { + if (res!=null){ + res.close();} + if (sta!=null){ + sta.close();} + if(conn!=null){ + conn.close();} + } catch (Exception e) { + System.out.println("资源释放异常"); + } + + } + } + ~~~ + + 2. 添加功能 + + ~~~ java + import java.sql.*; + + 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,'子豪','男')";// sql语句 + 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("语法错误"); + } + + + } + } + + ~~~ + + 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 name='叶子豪' where id=4";// sql语句 + 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("语法错误"); + } + } + } + + ~~~ + + + + 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";// sql语句 + 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("语法错误"); + } + } + } + + ~~~ + + + + \ No newline at end of file -- Gitee From 4ae7e196f734420598b0ac3eff04d148c34582c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=98=E6=96=87=E8=AF=9A?= <3287861587@qq.com> Date: Mon, 22 May 2023 23:44:47 +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 --- ...01\350\243\205\345\220\210\351\233\206.md" | 262 ++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 "08 \345\256\230\346\226\207\350\257\232/20230522 \345\260\201\350\243\205\345\220\210\351\233\206.md" diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230522 \345\260\201\350\243\205\345\220\210\351\233\206.md" "b/08 \345\256\230\346\226\207\350\257\232/20230522 \345\260\201\350\243\205\345\220\210\351\233\206.md" new file mode 100644 index 0000000..9972d72 --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230522 \345\260\201\350\243\205\345\220\210\351\233\206.md" @@ -0,0 +1,262 @@ +作业 + +工具类 + +~~~ java +import java.sql.*; + +public class student { + // 封装 + private static final String url="jdbc:mysql://localhost:3306/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; + } + + // 一个通用查询方法,接收SQL语句,返回结果集 +public static ResultSet chaxun(String sql, String... keys){ + ResultSet rs=null; + try { + Connection conn=getconn(); + //建立预编译SQl的对象 + PreparedStatement prt = conn.prepareStatement(sql); + //给每个变量赋值 + //遍历可变参数 + for (int i = 0; i < keys.length; i++) { + prt.setString((i+1),keys[i]); + } + // 执行SQl + rs = prt.executeQuery(); + + } catch (SQLException e) { + System.out.println("执行异常"); + e.printStackTrace(); + } + return rs; +} +//一个通用更新的方法(删除,添加,修改) + public static int update(String sql, String... keys) { + PreparedStatement prt = null; + try { + int num = 0; + + try { + Connection conn = getconn(); + //建立预编译SQl的对象 + prt = conn.prepareStatement(sql);//将?号转成变量 + //给每个变量赋值 + //遍历可变参数 + for (int i = 0; i < keys.length; i++) { + prt.setString((i + 1), keys[i]); + } + //执行SQL + + num = prt.executeUpdate(); + } catch (SQLException e) { + System.out.println("执行异常"); + } + return num; + } catch (Exception e) { + throw new RuntimeException(e); + + }finally { + try { + prt.close(); + getconn().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } + public static int insert(String sql,String ... keys){ + int num = 0; + PreparedStatement pre = null; + try { + pre = null; + Connection con = getconn(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + + } catch (SQLException e) { + System.out.println("执行异常"); + } finally { + try { + pre.close(); + getconn().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return num; + } + public static int delete(String sql,String...keys) {//删除 + int num = 0; + PreparedStatement pre = null; + try { + Connection con = getconn(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + + } catch (SQLException e) { + System.out.println("执行异常"); + } finally { + try { + pre.close(); + getconn().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + return num; + } + } + + + +~~~ + + + +~~~ java +public class Xue { + private int id; + private String name; + private String sex; + + public Xue(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public Xue() { + } + + 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; + } +} + +~~~ + +测试 + +1 + +~~~ java +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class test { + public static void main(String[] args) { + Connection conn =student.getconn(); + + ArrayList list=new ArrayList<>(); + + String sql="select * from student"; + + ResultSet re=student.chaxun(sql); + Xue stu=new Xue(); + try { + while (re.next()){ + int id= re.getInt(1); + String name=re.getString(2); + String sex= re.getString(3); + stu.setId(id); + stu.setName(name); + stu.setSex(sex); + System.out.println(id+"-"+name+"-"+sex); + + } + } catch (SecurityException e) { + System.out.println("执行查询异常"); + e.printStackTrace(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + System.out.println(list); + + } +} + + + +~~~ + +2 + +~~~ java +import java.sql.ResultSet; +import java.util.ArrayList; + +public class test2 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "delete from student where name='张三'"; + int rst = student.delete(sql); + try { + System.out.println("修改成功"); + } catch (Exception e) { + e.printStackTrace(); + } + for (int i = 0; i