jsp页面直接读取mysql数据库数据显示
jsp页面直接读取mysql数据库数据显示:
<%@page import="java.sql.ResultSet"%> <%@page import="com.mysql.jdbc.Statement"%> <%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>Insert title here</title> </head> <body> <% //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/house", "root", "123456"); //创建Statement Statement stm = (Statement) conn.createStatement(); //执行查询 ResultSet rs = stm.executeQuery("select username,pwd from user"); %> <table border="1" width="300"> <% //遍历结果 while (rs.next()) { %> <tr> <td><%=rs.getString(1)%></td> <td><%=rs.getString(2)%></td> </tr> <% } %> </table> </body> </html>