标签:
JSP中使用JDBC访问MySQL,首先应将mysql-connector-java-5.1.31.jar拷贝到tomcat安装目录下的lib文件夹里,然后再配置classpath。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!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=UTF-8">
<title>使用JDBC访问MySQL</title>
</head>
<body>
<center>
<br><br><br>
<h2>使用JDBC访问MySQL</h2>
<hr>
<table border=2 bgcolor="ccceee" align="center">
<tr>
<th>studentNumber</th>
<th>Name</th>
<th>Sex</th>
<th>Age</th>
<th>Academy</th>
<th>major</th>
</tr>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e) {
out.print(e);
}
Connection connection;
Statement statement;
ResultSet result;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "";
String sql = "SELECT * FROM studentinformation";
try{
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
result = statement.executeQuery(sql);
while(result.next()) {
%>
<tr>
<td><%=result.getString(1) %></td>
<td><%=result.getString(2) %></td>
<td><%=result.getString(3) %></td>
<td><%=result.getString(4) %></td>
<td><%=result.getString(5) %></td>
<td><%=result.getString(6) %></td>
</tr>
<%
}
result.close();
statement.close();
connection.close();
}catch(Exception e1) {
out.println(e1);
}
%>
</table>
</center>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/darrensun/p/4589546.html