From 76b9fc1314905a82852d94e35620260f3288d2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=99=BA=E7=A0=94?= <3058944672@qq.com> Date: Sun, 21 May 2023 23:26:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\254\241\344\275\234\344\270\232.md.txt" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "46 \350\203\241\346\231\272\347\240\224/20230521\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md.txt" diff --git "a/46 \350\203\241\346\231\272\347\240\224/20230521\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md.txt" "b/46 \350\203\241\346\231\272\347\240\224/20230521\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md.txt" new file mode 100644 index 0000000..2be2ffb --- /dev/null +++ "b/46 \350\203\241\346\231\272\347\240\224/20230521\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md.txt" @@ -0,0 +1,61 @@ +java +package 作业; + +import java.sql.*; +import java.util.ArrayList; + +public class Gongju { + 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", "root"); + 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]); + 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(); + } + } +} +package 作业; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Testi { + static class test{ + public static void main(String[] args) throws SQLException { + String sql="select * from student"; + ResultSet res = Gongju.Query(sql); + ArrayList list = new ArrayList<>(); + while (res.next()){ + list.add(new Studenti(res.getString(1),res.getString(2),res.getString(3))); + } + for (int i = 0; i < list.size(); i++) { + Studenti student = list.get(i); + System.out.println(student); + } + Gongju.close(null,null,res); + } + } +} -- Gitee