From e1a533d4344b0c9d71c9d5b3232e56c9abe97090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BF=8A=E4=BC=9F?= <2421084001@qq.com> Date: Mon, 22 May 2023 11:21:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\254\241\344\275\234\344\270\232.md" | 75 +++++++++ ...14\346\254\241\344\275\234\344\270\232.md" | 157 ++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 "12 \346\236\227\344\277\212\344\274\237/0516 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "12 \346\236\227\344\277\212\344\274\237/0520 \347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" diff --git "a/12 \346\236\227\344\277\212\344\274\237/0516 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/12 \346\236\227\344\277\212\344\274\237/0516 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..7cdc30c --- /dev/null +++ "b/12 \346\236\227\344\277\212\344\274\237/0516 \347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,75 @@ + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class insert { + public static void main(String[] args) { + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "insert into Student value(1,'老王','男'),(2,'老表','男')"; + str = con.createStatement(); + int i = str.executeUpdate(sql); + if (i > 0) { + System.out.println("插入成功"); + } else { + System.out.println("插入失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入正常"); + } catch (SQLException e) { + System.out.println("mysql语法错误"); + } finally { + try { + str.close(); + con.close(); + } 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 delect { + public static void main(String[] args) { + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "delete from Student where id=4"; + str = con.createStatement(); + int i = str.executeUpdate(sql); + if (i > 0) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("导包异常"); + } catch (SQLException e) { + System.out.println("mysql语法错误"); + } finally { + try { + str.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + } + diff --git "a/12 \346\236\227\344\277\212\344\274\237/0520 \347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/12 \346\236\227\344\277\212\344\274\237/0520 \347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..a784d3e --- /dev/null +++ "b/12 \346\236\227\344\277\212\344\274\237/0520 \347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,157 @@ + public class Stu { + private int id; + private String name; + private String sex; + public Stu() { + } + + public Stu(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + 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 "Stu{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } + } + public static Connection conget(){ + Connection con = null; + try { + con = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("连接异常"); + } + return con; + } + public static ResultSet qure(String sql,String...keys) { + PreparedStatement pre = null; + ResultSet res = null; + try { + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + res = pre.executeQuery(); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return res; + } + public static int insert(String sql,String...keys) { + int num = 0; + PreparedStatement pre = null; + try { + pre = null; + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return num; + } + public static int update(String sql,String...keys) { + int num = 0; + PreparedStatement pre = null; + try { + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().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 = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + return num; + } + +``` +public class Test { + public static void main(String[] args) { + String sql="select * from studnet"; + ResultSet qure = Text.qure(sql);//查询 +// String sql1="insert into Studnet value(6,'一','女'),(7,'二','男'),(8,'三','男'),(9,'四','男'),(10,'五','男'); "; +// int insert = Text.insert(sql1);//增添 +// String sql2="update Studnet set sex='男' where id=6";//修改 +// int update = Text.update(sql2); +// String sql3="delete from Studnet where id=10";//删除 +// int delete = Text.delete(sql3); +// } +} +} +``` \ No newline at end of file -- Gitee