From 5637ae910b282eb0a3161a4d399f7f6c9cfbc362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B8=85=E7=BF=94?= <3371439772@qq.com> Date: Mon, 22 May 2023 11:30:40 +0800 Subject: [PATCH] =?UTF-8?q?5=E6=9C=8821=E6=97=A5=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...21\346\227\245\344\275\234\344\270\232.md" | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 "52\347\250\213\345\270\205\347\277\224/5\346\234\21021\346\227\245\344\275\234\344\270\232.md" diff --git "a/52\347\250\213\345\270\205\347\277\224/5\346\234\21021\346\227\245\344\275\234\344\270\232.md" "b/52\347\250\213\345\270\205\347\277\224/5\346\234\21021\346\227\245\344\275\234\344\270\232.md" new file mode 100644 index 0000000..375affe --- /dev/null +++ "b/52\347\250\213\345\270\205\347\277\224/5\346\234\21021\346\227\245\344\275\234\344\270\232.md" @@ -0,0 +1,174 @@ +## Stu + +``` +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 + +``` +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); +// } +} +} +``` \ No newline at end of file -- Gitee