<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
String sql = "SELECT * FROM users WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 1);
rs = pstmt.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
String email = rs.getString("email");
out.println("用戶名:" + name + "
"); out.println("郵箱:" + email + "
"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } %>
"); out.println("郵箱:" + email + "
"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } %>
百度分享代碼,如果開啟HTTPS請參考李洋個人博客

還沒有評論,來說兩句吧...