import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Collect extends HttpServlet {
private String getParameter(String string) {
throw new UnsupportedOperationException("Not yet implemented");
}
public void didGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
// Load (and therefore register) the Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Get a Connection to the database
con = DriverManager.getConnection(
"jdbc:oracle:thin:@dbhost:1528:ORCL", "root", "project366");
} catch (ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
} catch (SQLException e) {
out.println("SQLException caught: " + e.getMessage());
} finally {
// Always close the database connection.
try {
if (con != null) {
con.close();
}
} catch (SQLException ignored) {
out.println(ignored);
}
}
String brand = getParameter("ItemBrand");
String category = getParameter("");
String name = getParameter("ItemName");
String model = getParameter("ModelNumber");
String owner = getParameter("OwnerName");
String creator = getParameter("");
String collectability = getParameter("Rarity");
String year = getParameter("ItemYear");
String description = getParameter("ItemDesc");
String subname = getParameter("ItemSubtitle");
String series = getParameter("ItemSeries");
String worth = getParameter("ItemValue");
String haveCondition = getParameter("HaveQuality");
String desiredCondition = getParameter("DesiredQuality");
String owned = getParameter("WantButton");
if (owned.equals("a10")) {
try {
//Load the Driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Get a Connection to the database
con = DriverManager.getConnection("jdbc:derby://localhost:1527/CollectionDB", "root", "project366");
//ADD DATA TO DATABASE
String collectableString = "INSERT INTO COLLECTABLE VALUES(?,?)";
String ownedString = "INSERT INTO ITEM_OWNED VALUES(?,?,?,?,?)";
String qualitiesString = "INSERT INTO QUALITIES_OWNED VALUES(?,?,?,?,?,?,?,?)";
String valueString = "INSERT INTO ITEM_VALUE VALUES(?,?,?,?,?,?)";
PreparedStatement pstC = con.prepareStatement(collectableString);
pstC.setString(1, brand);
pstC.setString(2, category);
PreparedStatement pstO = con.prepareStatement(ownedString);
pstO.setString(1, name);
pstO.setString(2, model);
pstO.setString(3, owner);
pstO.setString(4, brand);
pstO.setString(5, creator);
PreparedStatement pstQ = con.prepareStatement(qualitiesString);
pstQ.setString(1, collectability);
pstQ.setString(2, name);
pstQ.setString(3, model);
pstQ.setString(4, owner);
pstQ.setString(5, year);
pstQ.setString(6, description);
pstQ.setString(7, subname);
pstQ.setString(8, series);
PreparedStatement pstV = con.prepareStatement(valueString);
pstV.setString(1, collectability);
pstV.setString(2, name);
pstV.setString(3, model);
pstV.setString(4, owner);
pstV.setString(5, haveCondition);
pstV.setString(6, worth);
pstC.executeUpdate();
pstO.executeUpdate();
pstQ.executeUpdate();
pstV.executeUpdate();
pstC.close();
pstO.close();
pstQ.close();
pstV.close();
} catch (ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
} catch (SQLException e) {
out.println("SQLException caught: " + e.getMessage());
} catch (Exception e) {
out.println(e);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ignored) {
out.println(ignored);
}
}
try {
String collectableView = "SELECT * FROM COLLECTABLE";
String ownedView = "SELECT * FROM ITEM_OWNED";
String qualitiesView = "SELECT * FROM QUALITIES_OWNED";
String valueView = "SELECT * FROM ITEM_VALUE";
String cView = con.executeSQLQuery(collectableView);
String oView = con.executeSQLQuery(ownedView);
String qView = con.executeSQLQuery(qualitiesView);
String vView = con.executeSQLQuery(valueView);
//TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Your Data has been saved</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet Collect at " + request.getContextPath () + "</h1>");
out.println("<p>Collectable Table</p>");
out.println("<p>" + cView.executeQuery(collectableView) + "</p>");
out.println("<p>Item Owned Table</p>");
out.println("<p>" + oView.executeQuery(ownedView) + "</p>");
out.println("<p>Qualities Owned Table</p>");
out.println("<p>" + qView.executeQuery(qualitiesView) + "</p>");
out.println("<p>Item Value</p>");
out.println("<p>" + vView.executeQuery(valueView) + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
} else if (owned.equals("a20")) {
try {
//Load the Driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Get a Connection to the database
con = DriverManager.getConnection("jdbc:derby://localhost:1527/CollectionDB", "root", "project366");
//ADD DATA TO DATABASE
String collectableString = "INSERT INTO COLLECTABLE VALUES(?,?)";
String desiredString = "INSERT INTO ITEM_DESIRED VALUES(?,?,?,?)";
String qualityWantString = "INSERT INTO QUALITIES DESIRED VALUES(?,?,?,?,?,?,?,?)";
PreparedStatement pstC = con.prepareStatement(collectableString);
pstC.setString(1, brand);
pstC.setString(2, category);
PreparedStatement pstD = con.prepareStatement(desiredString);
pstD.setString(1, name);
pstD.setString(2, model);
pstD.setString(3, owner);
pstD.setString(4, brand);
PreparedStatement pstW = con.prepareStatement(qualityWantString);
pstW.setString(1, name);
pstW.setString(2, model);
pstW.setString(3, owner);
pstW.setString(4, year);
pstW.setString(5, description);
pstW.setString(6, subname);
pstW.setString(7, series);
pstW.setString(8, worth);
pstC.executeUpdate();
pstD.executeUpdate();
pstW.executeUpdate();
pstC.close();
pstD.close();
pstW.close();
} catch (ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
} catch (SQLException e) {
out.println("SQLException caught: " + e.getMessage());
} catch (Exception e) {
out.println(e);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ignored) {
out.println(ignored);
}
}
try {
String collectableView = "SELECT * FROM COLLECTABLE";
String desiredView = "SELECT * FROM ITEM_DESIRED";
String qWantView = "SELECT * FROM QUALITIES DESIRED";
PreparedStatement cView = con.prepareStatement(collectableView);
PreparedStatement dView = con.prepareStatement(desiredView);
PreparedStatement qView = con.prepareStatement(qWantView);
//TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Your Data has been saved</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet Collect at " + request.getContextPath () + "</h1>");
out.println("<p>Collectable Table</p>");
out.println("<p>" + cView.executeQuery(collectableView) + "</p>");
out.println("<p>Item Desired Table</p>");
out.println("<p>" + dView.executeQuery(desiredView) + "</p>");
out.println("<p>Qualities Desired Table</p>");
out.println("<p>" + qView.executeQuery(qWantView) + "</p>");
out.println("</body>");
out.println("</html>");
cView.close();
dView.close();
qView.close();
} finally {
out.close();
}
}
}
// <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>
private void processRequest(HttpServletRequest request, HttpServletResponse response) {
//throw new UnsupportedOperationException("Not yet implemented");
}
}
所以我的问题是我似乎无法摆脱这里的错误
try {
String collectableView = "SELECT * FROM COLLECTABLE";
String ownedView = "SELECT * FROM ITEM_OWNED";
String qualitiesView = "SELECT * FROM QUALITIES_OWNED";
String valueView = "SELECT * FROM ITEM_VALUE";
String cView = con.executeSQLQuery(collectableView);
String oView = con.executeSQLQuery(ownedView);
String qView = con.executeSQLQuery(qualitiesView);
String vView = con.executeSQLQuery(valueView);
//TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Your Data has been saved</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet Collect at " + request.getContextPath () + "</h1>");
out.println("<p>Collectable Table</p>");
out.println("<p>" + cView.executeQuery(collectableView) + "</p>");
out.println("<p>Item Owned Table</p>");
out.println("<p>" + oView.executeQuery(ownedView) + "</p>");
out.println("<p>Qualities Owned Table</p>");
out.println("<p>" + qView.executeQuery(qualitiesView) + "</p>");
out.println("<p>Item Value</p>");
out.println("<p>" + vView.executeQuery(valueView) + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
} else if (owned.equals("a20")) {
try {
//Load the Driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
//Get a Connection to the database
con = DriverManager.getConnection("jdbc:derby://localhost:1527/CollectionDB", "root", "project366");
//ADD DATA TO DATABASE
String collectableString = "INSERT INTO COLLECTABLE VALUES(?,?)";
String desiredString = "INSERT INTO ITEM_DESIRED VALUES(?,?,?,?)";
String qualityWantString = "INSERT INTO QUALITIES DESIRED VALUES(?,?,?,?,?,?,?,?)";
PreparedStatement pstC = con.prepareStatement(collectableString);
pstC.setString(1, brand);
pstC.setString(2, category);
PreparedStatement pstD = con.prepareStatement(desiredString);
pstD.setString(1, name);
pstD.setString(2, model);
pstD.setString(3, owner);
pstD.setString(4, brand);
PreparedStatement pstW = con.prepareStatement(qualityWantString);
pstW.setString(1, name);
pstW.setString(2, model);
pstW.setString(3, owner);
pstW.setString(4, year);
pstW.setString(5, description);
pstW.setString(6, subname);
pstW.setString(7, series);
pstW.setString(8, worth);
pstC.executeUpdate();
pstD.executeUpdate();
pstW.executeUpdate();
pstC.close();
pstD.close();
pstW.close();
} catch (ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
} catch (SQLException e) {
out.println("SQLException caught: " + e.getMessage());
} catch (Exception e) {
out.println(e);
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ignored) {
out.println(ignored);
}
}
try {
String collectableView = "SELECT * FROM COLLECTABLE";
String desiredView = "SELECT * FROM ITEM_DESIRED";
String qWantView = "SELECT * FROM QUALITIES DESIRED";
PreparedStatement cView = con.prepareStatement(collectableView);
PreparedStatement dView = con.prepareStatement(desiredView);
PreparedStatement qView = con.prepareStatement(qWantView);
//TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Your Data has been saved</title>");
out.println("</head>");
out.println("<body>");
//out.println("<h1>Servlet Collect at " + request.getContextPath () + "</h1>");
out.println("<p>Collectable Table</p>");
out.println("<p>" + cView.executeQuery(collectableView) + "</p>");
out.println("<p>Item Desired Table</p>");
out.println("<p>" + dView.executeQuery(desiredView) + "</p>");
out.println("<p>Qualities Desired Table</p>");
out.println("<p>" + qView.executeQuery(qWantView) + "</p>");
out.println("</body>");
out.println("</html>");
cView.close();
dView.close();
qView.close();
} finally {
out.close();
}
}
}
我收到错误消息
未报告的异常java.sql.SQLException;必须被抓住或抛出。
我的小组和我已经尝试了几种不同的方法来“修复”这个,因为你可以说没有任何改变。任何帮助将非常感谢,我会尽力回答任何问题。我们在Debian上使用Netbeans 7.0.1并使用Apache Tomcat 7.0.14.0。
编辑:抱歉,离开了。感谢所有的提示。我们现在只有八个错误(所有处理相同的事情)。它们发生在
String cView = con.executeSQLQuery(collectableView);
String oView = con.executeSQLQuery(ownedView);
String qView = con.executeSQLQuery(qualitiesView);
String vView = con.executeSQLQuery(valueView);
所有executeSQLQuery(拥有/ qualties / value)都加下划线并说明 找不到标志 symbol:方法executeQuery(java.lang.String) location:变量oView类型java.lang.String
答案 0 :(得分:0)
缺少有关错误发生位置的详细信息,看起来您正在尝试执行SQL而不会捕获SQLException
的代码块。
try {
...
PreparedStatement cView = con.prepareStatement(collectableView);
PreparedStatement dView = con.prepareStatement(desiredView);
PreparedStatement qView = con.prepareStatement(qWantView);
...
cView.close();
dView.close();
qView.close();
} finally {
out.close();
}
尝试在finally块之前添加catch (SQLException e)
块。
您可能还需要捕获out.close();
抛出的任何异常。
答案 1 :(得分:0)
你似乎在这里有很多错误,让我们先从一些明显的错误开始:
Yo正在执行sql查询而不会捕获sql异常,因此编译器会告诉您需要捕获SQL异常或声明您的方法抛出SQLException。
但是,您的代码似乎存在更多问题:
String vView = con.executeSQLQuery(valueView);
...
out.println("<p>" + vView.executeQuery(valueView) + "</p>");
在你的大多数代码中,con是java.sql.Connection,据我所知,没有 executeSQLQuery(String),并且没有 executeQuery(String) on vView是一个 String 。
我不确定您的表格中有哪些数据或者您希望如何呈现它,但这可能有助于您前进
String valueView = "SELECT * FROM ITEM_VALUE";
String vView = getSQLAsText(con, valueView);
...
out.println(vView);
借助方法
private String getSQLAsText(Connection connection, String sql) {
StringBuilder buff = new StringBuilder();
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
buff.append(resultSet.getString(1));
buff.append("<br/>");
}
} catch (SQLException e) {
buff.append(e.getMessage());
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
}
}
if (statement != null) {
try {
resultSet.close();
} catch (SQLException e) {
}
}
}
return buff.toString();
}
它应该获取所选行的第一列,并将它们输出为带有&lt; / br&gt;的字符串在每个项目之后。