diff --git "a/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232.md" "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..3fb13e9fecce3123f668c3f3f388c34371b64a04 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232.md" @@ -0,0 +1,187 @@ +MySQL中创建一个数据库student_db + +库中创建student表 + +1. 表中数据如下 + +2. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +```mysql +CREATE database student_db charset utf8; +use student_db; +CREATE TABLE student( +id int, +`name` varchar(20), +sex char +); +INSERT into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); +``` + +编写java 4个类,分别实现以下功能 + +1. 查询功能,查询student中所有数据 +2. 添加功能 + + 3.修改功能 + + 4.删除功能 + +```java +import java.sql.*; + +public class Test1 { + public static void main(String[] args) { + Connection conn=null; + Statement sta=null; + ResultSet res=null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false"; + String username="root"; + String password="root"; + conn = DriverManager.getConnection(url, username, password); + String sql="select * from student"; + sta = conn.createStatement(); + res = sta.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String name=res.getString("name"); + String sex=res.getString("sex"); + System.out.println(id+"-"+name+"-"+sex); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("语法错误"); + e.printStackTrace(); + } + try { + if (res!=null){ + res.close();} + if (sta!=null){ + sta.close();} + if(conn!=null){ + conn.close();} + } catch (Exception e) { + System.out.println("资源释放异常"); + } + + } +} +``` + +```java +import java.sql.*; + +public class Test2{ + public static void main(String[] args) { + + + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="insert into student values(4,'子豪','男')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("添加成功"); + }else { + System.out.println("失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } 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 Test3{ + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="update student set name='叶子豪' where id=4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } 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 Test4 { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="delete from student where id=4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } catch (SQLException e) { + System.out.println("语法错误"); + } + } +} + +``` + diff --git "a/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\2322.md" "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\2322.md" new file mode 100644 index 0000000000000000000000000000000000000000..ba5677b138715a791dbbbeed7255f56d3cc8faa6 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\2322.md" @@ -0,0 +1,256 @@ +MySQL中创建一个数据库student_db + +库中创建student表 + +1. 表中数据如下 + +2. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +```mysql +CREATE database student_db charset utf8; +use student_db; +CREATE TABLE student( +id int, +`name` varchar(20), +sex char +); +INSERT into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); +``` + +编写java 4个类,分别实现以下功能 + +1. 查询功能,查询student中所有数据 +2. 添加功能 + + 3.修改功能 + + 4.删除功能 + +```java +import java.sql.*; + +public class Test1 { + public static void main(String[] args) { + Connection conn=null; + Statement sta=null; + ResultSet res=null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false"; + String username="root"; + String password="root"; + conn = DriverManager.getConnection(url, username, password); + String sql="select * from student"; + sta = conn.createStatement(); + res = sta.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String name=res.getString("name"); + String sex=res.getString("sex"); + System.out.println(id+"-"+name+"-"+sex); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("语法错误"); + e.printStackTrace(); + } + try { + if (res!=null){ + res.close();} + if (sta!=null){ + sta.close();} + if(conn!=null){ + conn.close();} + } catch (Exception e) { + System.out.println("资源释放异常"); + } + + } +} +``` + +```java +import java.sql.*; + +public class Test2{ + public static void main(String[] args) { + + + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="insert into student values(4,'子豪','男')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("添加成功"); + }else { + System.out.println("失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } 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 Test3{ + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="update student set name='叶子豪' where id=4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } 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 Test4 { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="delete from student where id=4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + + sta.close(); + conn.close(); + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入错误"); + } catch (SQLException e) { + System.out.println("语法错误"); + } + } +} + +``` + +~~~java +public class Test5 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql="update studen set name='小米' where id=1"; + int que = ZSG.query(sql); + try { + System.out.println("修改成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + for (int i = 0; i