From 249ac07bf419a50b9e6604d7b713f1b9bc6ad2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E4=BC=9F?= <2152890632@qq.com> Date: Thu, 18 May 2023 00:09:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\344\275\234\344\270\232.md" | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 "06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8c804b1 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230518 js\345\220\210\345\271\266\344\275\234\344\270\232.md" @@ -0,0 +1,167 @@ +# 查询 + +```java +import java.sql.*; + +public class Select { + public static void main(String[] args) { + //0.导包 + //1.注册驱动 + ResultSet rst = null; + Connection conn = null; + Statement smt = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.连接对象 + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3.编写sql语句 + String sql = "select * from biao"; + //4.获取执行sql的对象 + smt = conn.createStatement(); + //5.smt执行sql + rst = smt.executeQuery(sql); + //6.处理返回的结果 + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + double money = rst.getDouble("money"); + System.out.println(id + ":" + name + "-" + money); + } + //7.释放资源 + + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } finally { + try { + if (rst!=null){ + rst.close(); + } + if (conn!=null){ + conn.close(); + } + if (smt!=null) { + smt.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } +} + +``` + +# 添加 + +```java +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Inset { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="insert into biao values('黎明',6,1000)"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + +# 删除 + +```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) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="delete from biao where id = 6"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功"+i+"条"); + }else { + System.out.println("删除失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + +# 修改 + +```java +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"); + String url = "jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql="update biao set name ='李明' where id = 6"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if (i>0){ + System.out.println("修改成功"+i+"条"); + }else { + System.out.println("修改失败"); + } + conn.close(); + sta.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入"); + } catch (SQLException e) { + System.out.println("sql语句执行错误"); + } + } +} + +``` + -- Gitee From ca03747743f9a4013d5b3573b63ef4a861fb55a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E4=BC=9F?= <2152890632@qq.com> Date: Mon, 22 May 2023 23:53:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=81=8D=E5=8E=86=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\216\206\346\225\260\347\273\204.md" | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 "06 \351\231\210\345\277\227\344\274\237/20230522 js\351\201\215\345\216\206\346\225\260\347\273\204.md" diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230522 js\351\201\215\345\216\206\346\225\260\347\273\204.md" "b/06 \351\231\210\345\277\227\344\274\237/20230522 js\351\201\215\345\216\206\346\225\260\347\273\204.md" new file mode 100644 index 0000000..2021703 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230522 js\351\201\215\345\216\206\346\225\260\347\273\204.md" @@ -0,0 +1,140 @@ +## 题目,如何将这些id,name,sex封装成对应的对象,并存入一个集合中。最后遍历这个集合,输出所有的对象的信息。 + +```java +import javax.xml.namespace.QName; + +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.*; +public class All { + private static final String url = "jdbc:mysql:///user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String use = "root"; + private static final String password = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, use, password); + } catch (SQLException e) { + System.out.println("获取失败"); + } + return conn; + } + public static ResultSet getSql(String sql,String ...keys){ + ResultSet re = null; + try { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + re = pre.executeQuery(); + } catch (SQLException e) { + System.out.println("sql异常"); + e.printStackTrace(); + } + return re; + } + public static int getUpdat(String sql,String ...keys){ + int num = 0; + try { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql异常"); + e.printStackTrace(); + } + return num; + } +} + +``` + +```java +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Text { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from biao"; + ResultSet res = All.getSql(sql); + try { + while (res.next()){ + int id = res.getInt(1); + String name = res.getString(2); + String sex = res.getString(3); + list.add(new Student(id,name,sex)); + } + } catch (SQLException e) { + System.out.println("sql异常"); + e.printStackTrace(); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } +} + +``` + -- Gitee