From 77324e657ae04532879331d7e1befbec28261041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?5=E5=8F=B7=E6=9E=97=E4=BC=9F=E5=BD=AC?= <2401916501@qq.com> Date: Wed, 17 May 2023 19:00:40 +0800 Subject: [PATCH] =?UTF-8?q?JDBC=E5=9F=BA=E7=A1=80=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\347\241\200\344\275\234\344\270\232 .md" | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 "05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" diff --git "a/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" "b/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" new file mode 100644 index 0000000..9e98393 --- /dev/null +++ "b/05 \346\236\227\344\274\237\345\275\254/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232 .md" @@ -0,0 +1,174 @@ +# 作业 + +### 查询 + +```java +import java.sql.*; +public class select { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="select * from sysy"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + ResultSet rs = st.executeQuery(sql); + //6.处理返回的结果 + while (rs.next()){ + //rs结果中有,三个字段 id name money + System.out.println(rs.getInt("编号")+ + "\t"+ rs.getString("姓名")+ + "\t"+rs.getInt("工资")); + } + //7.释放资源 + st.close(); + rs.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +### 修改 + +```java +import java.sql.*; + +public class update { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="UPDATE sysy SET 姓名='小臂崽子'where 编号=5"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + //6.处理返回的结果 + if (rs>0){ + System.out.println("修改成功-------------------------"); + }else { + System.out.println("修改失败-------------------------"); + } + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + +``` + +### 增加 + +```java +import java.sql.*; + +public class insert { + public static void main(String[] args) { + try { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql = "insert into sysy values(6,'小鸡崽子',2000) "; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + + //6.处理返回的结果 + + if (rs>0){ + System.out.println("添加成功-----------------------"); + }else { + System.out.println("添加失败------------------------"); + + } + + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + + + +``` + +### 删除 + +```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 { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url ="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + //3.定义Sql语句 + String sql="DELETE FROM sysy WHERE 编号=6"; + //4.获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //5.执行SQL + int rs = st.executeUpdate(sql); + //6.处理返回的结果 + if (rs>0){ + System.out.println("删除成功-------------------------"); + }else { + System.out.println("删除失败-------------------------"); + } + //7.释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + +``` + -- Gitee