From c26cff1107350694961a5e068f969f6626672dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=AD=A3=E6=B3=A2?= <1938448998@qq.com> Date: Wed, 17 May 2023 17:50:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=94=E6=9C=88=E5=8D=81=E5=85=AD=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0516 JAVA-JBDC\344\275\234\344\270\232.md" | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 "09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" "b/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..cd4dc38 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" @@ -0,0 +1,181 @@ +## 作业 + +## 数据库代码 + +```mysql +create database student_db CHARSET utf8; +use student_db; +CREATE table studen( + id int, + name varchar(10), + sex enum('男','女') +); +insert into studen values +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +``` + +## 查询 + +```java +public class Sele { + public static void main(String[] args) { + ResultSet re = null; + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "select * from studen"; + sta = con.createStatement(); + re = sta.executeQuery(sql); + while (re.next()) { + System.out.println(re.getInt("id") + ":" + re.getString("name") +" "+ re.getString("sex")); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + if (re != null) { + re.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } +} + +``` + + + +## 添加 + +```java +public class Inse { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "insert into studen values(4,'赵六','男')"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("添加成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + + + +## 修改 + +```java +public class Upda { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "update studen set name='老六' where id=4"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("修改成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +## 删除 + +```java +public class Dele { + public static void main(String[] args) { + Statement sta = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8";//地址 + String usename = "root";//数据库用户名 + String password = "root";//数据库密码 + con = DriverManager.getConnection(url, usename, password); + String sql = "delete from studen where id=4"; + sta = con.createStatement(); + int i = sta.executeUpdate(sql); + System.out.println("删除成功!"); + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } finally { + try { + if (con != null) { + con.close(); + } + if (sta != null) { + sta.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + -- Gitee