diff --git "a/21 \345\210\230\345\260\221\346\265\267/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" "b/21 \345\210\230\345\260\221\346\265\267/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..f7bb415962b145ec2be086606f7509eca712ab32 --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,133 @@ +### 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对象 + + 3. ```java + package M5D19; + + import java.sql.*; + import java.util.Scanner; + + public class Mysql { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入功能"); + switch (sc.next()){ + case "添加数据": + insErt(); + break; + case "删除数据": + delEte(); + break; + case "修改数据": + updAte(); + break; + case "查询数据": + selEct(); + break; + case "退出系统": + System.out.println("退出成功"); + break; + default: + System.out.println("没有这个功能"); + } + } + public static void insErt(){ + try { + Class.forName("com.mysql.jdbc.Driver");//加载插件 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password);//链接对象 + Statement st = conn.createStatement();//创建对象 + int i = st.executeUpdate("insert student values(1,'张三','男')");//执行语句 + System.out.println("添加"+i+"行");//输出 + conn.close(); + st.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }; + public static void delEte(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEnding=utf8","root","root"); + Statement st = conn.createStatement(); + int zx = st.executeUpdate("delete from student where id = 11"); + System.out.println("删除"+zx+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }; + public static void updAte(){ + try { + Class.forName("com.mysql.jdbc.Driver");//加载插件 + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root");//链接对象 + Statement st = conn.createStatement();//创建对象 + int zx = st.executeUpdate("update student set id=11,name='liu',sex='M' where id = 1");//执行语句 + System.out.println("修改"+zx+"行"); + conn.close(); + st.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }; + public static void selEct(){ + try { + Class.forName("com.mysql.jdbc.Driver");//加载插件 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password);//链接对象 + Statement st = conn.createStatement();//创建对象 + ResultSet zx = st.executeQuery("select * from student"); + while (zx.next()){ + System.out.println(zx.getString("id")+"\t" + +zx.getString("name")+"\t" + +zx.getString("sex"));; + } + zx.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }; + } + + ``` + + \ No newline at end of file diff --git "a/21 \345\210\230\345\260\221\346\265\267/20230521 jdpc.md" "b/21 \345\210\230\345\260\221\346\265\267/20230521 jdpc.md" new file mode 100644 index 0000000000000000000000000000000000000000..d8cbc96759dafe11fe0bd288533ad114f94ada6d --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/20230521 jdpc.md" @@ -0,0 +1,64 @@ +```java +package 作业; + +import java.sql.*; +import java.util.ArrayList; + +public class Gongju { + 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", "root"); + 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]); + 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(); + } + } +} +package 作业; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Testi { + static class test{ + public static void main(String[] args) throws SQLException { + String sql="select * from student"; + ResultSet res = Gongju.Query(sql); + ArrayList list = new ArrayList<>(); + while (res.next()){ + list.add(new Studenti(res.getString(1),res.getString(2),res.getString(3))); + } + for (int i = 0; i < list.size(); i++) { + Studenti student = list.get(i); + System.out.println(student); + } + Gongju.close(null,null,res); + } + } +} + +``` +