diff --git "a/30 \350\256\270\346\201\251\346\260\221/jdbc\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/30 \350\256\270\346\201\251\346\260\221/jdbc\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..91c438c4abcc4901b0bbf0e2dd18b6843c9cf668 --- /dev/null +++ "b/30 \350\256\270\346\201\251\346\260\221/jdbc\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,239 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + package day519; + + import java.sql.*; + + public class select { + public static void main(String[] args) { + ResultSet rs = null; + Statement st = null; + Connection con = null; + try { + //注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + String user = "root"; + String password = "root"; + con = DriverManager.getConnection(url, user, password); + //编写sql语句 + String sql = "select *from student"; + //获取执行sql的对象 + st = con.createStatement(); + //执行sql + rs = st.executeQuery(sql); + //处理返回的结果 + while (rs.next()) { + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id + name + sex); + } + + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + //释放资源 + try { + rs.close(); + st.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + + } + } + + ``` + + 2. 添加功能 + + ```java + package day519; + + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class add { + public static void main(String[] args) { + Statement st = null; + Connection con = null; + try { + //注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + con = DriverManager.getConnection(url, user, password); + //编写sql语句 + String sql = "insert into student values('1', '张三' ,'男')"; + //获取执行sql的对象 + st = con.createStatement(); + //执行sql + int i = st.executeUpdate(sql); + //处理结果 + if (i > 0) { + System.out.println("您已添加了" + i + "行数据"); + } else { + System.out.println("添加失败"); + System.out.println("您已添加了" + i + "行数据"); + + } + //释放资源 + + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + st.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } + } + + ``` + + 3. 修改功能 + + ```java + package day519; + + 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) { + Statement st = null; + Connection con = null; + try { + //注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + con = DriverManager.getConnection(url, user, password); + //编写sql语句 + String sql = "update students set name='小琪' where id=1"; + st = con.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("您已修改了" + i + "行数据"); + } else { + System.out.println("修改失败"); + System.out.println("您已修改了" + i + "行数据"); + } + st.close(); + con.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + st.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + } + + ``` + + 4. 删除功能 + + ```java + package day519; + + 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 st = null; + Connection con = null; + try { + //注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + con = DriverManager.getConnection(url, user, password); + //编写sql语句 + String sql = "delete from student where id=1"; + //获取执行sql的对象 + st = con.createStatement(); + //执行sql + int i = st.executeUpdate(sql); + //处理结果 + if (i > 0) { + System.out.println("您已删除了" + i + "行数据"); + } else { + System.out.println("删除失败"); + System.out.println("您已删除了了" + i + "行数据"); + + } + + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + st.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + } + + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b16895f2f1ec65a91af87642d16abaa7cdfda1c1 --- /dev/null +++ "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,98 @@ +```java +public class Student { + private int id; + private String name; + private String sex; + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public Student() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} +import java.sql.*; +import java.util.ArrayList; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&character=utf8", "root", "122319"); + return conn; + } + public static ResultSet Query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + ResultSet res = pre.executeQuery(); + return res; + } + public static void close(Connection con,PreparedStatement pre,ResultSet res) throws SQLException { + if(res!=null){ + res.close(); + }if(pre!=null){ + pre.close(); + }if(con!=null){ + con.close(); + } + } +} +class Test{ + public static void main(String[] args) throws SQLException { + String sql="select * from student"; + ResultSet res = DBUtil.Query(sql); + ArrayList list = new ArrayList<>(); + while (res.next()){ + list.add(new Student(res.getInt(1),res.getString(2),res.getString(3))); + } + for (int i = 0; i < list.size(); i++) { + Student student = list.get(i); + System.out.println(student); + } + DBUtil.close(null,null,res); + } +} +``` \ No newline at end of file