jsp连接MySQL数据库代码:
- <%@ page contentType="text/html; charset=GBK" language="java"
- errorPage=""%>
- <%@ page import="java.sql.*"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>小脚本测试</title>
- <meta name="website" content="http://www.crazyit.org" />
- </head>
- <body bgcolor="lightpink">
- <%
- Class.forName("com.mysql.jdbc.Driver");
- //填的是数据库名
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/user", "root", "root");
- Statement stmt = conn.createStatement();
- //填的是表名
- ResultSet rs = stmt.executeQuery("select * from employee");
- %>
- <table bgcolor="lightgreen" border="1" width="300">
- <%
- while (rs.next()) {
- %>
- <tr>
- <td><%=rs.getString(1)%></td>
- <td><%=rs.getString(2)%></td>
- </tr>
- <%
- }
- %>
- <table>
- </body>
- </html>