diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230517.md" "b/29 \347\216\213\351\233\257\351\235\231/20230517.md" new file mode 100644 index 0000000000000000000000000000000000000000..e519d3881138b7ada137ad29a24d5355f2d4c22e --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230517.md" @@ -0,0 +1,186 @@ + CREATE DATABASE student_db charset utf8; + USE student_db; + CREATE table student( + id int, + name VARCHAR(20), + sex enum("男","女") + + ); + INSERT INTO student VALUES + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + + +```javascript + +//查询功能,查询student中所有数据 +import java.sql.*; + +public class select { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="select*from Student"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + ResultSet re = stmt.executeQuery(sql); + + //处理返回结果 + while (re.next()){ + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+name+sex); + } + //释放资源 + re.close(); + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} + +//添加功能 +import java.sql.*; + +public class add { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="insert into student values(4,'傻六','女')"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("增加了"+i+"行"); + }else{ + System.out.println("添加错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} +//修改功能 + +import java.sql.*; + +public class change { + public static void main(String[] args) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="UPDATE student set sex=\"男\"where id=2"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("修改了"+i+"行"); + }else{ + System.out.println("修改错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} +// 删除功能 +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) { + try { + //导入jar包 + //注册驱动 + 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); + //定义SQL语句 + String sql ="DELETE from student WHERE id =4"; + //获取执行SQL的对象 + Statement stmt= conn.createStatement(); + //执行SQL + int i = stmt.executeUpdate(sql); + + //处理返回结果 + if (i>0){ + System.out.println("删除了"+i+"行"); + }else{ + System.out.println("删除错误"); + } + //释放资源 + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + + + + + + +``` + diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" "b/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..9209905c8d0af382503e5a799cfaa168a43cddaf --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230520\344\275\234\344\270\232.md" @@ -0,0 +1,105 @@ +```java + +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历这个集合的对象 + +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); + } +} +```