From 6ce5d344e4c25ed808b4b6cd5acaf835b5148e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?16=20=E9=98=99=E8=8B=8F=E6=96=87?= <2361635242@qq.com> Date: Thu, 18 May 2023 10:19:24 +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 --- .../20230518JDBC\344\275\234\344\270\232.md" | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 "16 \351\230\231\350\213\217\346\226\207/20230518JDBC\344\275\234\344\270\232.md" diff --git "a/16 \351\230\231\350\213\217\346\226\207/20230518JDBC\344\275\234\344\270\232.md" "b/16 \351\230\231\350\213\217\346\226\207/20230518JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..51e4f17 --- /dev/null +++ "b/16 \351\230\231\350\213\217\346\226\207/20230518JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,161 @@ +# 作业 + +```java +//查询 +import java.sql.*; + +public class Inquire { + 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&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.执行SQL + rst = smt.executeQuery(sql); +// 6.处理返回结果 + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + String sex = rst.getString("sex"); + System.out.println(id + "" + name + "" + sex); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + 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("资源释放异常"); + } + } + } +} + +``` + +```java +//添加 +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Add { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "insert into student values(4,'小六','男')"; + Statement se = conn.createStatement(); + int i = se.executeUpdate(sql); + if (i>0){ + System.out.println("插入成功"); + }else { + System.out.println("失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` + +```java +//修改 +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Amend { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "update student set name='张三' where id=1 "; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i>0){ + System.out.println("成功修改了"+i+"行"); + }else { + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` + +```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"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql ="delete from student where id=3"; + Statement ts = conn.createStatement(); + int i =ts.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功!"); + System.out.println("删除了"+i+"行"); + }else { + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` + -- Gitee From 330f9420f50ad9de6f600ebd4918789c1871f4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?16=20=E9=98=99=E8=8B=8F=E6=96=87?= <2361635242@qq.com> Date: Sun, 21 May 2023 22:54:59 +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\344\275\234\344\270\232.md" | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 "16 \351\230\231\350\213\217\346\226\207/20230521\345\260\201\350\243\205\344\275\234\344\270\232.md" diff --git "a/16 \351\230\231\350\213\217\346\226\207/20230521\345\260\201\350\243\205\344\275\234\344\270\232.md" "b/16 \351\230\231\350\213\217\346\226\207/20230521\345\260\201\350\243\205\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5bb705c --- /dev/null +++ "b/16 \351\230\231\350\213\217\346\226\207/20230521\345\260\201\350\243\205\344\275\234\344\270\232.md" @@ -0,0 +1,124 @@ +```java +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Scanner; +/** + * @author zhc + */ +public class test { + public static void main(String[] args) { + //获取连接 + Driver.conn(); + //注入SQL语句 + Driver.query("select * from student"); + //关闭连接 + Driver.close(); + //查看集合 + Driver.show(); + } +} +package java与数据库的连接.封装; + +import java.sql.*; +import java.util.ArrayList; + +/** + * @author zhc + */ +public class Driver { + private static final String Driver = "com.mysql.jdbc.Driver"; + private static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false&unicode=true&characterEncoding=utf8"; + private static final String user = "root"; + private static final String paw = "root"; + int id; + String name; + String sex; + + static void show() { + for (int i = 0; i < list.size(); i++) { + Driver driver = list.get(i); + int id = driver.id; + String name = driver.name; + String sex = driver.sex; + System.out.println("ID=" + id + ",name=" + name + ",sex=" + sex); + } + } + + //注册驱动 + static { + System.out.println("注册驱动中..."); + try { + Class.forName(Driver); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册失败"); + } + } + + public static Connection conn() { + System.out.println("连接数据库中..."); + Connection con = null; + try { + con = DriverManager.getConnection(URL, user, paw); + } catch (SQLException e) { + System.out.println("数据库连接失败"); + } + return con; + } + + static ArrayList list = new ArrayList<>(); + + public static void query(String sql, String... keys) { + System.out.println("查询中..."); + ResultSet rs = null; + int j = 0; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + rs = pst.executeQuery(sql); + while (rs.next()) { + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + System.out.printf("ID=%d,name=%s,sex=%s\n", id, name, sex); + Driver driver = new Driver(); + driver.id = id; + driver.name = name; + driver.sex = sex; + list.add(j++, driver); + } + } catch (SQLException e) { + System.out.println("查询失败"); + } + } + + public static void update(String sql, String... keys) { + System.out.println("更新中..."); + try { + PreparedStatement pst = conn().prepareStatement(sql, keys); + int i = pst.executeUpdate(sql); + if (i > 0) { + System.out.printf("变化了%d条\n", i); + } else { + System.out.printf("更新失败.变化%d条\n", i); + } + } catch (SQLException e) { + System.out.println("更新失败"); + } + } + + public static void close() { + System.out.println("释放资源中..."); + try { + conn().close(); + } catch (SQLException e) { + System.out.println("释放资源失败"); + } + System.out.println("释放资源成功"); + } + +} +``` + -- Gitee