/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Arrays;
import javax.servlet.ServletConfig;
/**
*
* @author Jim Ryan Pasatiempo
*/
public class JdbcController extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
Connection conn;
PreparedStatement ps;
String query;
String[] args;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
this.query = config.getInitParameter("queryStatement");
this.args = config.getInitParameter("arguments").split(",");
System.out.println("Query Statement: " + this.query);
System.out.println("Arguments: " + Arrays.toString(this.args));
System.out.println("Arguments Length: " + this.args.length);
Class.forName(config.getInitParameter("jdbcClassName"));
System.out.println("JDBC ClassName: " + config.getInitParameter("jdbcClassName"));
String username = config.getInitParameter("dbUserName");
String password = config.getInitParameter("dbPassword");
String url = config.getInitParameter("jdbcDriverURL") + "://" + config.getInitParameter("dbHostName") + ":" + config.getInitParameter("dbPort") + "/" + config.getInitParameter("databaseName");
this.conn = DriverManager.getConnection(url, username, password);
this.ps = this.conn.prepareStatement(this.query);
} catch (SQLException sqle) {
System.out.println("SQLException error occured - "
+ sqle.getMessage());
} catch (ClassNotFoundException nfe) {
System.out.println("ClassNotFoundException error occured - "
+ nfe.getMessage());
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
if (conn != null) {
ResultSet rs = null;
String actionPerformed = "none";
if(this.query.contains("SELECT")) {
actionPerformed = "SELECT";
for(int i = 0, j = 1; i < this.args.length; i++, j++) {
try {
ps.setInt(j, Integer.parseInt(this.args[i].trim()));
} catch(NumberFormatException nfe) {
try {
ps.setString(j, this.args[i].trim());
} catch(SQLException sqle) {
break;
}
} catch(SQLException sqle) {
break;
}
}
rs = ps.executeQuery();
System.out.println("Sending SELECT information");
} else if (this.query.contains("INSERT")) {
actionPerformed = "INSERT";
for(int i = 0, j = 1; i < this.args.length; i++, j++) {
try {
ps.setInt(j, Integer.parseInt(this.args[i].trim()));
} catch(NumberFormatException nfe) {
try {
ps.setString(j, this.args[i].trim());
} catch(SQLException sqle) {
break;
}
} catch(SQLException sqle) {
break;
}
}
ps.executeUpdate();
Statement stmt = this.conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM STUDENTDB ORDER BY STUDENT_NAME");
System.out.println("Student/s Added!");
} else if (this.query.contains("UPDATE")) {
actionPerformed = "UPDATE";
for(int i = 0, j = 1; i < this.args.length; i++, j++) {
try {
ps.setInt(j, Integer.parseInt(this.args[i].trim()));
} catch(NumberFormatException nfe) {
try {
ps.setString(j, this.args[i].trim());
} catch(SQLException sqle) {
break;
}
} catch(SQLException sqle) {
break;
}
}
ps.executeUpdate();
Statement stmt = this.conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM STUDENTDB ORDER BY STUDENT_NAME");
System.out.println("Student/s Updated!");
} else if (this.query.contains("DELETE")) {
actionPerformed = "DELETE";
for(int i = 0, j = 1; i < this.args.length; i++, j++) {
try {
ps.setInt(j, Integer.parseInt(this.args[i].trim()));
} catch(NumberFormatException nfe) {
try {
ps.setString(j, this.args[i].trim());
} catch(SQLException sqle) {
break;
}
} catch(SQLException sqle) {
break;
}
}
ps.executeUpdate();
Statement stmt = this.conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM STUDENTDB ORDER BY STUDENT_NAME");
System.out.println("Student/s Deleted!");
} else {
}
System.out.println("==========END==========");
request.setAttribute("actionPerformed", actionPerformed);
request.setAttribute("results", rs);
request.getRequestDispatcher("Display Results.jsp").forward(request, response);
} else {
System.out.println("Here 1");
response.sendRedirect("error.html");
}
} catch (SQLException sqle) {
System.out.println("Here 2");
response.sendRedirect("error.html");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
没有合适的资源?快使用搜索试试~ 我知道了~
Lab-Exercise-5.zip_Played
共23个文件
xml:9个
properties:3个
jsp:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 155 浏览量
2022-09-23
18:42:40
上传
评论
收藏 30KB ZIP 举报
温馨提示
. You have the results of a completed round-robin tournament in which n teams played each other once. Each game ended either with a victory for one of the teams or with a tie. Design an algorithm that lists the teams in a sequence so that every team did not lose the game with the team listed immediately after it. What is the time efficiency class of your algorithm?
资源推荐
资源详情
资源评论
收起资源包目录
Lab-Exercise-5.zip (23个子文件)
Lab Exercise 5
test
build.xml 3KB
src
conf
MANIFEST.MF 25B
java
controller
JdbcController.java 7KB
nbproject
build-impl.xml 79KB
private
private.xml 230B
private.properties 689B
ant-deploy.xml 9KB
project.xml 663B
genfiles.properties 473B
project.properties 5KB
web
Display Results.jsp 1KB
error.html 295B
WEB-INF
web.xml 2KB
glassfish-web.xml 523B
build
empty
web
Display Results.jsp 1KB
gfv3ee6.dpf 523B
error.html 295B
META-INF
MANIFEST.MF 25B
WEB-INF
web.xml 2KB
classes
.netbeans_automatic_build 0B
.netbeans_update_resources 0B
controller
JdbcController.class 6KB
glassfish-web.xml 523B
generated-sources
ap-source-output
共 23 条
- 1
资源评论
邓凌佳
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功