From 06d2a80ad82bb223c617866b3c98ba8e13d2a2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AF=8C?= <2744975513@qq.com> Date: Wed, 17 May 2023 23:10:02 +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 --- ...BC\344\275\234\344\270\232\357\274\232.md" | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 "17 \345\221\250\345\257\214/JDBC\344\275\234\344\270\232\357\274\232.md" diff --git "a/17 \345\221\250\345\257\214/JDBC\344\275\234\344\270\232\357\274\232.md" "b/17 \345\221\250\345\257\214/JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000..ecc53cc --- /dev/null +++ "b/17 \345\221\250\345\257\214/JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,201 @@ +### 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"; + 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,'赵六','男')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + + } 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"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + + } 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 name='小民'"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动错误"); + } catch (SQLException e) { + System.out.println("语法错误"); + } + } + } + + ~~~ + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From 7fbd54e4d1a49a92b8d83731b3b39374b2c0552a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=AF=8C?= <2744975513@qq.com> Date: Mon, 22 May 2023 23:37:39 +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 --- .../20230522 JDBS\344\275\234\344\270\232.md" | 198 ++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 "17 \345\221\250\345\257\214/20230522 JDBS\344\275\234\344\270\232.md" diff --git "a/17 \345\221\250\345\257\214/20230522 JDBS\344\275\234\344\270\232.md" "b/17 \345\221\250\345\257\214/20230522 JDBS\344\275\234\344\270\232.md" new file mode 100644 index 0000000..cb836e1 --- /dev/null +++ "b/17 \345\221\250\345\257\214/20230522 JDBS\344\275\234\344\270\232.md" @@ -0,0 +1,198 @@ +~~~ 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; + } + + @Override + public String toString() { + return "Test.student{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} + +~~~ + +~~~ java +// 包装类 +import java.sql.*; + +public class Dbutli { + 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("SQL数据异常"); + e.printStackTrace(); + } + return conn; + } + public static ResultSet query(String sql, String ...keys){ + ResultSet rst = null; + try { + Connection conn = getconn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + rst = pst.executeQuery(sql); + } catch (SQLException e) { + System.out.println("获取对象错误"); + e.printStackTrace(); + } + return rst; + } +} + +~~~ + +~~~ java +// 查询测试类 +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Test { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + try { + String sql = "select * from student"; + ResultSet rst = DBUtli.query(sql); + while(rst.next()){ + int id = rst.getInt(1); + String name = rst.getString(2); + String sex = rst.getString(3); + System.out.println("编号:"+id+",姓名:"+name+",性别:"+sex); + } + } catch (SQLException e) { + System.out.println("SQL语句错误"); + } + for (int i = 0; i list = new ArrayList<>(); + String sql = "delete from student where name='小明'"; + int rst = DubTil.query(sql); + try { + System.out.println("修改成功"); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} + + + +~~~ + -- Gitee