diff --git "a/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" "b/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" deleted file mode 100644 index cfcbc6ef4e4f5f34acad1ef81c5abec8827e6171..0000000000000000000000000000000000000000 --- "a/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" +++ /dev/null @@ -1,175 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - ```java - public class Select { - public static void main(String[] args) { - ResultSet rst = null; - Statement smt = null; - Connection conn = null; - try { - Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String user = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, user, password); - String sql = "select * from student"; - smt = conn.createStatement(); - rst = smt.executeQuery(sql); - while (rst.next()) { - int id = rst.getInt("id"); - String name = rst.getString("name"); - String sex = rst.getString("sex"); - System.out.println(id + ":" + name + ":" + sex); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包没有正确导入,请检查!"); - } catch (SQLException e) { - System.out.println("sql语句执行错误!"); - e.printStackTrace(); - } finally { - try { - rst.close(); - smt.close(); - conn.close(); - } catch (SQLException e) { - System.out.println("资源释放异常"); - } - } - } - } - ``` - - 2. 添加功能 - - ``` - public class Insert { - public static void main(String[] args) { - Statement st = null; - Connection conn = null; - try { - Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&Unicode=true&characterEncoding=utf8"; - String user = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, user, password); - String sql = "insert into student values(4,'娜娜','女')"; - st = conn.createStatement(); - int i = st.executeUpdate(sql); - if (i > 0) { - System.out.println("成功插入" + i + "行"); - } else { - System.out.println("插入失败,插入" + i + "行"); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包没有正确导入,请检查!"); - } catch (SQLException e) { - System.out.println("sql执行语句错误!"); - e.printStackTrace(); - } finally { - try { - st.close(); - conn.close(); - } catch (SQLException e) { - System.out.println("释放资源失败"); - } - } - } - } - ``` - - 3. 修改功能 - - ```java - public class Update { - public static void main(String[] args) { - Statement st = null; - Connection conn = null; - try { - Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String user = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, user, password); - String sql = "update student set name='NANA' where id=4"; - st = conn.createStatement(); - int i = st.executeUpdate(sql); - if (i > 0) { - System.out.println("成功修改" + i + "行"); - } else { - System.out.println("修改失败,修改" + i + "行"); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包没有正确导入,请检查!"); - } catch (SQLException e) { - System.out.println("sql语句执行错误!"); - e.printStackTrace(); - } finally { - try { - st.close(); - conn.close(); - } catch (SQLException e) { - System.out.println("资源释放异常"); - } - } - } - } - ``` - - 4. 删除功能 - - ```java - public class Delete { - public static void main(String[] args) { - Statement st = null; - Connection conn = null; - try { - Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String username = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, username, password); - String sql = "delete from Student where id=4"; - st = conn.createStatement(); - int i = st.executeUpdate(sql); - if (i > 0) { - System.out.println("删除成功,删除了" + i + "行"); - } else { - System.out.println("删除失败,删除了" + i + "行"); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包没有正确导入,请检查!"); - } catch (SQLException e) { - System.out.println("sql语句执行错误!"); - e.printStackTrace(); - } finally { - try { - st.close(); - conn.close(); - } catch (SQLException e) { - System.out.println("资源释放异常"); - } - } - } - } - ``` - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/05 \350\260\242\351\223\226\346\265\251/JDBC\344\275\234\344\270\232\357\274\232.md" "b/05 \350\260\242\351\223\226\346\265\251/JDBC\344\275\234\344\270\232\357\274\232.md" deleted file mode 100644 index b331d9349d0b3c26e7be715e85febc525c73cc4f..0000000000000000000000000000000000000000 --- "a/05 \350\260\242\351\223\226\346\265\251/JDBC\344\275\234\344\270\232\357\274\232.md" +++ /dev/null @@ -1,201 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - - ~~~ sql - CREATE database student_db charset utf8; - use student_db; - ~~~ - - - -2. 库中创建student表 - - ~~~ sql - CREATE TABLE student( - id int, - name varchar(20), - sex char - ); - ~~~ - - - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - - ~~~ sql - INSERT into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); - ~~~ - - - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - ~~~ JAVA - import java.sql.*; - - public class Select { - 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("sql错误"); - e.printStackTrace(); - } - try { - if (res!=null){ - res.close();} - if (sta!=null){ - sta.close();} - if(conn!=null){ - conn.close();} - } catch (Exception e) { - System.out.println("释放异常"); - } - - } - } - ~~~ - - 2. 添加功能 - - ~~~ java - import java.sql.*; - - public class insert { - 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("添加失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - - - } - } - - ~~~ - - 3. 修改功能 - - ~~~ java - import java.sql.Connection; - import java.sql.DriverManager; - import java.sql.SQLException; - import java.sql.Statement; - - public class Update { - 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("修改失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - } - } - - ~~~ - - - - 4. 删除功能 - - ~~~ 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 { - 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 name='小李子'"; - Statement sta = conn.createStatement(); - int i = sta.executeUpdate(sql); - if (i>0){ - System.out.println("删除成功"); - }else { - System.out.println("删除失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - } - } - - ~~~ - - - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file 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" deleted file mode 100644 index cd4dc38024668772a1d3e5319ae6b47708ce8edc..0000000000000000000000000000000000000000 --- "a/09 \346\233\271\346\255\243\346\263\242/20230516 JAVA-JBDC\344\275\234\344\270\232.md" +++ /dev/null @@ -1,181 +0,0 @@ -## 作业 - -## 数据库代码 - -```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); - } - } - } -} - -``` - diff --git "a/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" "b/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" deleted file mode 100644 index 2a8a01684613e17cea42c4b62c3a5ec85110364e..0000000000000000000000000000000000000000 --- "a/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" +++ /dev/null @@ -1,204 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. CREATE DATABASE student_db CHARSET utf8; - USE student_db; - CREATE TABLE student( - id int, - name VARCHAR(10), - sex VARCHAR(10) - ); - INSERT INTO student - VALUES - (1,'张三','男'), - (2,'李四','女'), - (3,'王五','男'); - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - -6. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - 2. - - 3. ``` - import java.sql.*; - - public class Test02 { - public static void main(String[] args) { - ResultSet res = null; - Connection conn = null; - Statement smt = null; - try { - Class.forName("com.mysql.jdbc.Driver"); - String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String usename="root"; - String password="root"; - conn = DriverManager.getConnection(url,usename,password); - String sql = "select * from student"; - smt = conn.createStatement(); - res = smt.executeQuery(sql); - while (res.next()){ - int id = res.getInt("id"); - String sex = res.getString("sex"); - String name = res.getString("name"); - System.out.println(id+"--"+name+"--"+sex); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动异常"); - } catch (SQLException s) { - System.out.println("sql语句错误"); - s.printStackTrace(); - } - try { - if (res != null){ - res.close(); - } - if (smt != null){ - smt.close(); - } - if (conn != null){ - conn.close(); - } - } catch (SQLException e) { - System.out.println("资源释放错误"); - e.printStackTrace(); - } - } - - // public static void main(String[] args) { - // Class.forName("com.mysql.jdbc.Driver"); - // String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; - // String usename="root";//数据库用户名 - // String password="root";//数据库密码 - // String sql = "select * from studen"; - // Connection conn = DriverManager.getConnection(url, usename, password); - // Statement re =conn.createStatement(); - // } - } - ``` - - 4. 添加功能 - - 5. - - 6. ``` - Insert - public class Insert { - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - - 7. 修改功能 - - 8. - - 9. ``` - import java.sql.Connection; - import java.sql.DriverManager; - import java.sql.SQLException; - import java.sql.Statement; - - public class Update { - 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 sex = '男' where name = '王六'"; - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - - 10. 删除功能 - - 11. - - 12. ``` - 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 { - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230516JDBC\344\275\234\344\270\232.md" "b/14 \346\235\216\344\277\212\345\205\264/20230516JDBC\344\275\234\344\270\232.md" deleted file mode 100644 index 128a5560b7060fbb537553b841243cf7a17c167d..0000000000000000000000000000000000000000 --- "a/14 \346\235\216\344\277\212\345\205\264/20230516JDBC\344\275\234\344\270\232.md" +++ /dev/null @@ -1,244 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - 2. 添加功能 - 3. 修改功能 - 4. 删除功能 - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 - -1. 查讯类 - -```java -import java.net.ConnectException; -import java.sql.*; - -public class Test { - public static void main(String[] args) { - extracted(); - } - - private static void extracted() { - ResultSet rst = null; - Statement smt = null; - Connection conn = null; - try { - //0 导包 - //1 注册驱动 - Class.forName("com.mysql.jdbc.Driver"); - //2 获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; - String username = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, username, password); - //3 编写SQL语句 - String sql = "select * from student"; - //4 获取执行 SQL 对象 - smt = conn.createStatement(); - //5 执行SQL - rst = smt.executeQuery(sql); - while (rst.next()) { - int id = rst.getInt("id"); - String name = rst.getString("name"); - String sex = rst.getString("sex"); - System.out.println(id + "-" + name + "." + sex); - } - - - } catch (ClassNotFoundException e) { - System.out.println("没有成功导入包,请正确导入"); - } catch (SQLException e) { - System.out.println("sql语句错误"); - e.printStackTrace(); - } finally { - try { - // 7 释放资源 - rst.close(); - smt.close(); - conn.close(); - } catch (SQLException e) { - System.out.println("资源释放异常"); - } - } - } -} - -``` - -2. 插入类 - -```java -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 smt = null; - Connection conn = null; - try { - //0 导包 - //1 注册驱动 - Class.forName("com.mysql.jdbc.Driver"); - //2 获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; - String username = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, username, password); - //3 SQL 语句 - String sql = "INSERT INTO student VALUES\n(3,'强强','男')"; - //4 获取执行 - smt = conn.createStatement(); - //5 执行sql - int i = smt.executeUpdate(sql); - //6 处理返回问题 - if (i > 0) { - System.out.println("插入成功"); - } else { - System.out.println("没有插入数据哦!请检查"); - } - - } catch (ClassNotFoundException e) { - System.out.println("没有成功导入包,请正确导入"); - } catch (SQLException e) { - System.out.println("sql语句错误"); - } finally { - //7 释放 - try { - smt.close(); - conn.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - } -} - -``` - -3. 修改类 - -~~~java -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; - -public class Update { - public static void main(String[] args) { - Connection conn = null; - Statement smt = null; - try { - //0 导包 - //1 注册驱动 - Class.forName("com.mysql.jdbc.Driver"); - //2 获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; - String username = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, username, password); - //3 编写SQL语句 - String sql = "UPDATE student SET NAME = '李小青' WHERE id = 2"; - //4 获取执行 SQL 对象 - smt = conn.createStatement(); - //5 执行SQL - int i = smt.executeUpdate(sql); - //6 处理返回结果 - if(i>0){ - System.out.println("成功修改了"+i+"行"); - }else { - System.out.println("成功修改了"+i+"行"); - System.out.println("没有改变"); - } - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } catch (SQLException e) { - throw new RuntimeException(e); - }finally { - // 7 释放资源 - try { - smt.close(); - conn.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - - - } -} - -~~~ - -4. 删除类 - -```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) { - Statement stm = null; - Connection conn = null; - try { - //0 导包 - //1 注册驱动 - Class.forName("com.mysql.jdbc.Driver"); - //2 获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; - String username = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, username, password); - //3 编写SQL语句 - String sql = "DELETE FROM student WHERE id=3"; - //4 获取执行 - stm = conn.createStatement(); - //5 执行sql语句 - int i = stm.executeUpdate(sql); - //6 处理返回结果 - if (i > 0) { - System.out.println("成功删除" + i + "行"); - System.out.println("删除成功"); - } else { - System.out.println("成功删除" + i + "行"); - System.out.println("删除失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("没有成功导入包,请正确导入"); - } catch (SQLException e) { - System.out.println("sql语句错误"); - } finally { - //7 释放 - try { - stm.close(); - conn.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - } -} - -``` - diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" "b/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" deleted file mode 100644 index 089646f4d4c254d8a9414e86973d575a72a7f4aa..0000000000000000000000000000000000000000 --- "a/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" +++ /dev/null @@ -1,195 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - 2. 添加功能 - 3. 修改功能 - 4. 删除功能 - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 - -```mysql -create database student_db charset utf8; -use student_db; -create table student( -id int, -name varchar(50), -sex VARCHAR(50) -); -insert into student VALUES -(1,'张三',"男"), -(2,'李四','女'), -(3,'王五','男'); -``` - - - -```java -select类 - import java.sql.*; - -public class select { - public static void main(String[] args) { - ResultSet re=null; - Statement st=null; - Connection con=null; - try { - Class.forName("com.mysql.jdbc.Driver"); - con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); - String sql = "select * from student"; - st = con.createStatement(); - re = st.executeQuery(sql); - while (re.next()){ - int id = re.getInt("id"); - String name = re.getString("name"); - String sex = re.getString("sex"); - System.out.println(id + "\t" + name + "\t" + sex); - - - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("运行错误"); - } finally { - try { - if(re!=null){ - re.close(); - } - if(st!=null){ - st.close(); - } - if(con!=null){ - con.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - System.out.println("资源释放异常"); - } - } - - } -} -``` - -```java -insert类 - 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) { - try { - Class.forName("com.mysql.jdbc.Driver"); - Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); - String sql = "insert into student values (4,'黒姫結灯','女')"; - Statement st = con.createStatement(); - int i = st.executeUpdate(sql); - - if(i>0){ - System.out.println("成功插入"+i+"行"); - System.out.println("插入成功"); - }else{ - System.out.println("成功插入"+i+"行"); - System.out.println("插入失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - e.printStackTrace(); - System.out.println("运行错误"); - } - - - } -} - -``` - -```java -delete类 - 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 { - Class.forName("com.mysql.jdbc.Driver"); - Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); - String sql = "delete from student where name='黒姫結灯'"; - Statement st = con.createStatement(); - int i = st.executeUpdate(sql); - - if(i>0){ - System.out.println("成功删除"+i+"行"); - System.out.println("删除成功"); - }else{ - System.out.println("成功删除"+i+"行"); - System.out.println("=删除失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("运行包错误"); - } catch (SQLException e) { - e.printStackTrace(); - System.out.println("运行错误"); - } - } -} - -``` - -```java -update类 - import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; - -public class update { - public static void main(String[] args) { - try { - Class.forName("com.mysql.jdbc.Driver"); - Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); - String sql = "update student set name='一之濑穗波',sex='女' where id=1"; - String sqls="update student set name='砚川·e·泪香' where id=2"; - String ssql="update student set name='冰见山玲',sex='女' where id=3"; - Statement st = con.createStatement(); - int i = st.executeUpdate(sqls); - if(i>0){ - System.out.println("成功修改"+i+"行"); - System.out.println("修改成功"); - }else { - System.out.println("成功修改"+i+"行"); - System.out.println("修改失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - e.printStackTrace(); - System.out.println("运行错误"); - } - } -} - -``` - diff --git "a/20 \347\237\263\350\211\257\346\266\233/20230516JSP\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/20 \347\237\263\350\211\257\346\266\233/20230516JSP\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 0267630e5f2b47626855265d31de028db825c316..0000000000000000000000000000000000000000 --- "a/20 \347\237\263\350\211\257\346\266\233/20230516JSP\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,119 +0,0 @@ -## 1. 作业 - -### 创建数据库: - -```sql -CREATE DATABASE student_db; -``` - -### 选择数据库: - -```sql -USE student_db; - -``` - -### 创建student表: - -```sql -CREATE TABLE student ( - 编号 INT PRIMARY KEY, - 姓名 VARCHAR(50), - 性别 VARCHAR(10) -); - -``` - -### 插入数据: - -```sql -INSERT INTO student (编号, 姓名, 性别) VALUES -(1, '张三', '男'), -(2, '李四', '女'), -(3, '王五', '男'); - -``` - -### JAVA - -```java -import java.sql.*; - -public class StudentDatabase { - private Connection connection; - private Statement statement; - - public StudentDatabase() { - try { - // 连接到MySQL数据库 - connection = DriverManager.getConnection("jdbc:mysql://localhost/student_db", "用户名", "密码"); - statement = connection.createStatement(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - // 查询功能,查询student表中所有数据 - public void queryStudents() { - try { - ResultSet resultSet = statement.executeQuery("SELECT * FROM student"); - while (resultSet.next()) { - int id = resultSet.getInt("编号"); - String name = resultSet.getString("姓名"); - String gender = resultSet.getString("性别"); - System.out.println("编号: " + id + ", 姓名: " + name + ", 性别: " + gender); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } - - // 添加功能,向student表中添加一条数据 - public void addStudent(int id, String name, String gender) { - try { - String query = "INSERT INTO student (编号, 姓名, 性别) VALUES (" + id + ", '" + name + "', '" + gender + "')"; - statement.executeUpdate(query); - System.out.println("已添加学生信息"); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - // 修改功能,修改student表中指定学生的数据 - public void updateStudent(int id, String newName, String newGender) { - try { - String query = "UPDATE student SET 姓名 = '" + newName + "', 性别 = '" + newGender + "' WHERE 编号 = " + id; - statement.executeUpdate(query); - System.out.println("已更新学生信息"); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - // 删除功能,删除student表中指定学生的数据 - public void deleteStudent(int id) { - try { - String query = "DELETE FROM student WHERE 编号 = " + id; - statement.executeUpdate(query); - System.out.println("已删除学生信息"); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - public static void main(String[] args) { - StudentDatabase studentDB = new StudentDatabase(); - studentDB.queryStudents(); // 查询所有学生信息 - - studentDB.addStudent(4, "赵六", "女"); // 添加学生信息 - studentDB.queryStudents(); - - studentDB.updateStudent(3, "王五", "女"); // 修改学生信息 - studentDB.queryStudents(); - - studentDB.deleteStudent(2); // 删除学生信息 - studentDB.queryStudents(); - } -} - -``` diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230517 JDBS\344\275\234\344\270\232.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230517 JDBS\344\275\234\344\270\232.md" deleted file mode 100644 index 00dc2705c2b5d4ec4ad4b4dc196b9f36fa76fdc5..0000000000000000000000000000000000000000 --- "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230517 JDBS\344\275\234\344\270\232.md" +++ /dev/null @@ -1,215 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - 2. 添加功能 - - 3. 修改功能 - - 4. 删除功能 - - ~~~ sql - create database student_db charset utf8; - use student_db; - create table student( - id int, - name varchar(20), - sex varchar(5) - ); - alter table student charset utf8; - insert into student values - (1,"张三","男"), - (2,"李四","女"), - (3,"王五","男"); - ~~~ - - - - ~~~ java - // 查询测试类 - import java.sql.*; - import java.util.concurrent.Callable; - - public class Test { - public static void main(String[] args) { - Connection conn = null; - Statement smt = null; - ResultSet rst = null; - // 1.建立驱动 - try { - Class.forName("com.mysql.jdbc.Driver"); - // 2.获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String uesrname = "root"; - String password = "root"; - conn = DriverManager.getConnection(url, uesrname, password); - // 3.定义sql语言 - String sql = "select * from student"; - // 4.获取执行SQL的对象 - smt = conn.createStatement(); - // 5.执行SQL - rst = smt.executeQuery(sql); - // 6.处理返回结果 - while(rst.next()){ - int id = rst.getInt("id"); - String name = rst.getString("name"); - String sex = rst.getString("sex"); - System.out.println(id+":"+name+"-"+sex); - } - } catch (ClassNotFoundException e) { - System.out.println("编译出错"); - } catch (SQLException e) { - System.out.println("SQL出错"); - } - // 7.释放资源 - try { - if (conn!=null){ - conn.close(); - } - if (smt!=null){ - smt.close(); - } - if (rst!=null){ - rst.close(); - } - } catch (SQLException e) { - System.out.println("释放资源出错"); - } - } - } - - ~~~ - - ~~~ java - // 添加测试类 - import java.sql.*; - - public class Insert { - public static void main(String[] args) { - // 1.建立驱动 - try { - Class.forName("com.mysql.jdbc.Driver"); - // 2.获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String uesrname = "root"; - String password = "root"; - Connection conn = DriverManager.getConnection(url, uesrname, password); - // 3.定义sql语言 - String sql = "insert into student values (4,'赵六','女')"; - // 4.获取执行SQL的对象 - Statement smt = conn.createStatement(); - // 5.执行SQL - int i = smt.executeUpdate(sql); - // 6.处理返回结果 - if (i>0){ - System.out.println("添加成功"); - }else{ - System.out.println("添加失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("编译出错"); - } catch (SQLException e) { - System.out.println("SQL出错"); - } - } - } - - ~~~ - - ~~~ 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) { - // 1.建立驱动 - try { - Class.forName("com.mysql.jdbc.Driver"); - // 2.获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String uesrname = "root"; - String password = "root"; - Connection conn = DriverManager.getConnection(url, uesrname, password); - // 3.定义sql语言 - String sql = "delete from student where id=4"; - // 4.获取执行SQL的对象 - Statement smt = conn.createStatement(); - // 5.执行SQL - int i = smt.executeUpdate(sql); - // 6.处理返回结果 - if (i>0){ - System.out.println("删除成功"); - }else{ - System.out.println("删除失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("编译出错"); - } catch (SQLException e) { - System.out.println("SQL出错"); - } - } - } - - ~~~ - - ~~~ java - // 修改测试类 - import java.sql.Connection; - import java.sql.DriverManager; - import java.sql.SQLException; - import java.sql.Statement; - - public class Update { - public static void main(String[] args) { - try { - Class.forName("com.mysql.jdbc.Driver"); - // 2.获取连接对象 - String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String uesrname = "root"; - String password = "root"; - Connection conn = DriverManager.getConnection(url, uesrname, password); - // 3.定义sql语言 - String sql = "update student set sex='女' where id=3"; - // 4.获取执行SQL的对象 - Statement smt = conn.createStatement(); - // 5.执行SQL - int i = smt.executeUpdate(sql); - // 6.处理返回结果 - if (i>0){ - System.out.println("修改成功"); - }else{ - System.out.println("修改失败"); - } - } catch (ClassNotFoundException e) { - System.out.println("编译出错"); - } catch (SQLException e) { - System.out.println("SQL出错"); - } - } - } - - ~~~ - - - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\232\357\274\232.md" "b/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\232\357\274\232.md" deleted file mode 100644 index b331d9349d0b3c26e7be715e85febc525c73cc4f..0000000000000000000000000000000000000000 --- "a/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\232\357\274\232.md" +++ /dev/null @@ -1,201 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - - ~~~ sql - CREATE database student_db charset utf8; - use student_db; - ~~~ - - - -2. 库中创建student表 - - ~~~ sql - CREATE TABLE student( - id int, - name varchar(20), - sex char - ); - ~~~ - - - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - - ~~~ sql - INSERT into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); - ~~~ - - - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - ~~~ JAVA - import java.sql.*; - - public class Select { - 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("sql错误"); - e.printStackTrace(); - } - try { - if (res!=null){ - res.close();} - if (sta!=null){ - sta.close();} - if(conn!=null){ - conn.close();} - } catch (Exception e) { - System.out.println("释放异常"); - } - - } - } - ~~~ - - 2. 添加功能 - - ~~~ java - import java.sql.*; - - public class insert { - 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("添加失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - - - } - } - - ~~~ - - 3. 修改功能 - - ~~~ java - import java.sql.Connection; - import java.sql.DriverManager; - import java.sql.SQLException; - import java.sql.Statement; - - public class Update { - 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("修改失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - } - } - - ~~~ - - - - 4. 删除功能 - - ~~~ 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 { - 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 name='小李子'"; - Statement sta = conn.createStatement(); - int i = sta.executeUpdate(sql); - if (i>0){ - System.out.println("删除成功"); - }else { - System.out.println("删除失败"); - } - - } catch (ClassNotFoundException e) { - System.out.println("驱动包错误"); - } catch (SQLException e) { - System.out.println("sql错误"); - } - } - } - - ~~~ - - - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" "b/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" deleted file mode 100644 index de0f3a3f8683b2d0abfce820cc1b4280ce8ffd3c..0000000000000000000000000000000000000000 --- "a/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" +++ /dev/null @@ -1,196 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - - ```mysql - create database student_db charset utf8; - use student_db; - create table student ( - id int, - name varchar(10), - sex char - ); - insert into student values - (1,'张三','男'), - (2,'李四','女'), - (3,'王五','男'); - - select * from student; - ``` - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - ```javascript - import java.sql.*; - - public class Select { - 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&useUnicode=true&characterEncoding=utf8"; - 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("驱动异常"); - } catch (SQLException e) { - System.out.println("sql语句错误"); - e.printStackTrace(); - }finally { - try { - if (res != null){ - res.close(); - } - if (sta != null){ - sta.close(); - } - if (conn != null){ - conn.close(); - } - } catch (SQLException e) { - System.out.println("资源释放错误"); - e.printStackTrace(); - } - } - } - } - ``` - - 2. 添加功能 - - ```javascript - 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) { - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - - 3. 修改功能 - - ```javascript - import java.sql.Connection; - import java.sql.DriverManager; - import java.sql.SQLException; - import java.sql.Statement; - - public class Update { - 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 sex = '男' where name = '王六'"; - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - - 4. 删除功能 - - ```javascript - 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 { - 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("sql语句错误"); - e.printStackTrace(); - } - } - } - ``` - - - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file 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" deleted file mode 100644 index 3fb13e9fecce3123f668c3f3f388c34371b64a04..0000000000000000000000000000000000000000 --- "a/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232.md" +++ /dev/null @@ -1,187 +0,0 @@ -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/\346\225\260\346\215\256\345\272\223.md" "b/\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..6eb4f382c4cdb647112b653915642d8fb6a1ba18 --- /dev/null +++ "b/\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,147 @@ +```java + +import java.sql.*; +import java.util.Scanner; + +public class Data { + public static void main(String[] args) { + while (true) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入你想要的功能:(1、查询所有数据 2、添加数据 3、修改数据 4、删除数据 5、退出)"); + int num = scanner.nextInt(); + switch (num) { + case 1: + new Select().select(); + break; + case 2: + new Add().add(); + break; + case 3: + new Updata().updata(); + break; + case 4: + new Delect().delect(); + break; + } + if (num == 5) { + break; + } + } + } + + static class Select { + void select() { + Connection conn = null; + Statement stmt = null; + ResultSet rs = null; + + try { + + String user = "root"; + String pwd = "xflovexz"; + // 加载MySQL驱动类 + Class.forName("com.mysql.jdbc.Driver"); + + // 创建连接 + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd); + + // 创建Statement对象 + stmt = conn.createStatement(); + + // 执行查询操作 + rs = stmt.executeQuery("SELECT * FROM sutdent"); + + // 遍历结果集 + while (rs.next()) { + System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getString("gender")); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + // 释放资源 + try { + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + + public static class Add { + void add() { + try { + String user = "root"; + String pwd = "xflovexz"; + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd); + Statement statement = connection.createStatement(); + int resultSet = statement.executeUpdate("insert into sutdent values (4,'小明','男')"); + if (pwd != null) { + System.out.println("添加成功"); + } else { + System.out.println("添加失败"); + } + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + public static class Updata { + void updata() { + try { + String user = "root"; + String pwd = "xflovaxz"; + Connection connection = DriverManager.getConnection("jdbc:mysql:///student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd); + Statement statement = connection.createStatement(); + int resultSet = statement.executeUpdate("update sutdent set name='小明' where id=2"); + if (pwd != null) { + System.out.println("修改成功"); + } else { + System.out.println("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + + public static class Delect { + void delect() { + try { + String user = "root"; + String pwd = "xflovexz"; + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd); + Statement statement = connection.createStatement(); + int resultSet = statement.executeUpdate("delete from sutdent where id=4 "); + if (pwd != null) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + +} + + +``` +