第一步:连接数据库,并取得查询结果:import java.sql.*
1
2
3
4
5
6
7
8
9
10
11
12
13
public class OraDriver{
public static void main(String args[]) throws Exception{
//利用Oracle驱动进行连接
Class.forName("oracle.jdbc.driver.OracleDriver")//载入驱动
String url = "jdbc:oracle:thin:@localhost:1521:SCHOOL"
Connection conn = DriverManager.getConnection(url,"scott","tiger")
Statement stat = conn.createStatement()
ResultSet rs = stat.executeQuery("SELECT 姓名 FROM 学籍")
while(rs.next()){
System.out.println(rs.getString("姓名"))
}
}
}
2.在JSP上显示可以用EL表达式如${user.getName()},也可以用JAVA胸本<%=user.getName()%>,或都直接用out.print(user.getName())
步骤如下:1、在web工程里面创建一个Servlet类,继承HttpServlet,重写doPost,doGet方法,在doPost方法中调用doGet方法;
2、在doGet方法中把要设置到jsp页面的值存到request中;
3、在doGet方法中添加转发到jsp页面的代码;
4、在jsp页面中使用jstl标签获取存入的值。
事例代码如下:
Servlet类:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("name", "nameValue")
request.getRequestDispatcher("/demo.jsp").forward(request, response)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)