1、首先明确一点,servlet就是一个继承自javax.servlet.http.HttpServlet的类。
2、范例:使用servlet连接数据库
package com.oracle.project.servletimport java.util.*
import java.io.*
import java.sql.*
import javax.servlet.*
import javax.servlet.http.*
import javax.servlet.annotation.*
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver"
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORACLEDB"
public static final String DBUSER = "scott"
public static final String PASSWORLD = "tiger"
private Connection conn = null
public Connection getConnection() {
try {
if(this.conn == null) {
Class.forName(DBDRIVER)
this.conn = DriverManager.getConnection(DBURL,DBUSER,PASSWORLD)
}
} catch(Exception e) {
e.printStackTrace()
}
return conn
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
PrintWriter out = response.getWriter()
out.println("<html>")
out.println("<body>")
out.println("<h1>" + this.getConnection() + "</h1>")
out.println("</body>")
out.println("</html>")
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
this.doGet(request,response)
}
}
第一步: 连接数据库第二部: 查询出数据(可以加入条件) 并通过( jstl \ el )展现到页面
第三部 : 在你展现数据的最后添加一列( *** 作列:有删除、修改)
第四部:进行相关 *** 作
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)