diff --git "a/29 \350\267\257\347\216\262/20230516 JDBC\344\275\234\344\270\232.md" "b/29 \350\267\257\347\216\262/20230516 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..7e86280905c6719b47184032b90e85a6fe2b0035 --- /dev/null +++ "b/29 \350\267\257\347\216\262/20230516 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,217 @@ +# 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 java.sql.*; + +public class Test { + public static void main(String[] args) { + Connection conn =null; + Statement sta = null; + ResultSet result =null; + try { + //第一步 导包 + //第二步 加载MYSQL 驱动类 + Class.forName("com.mysql.jdbc.Driver"); + //创建连接 + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8","root","root");//后面跟数据库的名字 + //编写SQL的语句 + String sql ="select * from student";//dql//注意这里查询的是表,所以写的是表名 + //获取执行SQL的对象 + sta = conn.createStatement(); + //sta执行SQL + result = sta.executeQuery(sql); + //处理返回结果 + while (result.next()){ + int id = result.getInt("id"); + String name= result.getString("name"); + String sex = result.getString("sex"); + System.out.println("ID是"+id+":姓名是:"+name+";性别是:"+sex); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句执行错误!"); + e.printStackTrace();//显示错误 + }finally{ + //释放资源 + try {//注意倒着来 + if (result != null) { + result.close(); + } + if (sta != null) { + sta.close(); + } + if (conn != null) { + conn.close(); + } + } 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 { + //第二步 加载MYSQL 驱动类 + Class.forName("com.mysql.jdbc.Driver"); + // 2.创建连接 + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8","root","root");//后面跟数据库的名字 + //3.编写SQL的语句 + String sql ="delete from student where name="张三";//dql//注意这里查询的是表,所以写的是表名 + //4. 获取执行SQL的对象 + Statement statement = conn.createStatement(); + //5.执行语句 + int i = statement.executeUpdate(sql); + //6.返回结果 + if (i >0) { + System.out.println("删除成功!"); + }else { + System.out.println("删除失败!"); + } + //7.释放资源 + statement.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句执行错误!"); + e.printStackTrace(); + } + } +} +``` + +修改语句 + +```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) { + //修改语句 + Connection conn =null; + Statement sta = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + //创建连接 + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8","root","root");//后面跟数据库的名字 + //编写SQL的语句 + String sql ="update student set name='李四' where id =2";//dql//注意这里查询的是表,所以写的是表名 + //获取执行SQL的对象 + sta = conn.createStatement(); + //sta执行SQL + int i = sta.executeQuery(sql); + //处理返回结果 + if (i >0) { + System.out.println("修改成功"+i+"行!"); + }else { + System.out.println("修改失败!"); + } + + + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句执行错误!"); + e.printStackTrace();//显示错误 + }finally{ + //释放资源 + try{ + sta.close(); + conn.close(); + } + catch (SQLException e) { + System.out.println("SQL语句执行错误!"); + e.printStackTrace();//显示错误 + } + +``` + +插入语句 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Test2 { + public static void main(String[] args) { + try { + //添加语句 + //第二步 加载MYSQL 驱动类 + Class.forName("com.mysql.jdbc.Driver"); + // 2.创建连接 + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8","root","root");//后面跟数据库的名字 + //3.编写SQL的语句 + String sql ="insert into student value(4,'罗云熙','男')";//dql//注意这里查询的是表,所以写的是表名 + //4. 获取执行SQL的对象 + Statement statement = conn.createStatement(); + //5.执行语句 + int i = statement.executeUpdate(sql); + //6.处理返回值 + if (i >0) { + System.out.println("插入成功!"); + }else{ + System.out.println("插入失败"); + } + //7.释放资源 + statement.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句执行错误!"); + e.printStackTrace();//显示错误 + } + + + } +} +``` +