diff --git "a/44 \344\273\243\347\221\236/5\346\234\21017\346\227\245\345\210\235\345\255\246jsp.md" "b/44 \344\273\243\347\221\236/5\346\234\21017\346\227\245\345\210\235\345\255\246jsp.md" new file mode 100644 index 0000000000000000000000000000000000000000..49f07c1911d18706d4017d3642013f7ccf8ed9bb --- /dev/null +++ "b/44 \344\273\243\347\221\236/5\346\234\21017\346\227\245\345\210\235\345\255\246jsp.md" @@ -0,0 +1,193 @@ +### mysql + +``` mysql +create database if not exists aaa charset utf8; +use aaa; +create table Student( +id int, +name varchar(4), +sex enum('女','男') +); +insert into Student value +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +select *from Student; +``` + +## 添加 + +``` java +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) { + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "insert into Student value(4,'曹贼','男'),(5,'曹正波','男')"; + str = con.createStatement(); + int i = str.executeUpdate(sql); + if (i > 0) { + System.out.println("插入成功"); + } else { + System.out.println("插入失败"); + } + + } catch (ClassNotFoundException e) { + System.out.println("驱动包导入正常"); + } catch (SQLException e) { + System.out.println("mysql语法错误"); + } finally { + try { + str.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +### 修改 + +``` java +import java.sql.*; + +public class update { + public static void main(String[] args) { + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "update Student set id=4 where name='曹正波'"; + str = con.createStatement(); + int i = str.executeUpdate(sql); + if (i > 0) { + System.out.println("修改成功"); + } else { + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("导包错误"); + } catch (SQLException e) { + System.out.println("mysql语法错误"); + }finally { + try { + str.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +## 查询 + +``` java +import java.sql.*; + +public class select { + public static void main(String[] args) { + ResultSet res = null; + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "select*from Student"; + str = con.createStatement(); + res = str.executeQuery(sql); + while (res.next()) { + String id = res.getString("id"); + String name = res.getString("name"); + String sex = res.getString("sex"); + System.out.println(id+"=="+name+"=="+sex); + } + } catch (ClassNotFoundException e) { + System.out.println("导包错误"); + } catch (SQLException e) { + System.out.println("mysql语言错误"); + } finally { + try { + if (res!=null){ + res.close(); + } + if (str!=null){ + str.close(); + } + if (con!=null){ + con.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +## 删除 + +``` java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class delect { + public static void main(String[] args) { + Statement str = null; + Connection con = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + con = DriverManager.getConnection(url, usename, password); + String sql = "delete from Student where id=4"; + str = con.createStatement(); + int i = str.executeUpdate(sql); + if (i > 0) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + + } catch (ClassNotFoundException e) { + System.out.println("导包异常"); + } catch (SQLException e) { + System.out.println("mysql语法错误"); + } finally { + try { + str.close(); + con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + diff --git "a/44 \344\273\243\347\221\236/JDBDC\345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" "b/44 \344\273\243\347\221\236/JDBDC\345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c66254b7097552a10651f03de9718df026712476 --- /dev/null +++ "b/44 \344\273\243\347\221\236/JDBDC\345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" @@ -0,0 +1,204 @@ +## studnet + +``` mysql +CREATE TABLE `studnet` ( + `id` int(11) NOT NULL, + `name` varchar(255) DEFAULT NULL, + `sex` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +INSERT INTO `student` VALUES (1, '张三', '男'); +INSERT INTO `student` VALUES (5, '马达', '男'); +INSERT INTO `student` VALUES (6, '佳炜', '男'); +INSERT INTO `student` VALUES (7, '俊伟', '男'); +INSERT INTO `student` VALUES (8, '帅翔', '男'); +INSERT INTO `student` VALUES (5, '马达', '男'); +INSERT INTO `student` VALUES (6, '佳炜', '男'); +INSERT INTO `student` VALUES (7, '俊伟', '男'); +INSERT INTO `student` VALUES (8, '帅翔', '男'); +INSERT INTO `student` VALUES (5, '马达', '男'); +INSERT INTO `student` VALUES (6, '佳炜', '男'); +INSERT INTO `student` VALUES (7, '俊伟', '男'); +INSERT INTO `student` VALUES (8, '帅翔', '男'); + +SET FOREIGN_KEY_CHECKS = 1; +``` + +## Stu + +``` java +public class Stu { + private int id; + private String name; + private String sex; + + public Stu() { + } + + public Stu(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + 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 "Stu{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} + +``` + +## 增删改查 + +``` +public static Connection conget(){ + Connection con = null; + try { + con = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("连接异常"); + } + return con; +} +public static ResultSet qure(String sql,String...keys) {//查询 + + PreparedStatement pre = null; + ResultSet res = null; + try { + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + res = pre.executeQuery(); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return res; +} +public static int insert(String sql,String...keys) {//添加 + int num = 0; + PreparedStatement pre = null; + try { + pre = null; + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return num; +} +public static int update(String sql,String...keys) {//修改 + int num = 0; + PreparedStatement pre = null; + try { + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return num; +} +public static int delete(String sql,String...keys) {//删除 + int num = 0; + PreparedStatement pre = null; + try { + Connection con = conget(); + pre = con.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i + 1), keys[i]); + } + num = pre.executeUpdate(); + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + pre.close(); + conget().close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + return num; +} +``` + +## Test + +```java +import java.sql.ResultSet; + +public class Test { + public static void main(String[] args) { + String sql="select * from studnet"; + ResultSet qure = Text.qure(sql);//查询 +// String sql1="insert into Studnet value(6,'马达','女'),(7,'佳炜','男'),(8,'俊伟','男'),(9,'帅翔','男'),(10,'帅翔','男'); "; +// int insert = Text.insert(sql1);//增添 +// String sql2="update Studnet set sex='男' where id=6";//修改 +// int update = Text.update(sql2); +// String sql3="delete from Studnet where id=10";//删除 +// int delete = Text.delete(sql3); +// } +} +} + +``` +