From e51a387b997435eae6e6e28d877578371306f443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E4=BC=9F=E5=B1=B1?= <2609838563@qq.com> Date: Fri, 19 May 2023 18:53:16 +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 --- .../20230519 jsp\345\237\272\347\241\200.md" | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 "39 \345\247\234\344\274\237\345\261\261/20230519 jsp\345\237\272\347\241\200.md" diff --git "a/39 \345\247\234\344\274\237\345\261\261/20230519 jsp\345\237\272\347\241\200.md" "b/39 \345\247\234\344\274\237\345\261\261/20230519 jsp\345\237\272\347\241\200.md" new file mode 100644 index 0000000..e8f45c2 --- /dev/null +++ "b/39 \345\247\234\344\274\237\345\261\261/20230519 jsp\345\237\272\347\241\200.md" @@ -0,0 +1,164 @@ +```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