diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" "b/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..089646f4d4c254d8a9414e86973d575a72a7f4aa --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20230517 JSP+Servlet JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,195 @@ +### 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对象 + +```mysql +create database student_db charset utf8; +use student_db; +create table student( +id int, +name varchar(50), +sex VARCHAR(50) +); +insert into student VALUES +(1,'张三',"男"), +(2,'李四','女'), +(3,'王五','男'); +``` + + + +```java +select类 + import java.sql.*; + +public class select { + public static void main(String[] args) { + ResultSet re=null; + Statement st=null; + Connection con=null; + try { + Class.forName("com.mysql.jdbc.Driver"); + con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); + String sql = "select * from student"; + st = con.createStatement(); + re = st.executeQuery(sql); + while (re.next()){ + int id = re.getInt("id"); + String name = re.getString("name"); + String sex = re.getString("sex"); + System.out.println(id + "\t" + name + "\t" + sex); + + + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包错误"); + } catch (SQLException e) { + System.out.println("运行错误"); + } finally { + try { + if(re!=null){ + re.close(); + } + if(st!=null){ + st.close(); + } + if(con!=null){ + con.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("资源释放异常"); + } + } + + } +} +``` + +```java +insert类 + import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class insert { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); + String sql = "insert into student values (4,'黒姫結灯','女')"; + Statement st = con.createStatement(); + int i = st.executeUpdate(sql); + + if(i>0){ + System.out.println("成功插入"+i+"行"); + System.out.println("插入成功"); + }else{ + System.out.println("成功插入"+i+"行"); + System.out.println("插入失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包错误"); + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("运行错误"); + } + + + } +} + +``` + +```java +delete类 + 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 { + Class.forName("com.mysql.jdbc.Driver"); + Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); + String sql = "delete from student where name='黒姫結灯'"; + Statement st = con.createStatement(); + int i = st.executeUpdate(sql); + + if(i>0){ + System.out.println("成功删除"+i+"行"); + System.out.println("删除成功"); + }else{ + System.out.println("成功删除"+i+"行"); + System.out.println("=删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("运行包错误"); + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("运行错误"); + } + } +} + +``` + +```java +update类 + 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) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8", "root", "root"); + String sql = "update student set name='一之濑穗波',sex='女' where id=1"; + String sqls="update student set name='砚川·e·泪香' where id=2"; + String ssql="update student set name='冰见山玲',sex='女' where id=3"; + Statement st = con.createStatement(); + int i = st.executeUpdate(sqls); + if(i>0){ + System.out.println("成功修改"+i+"行"); + System.out.println("修改成功"); + }else { + System.out.println("成功修改"+i+"行"); + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包错误"); + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("运行错误"); + } + } +} + +``` + diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230522 jsp+servlet \344\275\234\344\270\232.md" "b/18 \345\276\220\346\260\270\346\267\263/20230522 jsp+servlet \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..0e81277f38ae574a156473d50b6e63686142df17 --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20230522 jsp+servlet \344\275\234\344\270\232.md" @@ -0,0 +1,188 @@ + + +```java +主类型 +import java.sql.*; + +public class DBUtil { + private static final String url = "jdbc:mysql:///amakano2?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user = "root"; + private static final String password = "root"; +static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } +} + +public static Connection getCon() { + Connection con = null; + try { + con = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return con; +} + +public static ResultSet select(String sql, String... key) { + Connection con = getCon(); + ResultSet re = null; + try { + PreparedStatement pr = con.prepareStatement(sql); + + for (int i = 0; i < key.length; i++) { + pr.setString((i + 1), key[i]); + + } + re = pr.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; +} + +public static int insert(String sql, String... key) { + Connection con = getCon(); + int num = 0; + try { + PreparedStatement pr = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + pr.setString((i + 1), key[i]); + } + num = pr.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; +} +public static int update(String sql,String ... key){ + Connection con = getCon(); + int num = 0; + try { + PreparedStatement pr = con.prepareStatement(sql); + + for (int i = 0; i < key.length; i++) { + pr.setString((i+1),key[i]); + } + num = pr.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; +} +public static int delete(String sql,String ... key){ + Connection con = getCon(); + int num = 0; + try { + PreparedStatement preparedStatement = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + preparedStatement.setString((i+1),key[i]); + } + num = preparedStatement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; +} + } +``` + + + + +```java +查询测试类 +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class Select { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + ResultSet se = DBUtil.select("select * from amakano"); + try { + while (se.next()){ + int age=se.getInt("age"); + String name=se.getString("name"); + String address=se.getString("address"); + System.out.println(name+" "+address+"|"+age); + } + } catch (SQLException e) { + e.printStackTrace(); + } + +} + +} +``` + + + + + +```jAVA +插入测试类 +import java.util.Scanner; + +public class Insert { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入要插入的姓名,地址,年龄"); + int i = DBUtil.insert("insert into amakano values (?,?,?)", sc.next(), sc.next(), sc.next()); + int num=DBUtil.insert("insert into amakano values(?,?,?)", sc.next(), sc.next(), sc.next()); + if(num>0){ + System.out.println("插入成功"); + System.out.println("插入"+num+"行"); + }else{ + System.out.println("失败"); + } + } +} +``` + + + + + +```java +修改测试类 +public class Update { + public static void main(String[] args) { + int update = DBUtil.update("update amakano set address=? where name=?","白鹭市","黑姬结灯"); + if(update>0){ + System.out.println("成功修改"+update+"行"); + System.out.println("成功修改"); + }else { + System.out.println("修改失败"); + } + +} + +} +``` + + + + + +```java +删除测试类 +public class Delete { + public static void main(String[] args) { + int delete = DBUtil.delete("delete from amakano where name=?","一之濑穗波"); + if(delete>0){ + System.out.println("成功删除"+delete+"行"); + System.out.println("成功删除"); + }else { + System.out.println("删除失败"); + } + } +} +``` + + + + +