想知道java连数据库的servlet的删除和修改方法怎么写,请好心人详细指点,麻烦写具体点。可追加50

想把已有的添加功能的servlet发来,可是发不上来,请有意回答的好心人留个q-q或邮-箱神马的,我发给你。能直接把这些servlet的doGet&doPost方法写下来... 想把已有的添加功能的servlet发来,可是发不上来,请有意回答的好心人留个q-q或邮-箱神马的,我发给你。能直接把这些servlet的doGet &doPost方法写下来更加欢迎!不胜感激! 展开
 我来答
dngoryaner
推荐于2018-04-27 · TA获得超过2350个赞
知道小有建树答主
回答量:2404
采纳率:0%
帮助的人:1137万
展开全部
public class testServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
Connection conn = null;

public testServlet() {
if (null == conn) {
conn = getConnection();
}
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String service = request.getParameter("serviceMethod");
if ("add".equalsIgnoreCase(service)) {
getAdd(request, response);
}
else if ("update".equalsIgnoreCase(service)) {
getUpdate(request, response);
}
else if ("delete".equalsIgnoreCase(service)) {
getDelet(request, response);
}
else if ("search".equalsIgnoreCase(service)) {
getSearch(request, response);
}
else {
// error
}

}
private void getAdd(HttpServletRequest request, HttpServletResponse response) throws Exception {
Statement stat = conn.createStatement();
stat.executeUpdate("insert into test values(1)");
stat.close();
conn.close();
}

private void getDelet(HttpServletRequest request, HttpServletResponse response) throws Exception {
Statement stat = conn.createStatement();
stat.executeUpdate("delete test ");
stat.close();
conn.close();
}

private void getUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
Statement stat = conn.createStatement();
stat.executeUpdate("update test set a=" + request.getParameter("test"));
stat.close();
conn.close();
}

private void getSearch(HttpServletRequest request, HttpServletResponse response) throws Exception {
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM test");
result.close();
stat.close();
conn.close();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
try {
processRequest(request, response);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:binbo";
String user = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;

}
catch (ClassNotFoundException en) {
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}
catch (Exception e) {
System.out.println("其他未知异常!");
e.printStackTrace();
}
return null;
}
}

大体就是这个样子喽
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式