From 0fbd7cf9eadcba953574299392aac055fcba6aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=99=BA=E7=BF=94?= <2045602860@qq.com> Date: Thu, 25 May 2023 02:20:04 +0000 Subject: [PATCH] =?UTF-8?q?34=20=E5=88=98=E6=99=BA=E7=BF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘智翔 <2045602860@qq.com> --- ...45\346\225\260\346\215\256\345\272\223.md" | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 "34 \345\210\230\346\231\272\347\277\224/web\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" diff --git "a/34 \345\210\230\346\231\272\347\277\224/web\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/34 \345\210\230\346\231\272\347\277\224/web\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..da4b92d --- /dev/null +++ "b/34 \345\210\230\346\231\272\347\277\224/web\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,111 @@ +## 查询 + +```java +public class Select { + private static String url="jdbc:mysql:///student_db?useSSL=false&characterEncoding=utf8"; + private static String user="root"; + private static String password="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + System.out.println("驱动包加载异常"); + } + } + public static Connection getconn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + public static ResultSet query(String sql, String ...key){ + ResultSet rs = null; + try { + Connection ge = getconn(); + PreparedStatement prt = ge.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + prt.setString((i+1),key[i]); + } + rs = prt.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } +} +``` + +```java +@WebServlet("/stu") +public class SelectTest extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + PrintWriter wr = resp.getWriter(); + String sql="select * from studen"; + ResultSet que =Select.query(sql); + wr.write("
| 编号 | 姓名 | 性别 |
|---|---|---|
| "+id+" | "+name+" | "+sex+" |