我想将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;
%>
home idData.....
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM table";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
...
}
} catch (Exception e) {
e.printStackTrace();
}
%>