我想将Java代码(在servlet中)与html代码分开。
此代码显示了jsp中mySql表的数据。
不使用脚本的最佳实践是什么?
谢谢你的帮助。
<%
String id = request.getParameter("userId");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost/";
String dbName = "db";
String userId = "root";
String password = "root";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<table>
<td style="border:none"><a href="index.jsp" class="LinkButton">home</a> <br></td>
<tr>
<th>id</th>
<th>Data</th>
.....
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM table";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("id") %></td>
<td><%=resultSet.getString("Data") %></td>
...
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
慕的地8271018
当年话下
Smart猫小萌
随时随地看视频慕课网APP
相关分类