diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/JDBC\344\275\234\344\270\232.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..00dc2705c2b5d4ec4ad4b4dc196b9f36fa76fdc5 --- /dev/null +++ "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,215 @@ +### 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