diff --git "a/09 \346\241\202\346\263\275\347\205\234/JDBC\344\275\234\344\270\232\357\274\232.md" "b/09 \346\241\202\346\263\275\347\205\234/JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c8de46dc0a8afa7e831ff23675b70b6637301316 --- /dev/null +++ "b/09 \346\241\202\346\263\275\347\205\234/JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,231 @@ +### 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对象 + +```java +import com.mysql.cj.jdbc.Driver; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class Conn { + Connection con; + public static String user; + public static String password; + public Connection getConnection(){ + try{ + Class.forName("com.mysql.cj.jdbc.Driver"); + System.out.println("数据库驱动加载成功"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + user ="root"; + password = "root"; + try { + con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useUnicode=true&characterEncoding=gbk",user,password); + System.out.println("数据库连接成功"); + }catch (SQLException e){ + e.printStackTrace(); + } + return con; + } + + public static void main(String[] args) { + Conn c = new Conn(); + c.getConnection(); + } +} +``` + +```java +import java.sql.*; + +public class JDBC { + static String url = "jdbc:mysql://localhost:3306/student_db?useUnicode=true&characterEncoding=utf8&useSSL=false"; + static String username = "root"; + static String password = "root"; + + public static void main(String[] args) { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + find(); + add(); + update(); + delete(); + } + + static void find() { + Connection connection = null; + try { + connection = DriverManager.getConnection(url, username, password); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + Statement statement = null; + try { + statement = connection.createStatement(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + String sql = "SELECT * FROM STUDENT;"; + ResultSet resultSet = null; + try { + resultSet = statement.executeQuery(sql); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + while (true) { + try { + if (!resultSet.next()) break; + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + try { + System.out.println("id=" + resultSet.getInt("id")); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + try { + System.out.println("name=" + resultSet.getString("name")); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + try { + System.out.println("sex=" + resultSet.getString("sex")); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + try { + resultSet.close(); + connection.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + + static void add() { + Connection connection = null; + try { + connection = DriverManager.getConnection(url, username, password); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + Statement statement = null; + try { + statement = connection.createStatement(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + String sql2 = "INSERT INTO STUDENT VALUES (4,'柳长风','男');"; + int i = 0; + try { + i = statement.executeUpdate(sql2); + } catch (SQLException throwables) { + } + System.out.println("添加了" + i + "条数据"); + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + + static void update() { + Connection connection = null; + try { + connection = DriverManager.getConnection(url, username, password); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + Statement statement = null; + try { + statement = connection.createStatement(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + String sql3 = "UPDATE STUDENT SET NAME='张翰' WHERE ID =1;"; + int s = 0; + try { + s = statement.executeUpdate(sql3); + } catch (SQLException throwables) { + } + System.out.println("更新了" + s + "条数据"); + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + + static void delete() { + Connection connection = null; + try { + connection = DriverManager.getConnection(url, username, password); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + Statement statement = null; + try { + statement = connection.createStatement(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + String sql4 = "DELETE FROM STUDENT WHERE ID =4"; + int j = 0; + try { + j = statement.executeUpdate(sql4); + } catch (SQLException throwables) { + } + System.out.println("删除了" + j + "条数据"); + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } +} +``` + +```mysql +create database student_db charset=utf8; +use student_db; +create table student( +id int primary key auto_increment comment'编号', +name varchar(20) comment'姓名', +sex char(1) comment'性别' +)comment'学生表'; +insert into student +values +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); + +``` + diff --git "a/09 \346\241\202\346\263\275\347\205\234/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/09 \346\241\202\346\263\275\347\205\234/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..cd35f16b3e6fc10f6f1fc9b4883a9b473c0b6abb --- /dev/null +++ "b/09 \346\241\202\346\263\275\347\205\234/\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,177 @@ +```mysql +create database student_db charset=utf8; +use student_db; +create table student( +id int primary key auto_increment comment'编号', +name varchar(20) comment'姓名', +sex char(1) comment'性别' +)comment'学生表'; +insert into student +values +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +``` + +```java +import java.sql.*; +import java.util.Scanner; + +public class studentmanagementsystem { + static Scanner scanner = new Scanner(System.in); + + { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + Connection connection = null; + try { + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSl=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + studentmanagementsystem studentmanagementsystem = new studentmanagementsystem(); + while (true) { + System.out.println("---学生管理系统---" + + "\n---1.查询所有数据" + + "\n---2.添加数据" + + "\n---3.删除数据" + + "\n---4.更新数据" + + "\n---5.退出" + + "\n请输入对应的数字"); + int i = scanner.nextInt(); + switch (i) { + case 1: + find(); + break; + case 2: + add(); + break; + case 3: + delete(); + break; + case 4: + update(); + break; + case 5: + System.exit(0); + } + } + + } + + static void find() { + Connection connection = null; + String sql = "SELECT * FROM STUDENT;"; + Statement statement = null; + ResultSet resultSet = null; + try { + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSl=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + statement = connection.prepareStatement(sql); + resultSet = statement.executeQuery(sql); + } catch (SQLException e) { + throw new RuntimeException(e); + } + while (true) { + try { + if (!resultSet.next()) break; + System.out.println("编号:" + resultSet.getString("id")); + System.out.println("姓名:" + resultSet.getString("name")); + System.out.println("性别:" + resultSet.getString("sex")); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + try { + resultSet.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + static void add() { + Connection connection = null; + System.out.println("请输入编号:"); + String number = scanner.next(); + System.out.println("请输入姓名:"); + String name = scanner.next(); + System.out.println("请输入性别:"); + String sex = scanner.next(); + String sql = "INSERT INTO student VALUES (?,?,?);"; + int i = 0; + PreparedStatement statement=null; + try { + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSl=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + statement = connection.prepareStatement(sql); + statement.setString(1,number); + statement.setString(2,name); + statement.setString(3,sex); + i = statement.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + System.out.println("添加了" + i + "条数据"); + try { + statement.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + static void delete() { + Connection connection = null; + System.out.println("请输入你要删除的编号:"); + String number = scanner.next(); + String sql = "DELETE FROM STUDENT WHERE id=?;"; + PreparedStatement statement = null; + int i = 0; + try { + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSl=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + statement = connection.prepareStatement(sql); + statement.setString(1,number); + i = statement.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + System.out.println("删除了" + i + "条数据"); + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + + static void update() { + Connection connection = null; + System.out.println("请输入修改的姓名:"); + String name = scanner.next(); + System.out.println("请输入它的编号"); + String number = scanner.next(); + String sql = "UPDATE student set name=? where id = ?;"; + PreparedStatement statement = null; + int i = 0; + try { + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSl=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + statement = connection.prepareStatement(sql); + statement.setString(1,name); + statement.setString(2,number); + i = statement.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + System.out.println("更新了" + i + "条数据"); + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } +} + +``` + diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8a99cf16241be659ed8ec007590b485c1466bcb8 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# JSP+Sevrlet网站开发 + +#### 介绍 +JSP+Sevrlet网站开发 + +#### 软件架构 +软件架构说明 + + +#### 安装教程 + +1. xxxx +2. xxxx +3. xxxx + +#### 使用说明 + +1. xxxx +2. xxxx +3. xxxx + +#### 参与贡献 + +1. Fork 本仓库 +2. 新建 Feat_xxx 分支 +3. 提交代码 +4. 新建 Pull Request + + + +#### 特技 + +1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md +2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) +3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 +4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 +5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) +6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)