diff --git "a/19 \345\224\220\345\233\275\344\272\256/JDBC\344\275\234\344\270\232\357\274\232.md" "b/19 \345\224\220\345\233\275\344\272\256/JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b3a55fc6b10d923af7c7016d64e2b50be31e0c03 --- /dev/null +++ "b/19 \345\224\220\345\233\275\344\272\256/JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,170 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + ```java + import java.sql.*; + + public class Select { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false", "root", "root"); + String sql = "select * from student"; + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + while (rs.next()){ + int stuNo = rs.getInt("stuNo"); + String stuName = rs.getString("stuName"); + String stuSex = rs.getString("stuSex"); + System.out.println(stuNo +" "+ "姓名:" + stuName +" "+ "性别:" + stuSex); + } + rs.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + ``` + + ```java + import java.sql.*; + + public class Add { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "insert into student values (4,'刘高科','男'),(5,'程阳','男')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("添加成功"+i+"行"); + }else { + System.out.println("添加失败"); + System.out.println("添加失败"+i+"行"); + } + 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 Update { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "update student set stuName='宋宏杨' where stuNo=3"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("添加成功"+i+"行"); + }else { + System.out.println("添加失败"); + System.out.println("添加失败"+i+"行"); + } + 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 { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "delete from student where stuNo=4"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("成功删除"+i+"行"); + }else { + System.out.println("删除失败"); + System.out.println("删除失败"+i+"行"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + ``` + + ```mysql + create database Ok; + create database student_db charset utf8; + use student_db; + create table student( + stuNo int, + stuName varchar(20), + stuSex varchar(20) + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + + + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file