From 6a684b83b3ff2e4820500f85926f380b1d786ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Thu, 18 May 2023 22:46:39 +0800 Subject: [PATCH 1/2] JDBC --- .../20230517.md" | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 "29 \347\216\213\351\233\257\351\235\231/20230517.md" diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230517.md" "b/29 \347\216\213\351\233\257\351\235\231/20230517.md" new file mode 100644 index 0000000..e519d38 --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230517.md" @@ -0,0 +1,186 @@ + CREATE DATABASE student_db charset utf8; + USE student_db; + CREATE table student( + id int, + name VARCHAR(20), + sex enum("男","女") + + ); + INSERT INTO student VALUES + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + + +```javascript + +//查询功能,查询student中所有数据 +import java.sql.*; + +public class select { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="select*from Student"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + ResultSet re = stmt.executeQuery(sql); + + //处理返回结果 + while (re.next()){ + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+name+sex); + } + //释放资源 + re.close(); + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} + +//添加功能 +import java.sql.*; + +public class add { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="insert into student values(4,'傻六','女')"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("增加了"+i+"行"); + }else{ + System.out.println("添加错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} +//修改功能 + +import java.sql.*; + +public class change { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="UPDATE student set sex=\"男\"where id=2"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("修改了"+i+"行"); + }else{ + System.out.println("修改错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} +// 删除功能 +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 { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="DELETE from student WHERE id =4"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("删除了"+i+"行"); + }else{ + System.out.println("删除错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + + + + + + +``` + -- Gitee From b07b820936ba78a85e43d87c2e2f689625a09af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Sun, 21 May 2023 22:19:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?JDBC=E4=BD=9C=E4=B8=9A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230520\344\275\234\344\270\232.md" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" "b/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" new file mode 100644 index 0000000..9209905 --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" @@ -0,0 +1,105 @@ +```java + +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历这个集合的对象 + +public class Student { + private int id; + private String name; + private String sex; + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public Student() { + } + + 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; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} + +``` + +```java +import java.sql.*; +import java.util.ArrayList; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&character=utf8", "root", "122319"); + return conn; + } + public static ResultSet Query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + ResultSet res = pre.executeQuery(); + return res; + } + public static void close(Connection con,PreparedStatement pre,ResultSet res) throws SQLException { + if(res!=null){ + res.close(); + }if(pre!=null){ + pre.close(); + }if(con!=null){ + con.close(); + } + } +} +class Test{ + public static void main(String[] args) throws SQLException { + String sql="select * from student"; + ResultSet res = DBUtil.Query(sql); + ArrayList list = new ArrayList<>(); + while (res.next()){ + list.add(new Student(res.getInt(1),res.getString(2),res.getString(3))); + } + for (int i = 0; i < list.size(); i++) { + Student student = list.get(i); + System.out.println(student); + } + DBUtil.close(null,null,res); + } +} +``` -- Gitee