diff --git "a/48 \346\235\250\346\264\213/20230521JDBC\344\275\234\344\270\232.md" "b/48 \346\235\250\346\264\213/20230521JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..8d00c2df491d8a7b99cdc942185c4f4ca4d4e101 --- /dev/null +++ "b/48 \346\235\250\346\264\213/20230521JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,233 @@ +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + + ```sql + CREATE DATABASE student_db CHARSET utf8; + use student_db; + CREATE TABLE student( + id int , + name VARCHAR(4), + sex enum("男","女") + ); + INSERT into student VALUES + (1,"张三","男"),(2,"李四","女"),(3,"王五","男"); + ``` + + + +1. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + + public class composite { + public static void main(String[] args) throws SQLException, ClassNotFoundException { + select1(); + add2(); + update3(); + delete4(); + } + public static void select1() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + + //String url="jdbc:mysql://localhost/user?useSSL=false"; + String url="jdbc:mysql://localhost/student_db?useSSL=false"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + + //String sql="select * from math"; + String sql="select * from student"; + + Statement st = conn.createStatement(); + + ResultSet rs = st.executeQuery(sql);//专用来执行select语句,因为它会返回一个结果集 + //int i = st.executeUpdate(sql);//用来执行除了select以外的语句,返回的是一个受影响的函数 + + while (rs.next()){ + //结果集取值方式,结果集对象名.getXxx(索引,or 字段名) Xxx是数据类型 + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+","+name+","+sex); + } + rs.close(); + st.close(); + conn.close(); + } + public static void add2() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + //避免乱码,在连接时,设置编码和字符集 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + + String sql="insert into student values (10,'朱元璋','男')"; // 添加 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("添加成功了" + i + "行"); + } else System.out.println("添加失败" + i + "行"); + st.close(); + conn.close(); + } + public static void update3() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + //Connection conn = DriverManager.getConnection("jbdc:mysql//localhost:3306/user?useSSL=false", "root", "root"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + //避免乱码,在连接时,设置编码和字符集 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "update student set name='周京泽' where id=10"; //修改 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("修改成功了" + i + "行"); + } else System.out.println("修改失败" + i + "行"); + st.close(); + conn.close(); + } + public static void delete4() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + //Connection conn = DriverManager.getConnection("jbdc:mysql//localhost:3306/user?useSSL=false", "root", "root"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + //避免乱码,在连接时,设置编码和字符集 + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="delete from student where id=2"; //删除 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("删除成功了" + i + "行"); + } else System.out.println("删除失败" + i + "行"); + st.close(); + conn.close(); + } + } + + ``` + + + +2. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + + 2. 能否实现将查询的结果,封装成java对象 + + 3. ~~~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 + '\'' + + '}'; + } + } + + ``` + + ```java + 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