diff --git "a/11 \345\276\220\347\253\213\345\237\216/JDBC\344\275\234\344\270\232\357\274\232.md" "b/11 \345\276\220\347\253\213\345\237\216/JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..96d5dfe72a2591db5e3f293509fe1c2ba9822707 --- /dev/null +++ "b/11 \345\276\220\347\253\213\345\237\216/JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,123 @@ +### 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.*; +import java.sql.ResultSet; +public class d1 { + public static void main(String[] args) { + try { + selectAll(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static void selectAll() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + String utl = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password ="root"; + Connection zzzz =DriverManager.getConnection(utl,username,password); + String sql= "SELECT * FROM student"; + Statement st= zzzz.createStatement(); + ResultSet 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); + } + rs.close(); + st.close(); + zzzz.close(); + + } + public static void select() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + String utl = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username="root"; + String password ="root"; + Connection zzzz =DriverManager.getConnection(utl,username,password); + String sql= "DELETE FROM student WHERE id=2"; + Statement st= zzzz.createStatement(); + int i=st.executeUpdate(sql); + if(i>0){ + System.out.println("成功"); + }else { + System.out.println("失败"); + } + st.close(); + zzzz.close(); + } + public static void selectA() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + String utl = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + + String username="root"; + String password ="root"; + Connection zzzz =DriverManager.getConnection(utl,username,password); + String sql= "INSERT INTO student VALUES(1,'红','男')"; + Statement st= zzzz.createStatement(); + int i=st.executeUpdate(sql); + if(i>0){ + System.out.println("成功"); + }else { + System.out.println("失败"); + } + st.close(); + zzzz.close(); + } + public static void selectAL() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + String utl = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + + String username="root"; + String password ="root"; + Connection zzzz =DriverManager.getConnection(utl,username,password); + String sql= "UPDATE student set `name`='大红' WHERE id=1"; + Statement st= zzzz.createStatement(); + int i=st.executeUpdate(sql); + if(i>0){ + System.out.println("成功"); + }else { + System.out.println("失败"); + } + st.close(); + zzzz.close();} +} +``` + +CREATE DATABASE student_db charset utf8 ; +use student_db; +CREATE TABLE student( +id int , +name VARCHAR(20), +sex VARCHAR(20) +); +insert into student VALUES(1,"张三","男"),(2,"李四","女"),(3,"王五","男"); +SELECT * FROM student; \ No newline at end of file diff --git "a/11 \345\276\220\347\253\213\345\237\216/\344\275\234\344\270\232 java.md" "b/11 \345\276\220\347\253\213\345\237\216/\344\275\234\344\270\232 java.md" new file mode 100644 index 0000000000000000000000000000000000000000..619daa982ffb44c2c154b8801bdf286081d476d9 --- /dev/null +++ "b/11 \345\276\220\347\253\213\345\237\216/\344\275\234\344\270\232 java.md" @@ -0,0 +1,64 @@ +```java +import java.sql.ResultSet; +import java.sql.SQLException; + +public class d3 { + public static void main(String[] args) throws SQLException { + String sql = "SELECT * FROM student "; + ResultSet rs = D2.select(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); + String scl= "DELETE FROM student WHERE id=2"; + int i = D2.update(sql,""); + if(i>0){ + System.out.println("成功"); + }else { + System.out.println("失败"); + } + } + } +} +``` + +```java +import java.sql.*; + +public class D2 { + private static final String utl = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + 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(utl, username, password); + return conn; + } + + public static ResultSet select(String sql,String ...keys) throws SQLException { + Connection coon =getConn(); + PreparedStatement pst = coon.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + ResultSet rs = pst.executeQuery(); + return rs; + } + public static int update(String sql, String ...keys) throws SQLException { + Connection coon =getConn(); + PreparedStatement pst = coon.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((0+1),keys[i]); + } + int i = pst.executeUpdate(); + return i; + } +} +``` \ No newline at end of file