想知道java连数据库的servlet的删除和修改方法怎么写,请好心人详细指点,麻烦写具体点。可追加50
想把已有的添加功能的servlet发来,可是发不上来,请有意回答的好心人留个q-q或邮-箱神马的,我发给你。能直接把这些servlet的doGet&doPost方法写下来...
想把已有的添加功能的servlet发来,可是发不上来,请有意回答的好心人留个q-q或邮-箱神马的,我发给你。能直接把这些servlet的doGet &doPost方法写下来更加欢迎!不胜感激!
展开
展开全部
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;
}
}
大体就是这个样子喽
/**
*
*/
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;
}
}
大体就是这个样子喽
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询